How to get .avi files length

后端 未结 3 790
梦毁少年i
梦毁少年i 2020-12-10 21:06

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

相关标签:
3条回答
  • 2020-12-10 21:44

    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

    0 讨论(0)
  • 2020-12-10 21:53

    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'))
    
    0 讨论(0)
  • 2020-12-10 22:03

    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=
    
    0 讨论(0)
提交回复
热议问题