2017-12-20 6 views
2

나는 파이썬이 비디오 파일을 열고 ffmpeg 명령의 표준 입력으로 스트림하는 프로세스를 만들고 있습니다. 나는 올바른 생각을 가지고 있다고 생각하지만 파일이 열리는 방식이 stdin과 작동하지 않습니다. 여기 내 코드는 지금까지 있습니다 :이 파이프에 비디오 파일을 스트리밍/열표준 비디오 파일을 바이트로 stdin

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

어떤 것이 올바른 방법 :

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) 

이 오류를 얻을 수? 또한 모든 것을 메모리로 읽어주기보다는 스트림이되어야합니다.

추신. 나는 기본적으로 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를 사용

관련 문제