Skip to content

Instantly share code, notes, and snippets.

@sazio
Created August 28, 2020 14:20
Show Gist options
  • Save sazio/572046a2ea22f7b7fde808c9f01b27cb to your computer and use it in GitHub Desktop.
Save sazio/572046a2ea22f7b7fde808c9f01b27cb to your computer and use it in GitHub Desktop.
def run_command(*popenargs, **kwargs):
closeNULL = 0
try:
from subprocess import DEVNULL
closeNULL = 0
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
closeNULL = 1
process = sp.Popen(stdout=sp.PIPE, stderr=DEVNULL, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if closeNULL:
DEVNULL.close()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = sp.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
def ffprobe(filename, options = ["-show_error", "-show_format", "-show_streams", "-show_programs", "-show_chapters", "-show_private_data"]):
ret = {}
command = [FFMPEG_PATH + "/ffprobe", "-v", "error", *options, "-print_format", "json", filename]
ret = run_command(command)
if ret:
ret = json.loads(ret)
return ret
# ffmpeg -i input.mov -r 0.25 output_%04d.png
def ffextract_frames(filename, output_folder, rate = 0.25):
command = [FFMPEG_PATH + "/ffmpeg", "-i", filename, "-r", str(rate), "-y", output_folder + "/output_%04d.png"]
ret = run_command(command)
return ret
# ffmpeg -i input-video.mp4 output-audio.mp3
def ffextract_audio(filename, output_path):
command = [FFMPEG_PATH + "/ffmpeg", "-i", filename, "-vn", "-ac", "1", "-acodec", "copy", "-y", output_path]
ret = run_command(command)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment