Get total length of videos in a particular directory in python

后端 未结 4 2039
后悔当初
后悔当初 2020-12-03 23:55

I have downloaded a bunch of videos from coursera.org and have them stored in one particular folder. There are many individual videos in a particular folder (Coursera breaks

相关标签:
4条回答
  • 2020-12-04 00:14
    1. Download MediaInfo and install it (don't install the bundled adware)
    2. Go to the MediaInfo source downloads and in the "Source code, All included" row, choose the link next to "libmediainfo"
    3. Find MediaInfoDLL3.py in the downloaded archive and extract it anywhere. Example location: libmediainfo_0.7.62_AllInclusive.7z\MediaInfoLib\Source\MediaInfoDLL\MediaInfoDLL3.py
    4. Now make a script for testing (sources below) in the same directory.
    5. Execute the script.

    MediaInfo works on POSIX too. The only difference is that an so is loaded instead of a DLL.

    Test script (Python 3!)

    import os
    
    os.chdir(os.environ["PROGRAMFILES"] + "\\mediainfo")
    from MediaInfoDLL3 import MediaInfo, Stream
    
    MI = MediaInfo()
    
    def get_lengths_in_milliseconds_of_directory(prefix):
      for f in os.listdir(prefix):
        MI.Open(prefix + f)
        duration_string = MI.Get(Stream.Video, 0, "Duration")
    
        try:
          duration = int(duration_string)
          yield duration
          print("{} is {} milliseconds long".format(f, duration))
        except ValueError:
          print("{} ain't no media file!".format(f))
    
        MI.Close()
    
    print(sum(get_lengths_in_milliseconds_of_directory(os.environ["windir"] + "\\Performance\\WinSAT\\"
    )), "milliseconds of content in total")
    
    0 讨论(0)
  • 2020-12-04 00:15

    This link shows how to get the length of a video file https://stackoverflow.com/a/3844467/735204

    import subprocess
    
    def getLength(filename):
      result = subprocess.Popen(["ffprobe", filename],
        stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
      return [x for x in result.stdout.readlines() if "Duration" in x]
    

    If you're using that function, you can then wrap it up with something like

    import os
    
    for f in os.listdir('.'):
        print "%s: %s" % (f, getLength(f))
    
    0 讨论(0)
  • 2020-12-04 00:22

    First, install the ffprobe command (it's part of ffmpeg) with

    sudo apt install ffmpeg
    

    then use subprocess.run() to run this bash command:

    ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 <filename>
    

    (which I got from http://trac.ffmpeg.org/wiki/FFprobeTips#Formatcontainerduration), like this:

    from pathlib import Path
    import subprocess
    
    def video_length_seconds(filename):
        result = subprocess.run(
            [
                "ffprobe",
                "-v",
                "error",
                "-show_entries",
                "format=duration",
                "-of",
                "default=noprint_wrappers=1:nokey=1",
                filename,
            ],
            capture_output=True,
            text=True,
        )
        try:
            return float(result.stdout)
        except ValueError:
            raise ValueError(result.stderr.rstrip("\n"))
    
    # a single video
    video_length_seconds('your_video.webm')
    
    # all mp4 files in the current directory (seconds)
    print(sum(video_length_seconds(f) for f in Path(".").glob("*.mp4")))
    
    # all mp4 files in the current directory and all its subdirectories
    # `rglob` instead of `glob`
    print(sum(video_length_seconds(f) for f in Path(".").rglob("*.mp4")))
    
    # all files in the current directory
    print(sum(video_length_seconds(f) for f in Path(".").iterdir() if f.is_file()))
    

    This code requires Python 3.7+ because that's when text= and capture_output= were added to subprocess.run. If your Python version is older and you can't upgrade, check the edit history of this answer for an older version.

    0 讨论(0)
  • 2020-12-04 00:23

    In addition to Janus Troelsen's answer above, I would like to point out a small problem I encountered when implementing his answer. I followed his instructions one by one but had different results on windows (7) and linux (ubuntu). His instructions worked perfectly under linux but I had to do a small hack to get it to work on windows. I am using a 32-bit python 2.7.2 interpreter on windows so I utilized MediaInfoDLL.py. But that was not enough to get it to work for me I was receiving this error at this point in the process:

    "WindowsError: [Error 193] %1 is not a valid Win32 application".

    This meant that I was somehow using a resource that was not 32-bit, it had to be the DLL MediaInfoDLL.py was loading. If you look at the MediaInfo intallation directory you will see 3 dlls MediaInfo.dll is 64-bit while MediaInfo_i386.dll is 32-bit. MediaInfo_i386.dll is the one which I had to use because of my python setup. I went to MediaInfoDLL.py (which I already had included in my project) and changed this line:

    MediaInfoDLL_Handler = windll.MediaInfo
    

    to

    MediaInfoDLL_Handler = WinDLL("C:\Program Files (x86)\MediaInfo\MediaInfo_i386.dll")
    

    I didn't have to change anything for it to work in linux

    0 讨论(0)
提交回复
热议问题