3
votes

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 '\n'
3
The length of the video in seconds you mean? Or the length of the file in Bytes? - Inbar Rose
length in seconds. Sorry, had to be more specific. - MaxPower

3 Answers

6
votes

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'))
2
votes

If your server running any UNIX operation system you can use ffmpeg to do this. Usually just default command like ffmpeg myvideo.avi will give you full video details.

There's also a python wrapper for ffmpeg which probably will return video details in dictionary or list.

EDIT:

I've also found nice ffmpeg tool called ffprobe which can output length of video without additional fuss.

fprobe -loglevel error -show_streams inputFile.avi | grep duration | cut -f2 -d=
1
votes

Not sure if there is a platform independent way to do this, but if you only need this to work on windows then it looks like MediaInfo (below) has a command line interface which you can use to output details about video files, which could then be parsed to get the information. Not the prettiest solution but looks like it should work.

http://mediainfo.sourceforge.net/en