2017-04-27 6 views
13

파이썬은파이프 PIL 이미지 - 나는 MP4 비디오에 HTML5 비디오를 변환 할 노력하고있어

가 나는 또한 그래서 결국 PIL을 사용하여 이미지를 자르기있어 시간이 지남에 PhantomJS를 통해 촬영 화면이 그렇게하고 내 코드는 대략 다음과 같습니다

while time() < end_time: 
    screenshot_list.append(phantom.get_screenshot_as_base64()) 
. 
. 
for screenshot in screenshot_list: 
    im = Image.open(BytesIO(base64.b64decode(screenshot))) 
    im = im.crop((left, top, right, bottom)) 

지금 내가 디스크에 모든 이미지를 저장하고 저장된 파일로부터는 FFmpeg을 사용하고 있습니다 :

os.system('ffmpeg -r {fps} -f image2 -s {width}x{height} -i {screenshots_dir}%04d.png -vf scale={width}:-2 ' 
     '-vcodec libx264 -crf 25 -vb 20M -pix_fmt yuv420p {output}'.format(fps=fps, width=width, 
                    screenshots_dir=screenshots_dir, 
                    height=height, output=output)) 

하지만 대신 그 저장된 파일을 사용하고자 할 수 있도록 PIL.Images 지시문을 ffmpeg로 파이프 처리하려면 어떻게해야합니까?

+0

ffmpeg 부분은'ffmpeg -f image2pipe -vcodec png -i - -vf ....'처럼 시작해야합니다. PIL이 적절한 PNG 구문을 출력하면 입력 이미지 크기를 지정하지 않아도됩니다. – Mulvya

+0

때로는 2로 나눌 수 없기 때문에 문제가 발생하여 처리하고 지정합니다. – bluesummers

+0

스케일 필터 인수가 mod 2 요구 사항을 처리합니다. 그것은 이미지를 섭취하는 것과 관련이 없습니다. – Mulvya

답변

6

바운티는 사라졌지만 해결책을 찾았습니다.

64 기수 문자열로 스크린 샷을 모두 획득 한 후 내가 실행 시간이 문제가

import subprocess as sp 

# Generating all of the screenshots as base64 
# in a variable called screenshot_list 

cmd_out = ['ffmpeg', 
      '-f', 'image2pipe', 
      '-vcodec', 'png', 
      '-r', '30', # FPS 
      '-i', '-', # Indicated input comes from pipe 
      '-vcodec', 'png', 
      '-qscale', '0', 
      '/home/user1/output_dir/video.mp4'] 

pipe = sp.Popen(cmd_out, stdin=sp.PIPE) 

for screenshot in screenshot_list: 
    im = Image.open(BytesIO(base64.b64decode(screenshot))) 
    im.save(pipe.stdin, 'PNG') 

pipe.stdin.close() 
pipe.wait() 

# Make sure all went well 
if pipe.returncode != 0: 
    raise sp.CalledProcessError(pipe.returncode, cmd_out) 

경우 다음 코드를 사용하여 서브 프로세스로 쓰기 대신 JPEG 파일로 이미지를 저장하고 적절한 코덱을 사용할 수 있습니다 그것을 위해, 그러나 내가 달성 할 수 있었던 가장 높은 품질은이 설정을 가지고있었습니다.