3

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'
MaxPower
  • 415
  • 1
  • 6
  • 16

3 Answers3

6

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'))
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Silly question perhaps... Would this work on a mapped network drive? – MaxPower Jun 19 '13 at 12:43
  • @MaxPower: I see no reason it wouldn't work. You could test it. – jfs Jun 19 '13 at 13:22
  • I don't understand why filename = sys.argv[1] I'm trying to test it on 1 avi file and i can't manage to make it work. Eventually i want to run it on 100-200 avi files... – MaxPower Jun 19 '13 at 14:15
  • 1
    @MaxPower: `sys.argv` is to allow running the above code as a standalone script. In your script `filename = fpath`. If you are on Windows, you could skip `getTerminalCharset()`, `unicodeFilename()` calls and pass a Unicode filename directly to `createParser()` (use `path = ur"c:\Python27\test"`) – jfs Jun 19 '13 at 15:00
  • 1
    @MaxPower: I've added an example how to print info about all avi files in a directory tree – jfs Jun 20 '13 at 13:48
2

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=
holms
  • 9,112
  • 14
  • 65
  • 95
  • I don't understand this part "| grep duration | cut -f2 -d=" do you mean by " | " "OR" ? – MaxPower Jun 19 '13 at 13:00
  • it's bash shell commands. http://stackoverflow.com/questions/89228/calling-an-external-command-in-python – holms Jun 19 '13 at 17:31
1

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

robjohncox
  • 3,639
  • 3
  • 25
  • 51