2017-12-20 30 views
2

私は、pythonがビデオファイルを開き、ffmpegコマンドのstdinにストリームするプロセスを作成しています。私は正しいアイデアを持っていると思うが、ファイルが開かれている方法はstdinで動作していない。ここに私のコードは、これまでです:ストリームビデオファイルをバイトとして標準入力

def create_pipe(): 

    return Popen([ 'ffmpeg', 
         '-loglevel', 'panic', 
         '-s', '1920x1080', 
         '-pix_fmt', 'yuvj420p', 
         '-y', 
         '-f', 'image2pipe', 
         '-vcodec', 'mjpeg', 
         '-r', self.fps, 
         '-i', '-', 
         '-r', self.fps, 
         '-s', '1920x1080', 
         '-f', 'mov', 
         '-vcodec', 'prores', 
         '-profile:v', '3', 
         '-aspect', '16:9', '-y', 
         'output_file_name' + '.mov'], stdin=PIPE) 



in_pipe = create_pipe() 
with open("~/Desktop/IMG_1973.mov", "rb") as f: 
    in_pipe.communicate(input=f) 

これは、エラーが得られます。

TypeError: a bytes-like object is required, not '_io.BufferedReader'

このパイプにストリーム/ビデオファイルを開くための正しい方法でしょうか?私はまた、すべてを記憶に読み込むのではなく、ストリームにする必要があります。

PS。私はネイティブにffmpegでファイルを開くことができることを無視してください...私はラッパーを作成しています、そして、私は入力を制御することができます。

答えて

1

まず、入力形式をパイプできることを確認します。 ISO BMFFでは、moovアトムは、これが機能するためにファイルの先頭になければなりません。

def read_bytes(input_file, read_size=8192): 
    """ read 'read_size' bytes at once """ 
    while True: 
     bytes_ = input_file.read(read_size) 
     if not bytes_: 
      break 
     yield bytes_ 


def main(): 
    in_pipe = create_pipe() 

    with open("in.mov", "rb") as f: 
     for bytes_ in read_bytes(f): 
      in_pipe.stdin.write(bytes_) 

    in_pipe.stdin.close() 
    in_pipe.wait() 
:それはpipeableなら

は、サブプロセスへのファイルやパイプ、それを読むためにgeneratorを使用します

関連する問題