getmediainfo.py
import subprocess
def get_duration(file):
"""Get the duration of a video using ffprobe."""
cmd="ffprobe -i %s -show_entries format=duration -v quiet -of csv=\"p=0\"" %(file)
output = subprocess.check_output(
cmd,
shell=True, # Let this run in the shell
stderr=subprocess.STDOUT
)
# return round(float(output)) # ugly, but rounds your seconds up or down
return float(output)
if __name__ == "__main__":
ret = get_duration("/data/media/0.mp4")
print(ret)
执行:
$ python getmediainfo.py
19.367