Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

python - How to get .avi files length

I am trying to loop over a directory of sub folders where every folder contains one .avi file that i want to retrieve its length in seconds.

I've found PyMedia http://pymedia.org/ and i understand it could possibly help me achieve this but i cannot find anything about avi duration / length in the documentation.

How would i be able to do that? also, if there is a different library of some sort i'd like to know aswel.

Edit: Added my final solution that works thanks to J.F. Sebastian

import sys
import glob
import os

from hachoir_core.cmd_line import unicodeFilename
from hachoir_core.i18n import getTerminalCharset
from hachoir_metadata import extractMetadata
from hachoir_parser import createParser

path = "z:*"
for fpath in glob.glob(os.path.join(path, '*avi')):
    filename = fpath
    filename, real_filename = unicodeFilename(filename), filename
    parser = createParser(filename, real_filename=real_filename)
    metadata = extractMetadata(parser)
    print fpath
    print("Duration (hh:mm:ss.f): %s" % metadata.get('duration'))
    print '
'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use hachoir-metadata to extract avi duration from a file:

#!/usr/bin/env python
import sys

# $ pip install hachoir-{core,parser,metadata}
from hachoir_core.cmd_line import unicodeFilename
from hachoir_core.i18n import getTerminalCharset
from hachoir_metadata import extractMetadata
from hachoir_parser import createParser


filename = sys.argv[1]
charset = getTerminalCharset()
filename, real_filename = unicodeFilename(filename, charset), filename
parser = createParser(filename, real_filename=real_filename)
metadata = extractMetadata(parser)
print("Duration (hh:mm:ss.f): %s" % metadata.get('duration'))

It uses pure Python RIFF parser to extract info from avi file.

Example:

$ get-avi-duration.py test.avi
Duration (hh:mm:ss.f): 0:47:03.360000

Here's ffmpeg's output for comparison:

$ ffmpeg -i test.avi |& grep -i duration
  Duration: 00:47:03.36, start: 0.000000, bitrate: 1038 kb/s

To print info about all avi files in a directory tree:

#!/usr/bin/env python
import os
import sys
from hachoir_metadata import extractMetadata
from hachoir_parser import createParser

def getinfo(rootdir, extensions=(".avi", ".mp4")):
    if not isinstance(rootdir, unicode):
       rootdir = rootdir.decode(sys.getfilesystemencoding())
    for dirpath, dirs, files in os.walk(rootdir):
        dirs.sort() # traverse directories in sorted order
        files.sort()
        for filename in files:
            if filename.endswith(extensions):
               path = os.path.join(dirpath, filename)
               yield path, extractMetadata(createParser(path))

for path, metadata in getinfo(u"z:"):
    if metadata.has('duration'):
        print(path)
        print("  Duration (hh:mm:ss.f): %s" % metadata.get('duration'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...