I’ve installed ffprobe using the pip ffprobe command on my PC, and installed ffmpeg from here.
However, I’m still having trouble running the code listed here.
I try to use the following code unsuccessfully.
JavaScript
x
4
1
SyntaxError: Non-ASCII character 'xe2' in file GetVideoDurations.py
2
on line 12, but no encoding declared; see
3
http://python.org/dev/peps/pep-0263/ for details
4
Does anyone know what’s wrong? Am I not referencing the directories correctly? Do I need to make sure the .py
and video files are in a specific location?
JavaScript
1
11
11
1
import subprocess
2
3
def getLength(filename):
4
result = subprocess.Popen(["ffprobe", "filename"],
5
stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
6
return [x for x in result.stdout.readlines() if "Duration" in x]
7
8
fileToWorkWith = 'C:UsersPCDesktopVideo.mkv'
9
10
getLength(fileToWorkWith)
11
Apologies if the question is somewhat basic. All I need is to be able to iterate over a group of video files and get their start time and end time.
Thank you!
Advertisement
Answer
There is no need to iterate though the output of FFprobe
. There is one simple command which returns only the duration of the input file:
JavaScript
1
2
1
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 <input_video>
2
You can use the following method instead to get the duration:
JavaScript
1
4
1
def get_length(input_video):
2
result = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', input_video], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
3
return float(result.stdout)
4