问题
I want to extract video duration metadata from every video file in a specified directory and then view the total duration.
I need to extract the data from as much as thousands of videos overall. In Windows I can view the total duration for many files manually when selecting them in the explorer and going into details. For 1500 mp4 files it takes about 20 seconds to make the calculations and view the total time. It's relatively much faster then what I'm currently getting when iterating with FFprobe
.
How fast I am currently getting the result with FFprobe.
for filename in dirFiles:
print(subprocess.check_output(['ffprobe', '-i', filename, '-show_entries','format=duration', '-sexagesimal' ,'-v', 'quiet', '-of', 'csv=%s' % ("p=0")]))
What is the faster way to do this?
回答1:
I solved the problem with mutagen
module as it handles files quite fast.
mp4 = mutagen.mp4.MP4(filename)
duration+=mp4.info.length
回答2:
There is no "best" way, just a best way for your use case. os.stat doesn't have it because video duration is not part of any posix file system, (files systems don't care about the contents of a file, Whats is the duration of a text file? what is the resolution of an executable?) If you don't like ffprobe, try mediainfo, or mp4box, or any other tool that can read media files.
来源:https://stackoverflow.com/questions/53144494/get-duration-from-multiple-video-files