I need to grab the duration of a video file via python as part of a larger script. I know I can use ffmpeg to grab the duration, but I need to be able to save that output as a variable back in python. I thought this would work, but it's giving me a value of 0:
cmd = 'ffmpeg -i %s 2>&1 | grep "Duration" | cut -d \' \' -f 4 | sed s/,//' % ("Video.mov")
duration = os.system(cmd)
os.system returns a return value indicating the success or failure of the command. It does not return the output from stdout or stderr. To grab the output from stdout (or stderr), use
import subprocess proc=subprocess.Popen('echo "to stdout"', shell=True, stdout=subprocess.PIPE, ) output=proc.communicate() print output
See the wonderfully written Python Module of the Week blog.