Skip to content

Instantly share code, notes, and snippets.

@genzj
Last active May 8, 2020 23:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genzj/b8ec8131cd7632ab7821 to your computer and use it in GitHub Desktop.
Save genzj/b8ec8131cd7632ab7821 to your computer and use it in GitHub Desktop.
Get the process bar of ffmpeg
#!/usr/bin/env python3
from io import BytesIO
def ffmpeg_it(clip, input, output, overwriteopt='-y'):
cmd = [ffmpeg, '-ss', clip['start'], '-i', input, '-c', 'copy',
'-t', clip['end'], overwriteopt, output ]
ffmpegp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
buf = BytesIO()
while True:
# put the single char to IO buffer in case it's a multi-byte
# character, utf8 for example
out = ffmpegp.stdout.read(1)
buf.write(out)
# decode and display output only when \r or \n met
# it doens't matter for '\n\r' sequence because there is no
# more char in between, they will be printed in adjoint loop,
# as without buffer
if out in (b'\r', b'\n'):
sys.stdout.write(buf.getvalue().decode().strip('\x00'))
buf.truncate(0)
if ffmpegp.returncode is None:
if ffmpegp.poll() is not None: break
else:
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment