【问题标题】:How to get the duration of a video in Python?如何在 Python 中获取视频的时长?
【发布时间】:2011-04-20 03:43:21
【问题描述】:

我需要在 Python 中获取视频时长。我需要获取的视频格式是MP4、Flash 视频、AVI 和 MOV...我有一个共享托管解决方案,所以我没有FFmpeg 支持。

【问题讨论】:

  • 因为您没有 ffmpeg(答案无法使用),请查看以下其他答案:0123

标签: python video


【解决方案1】:

您可以为此使用外部命令ffprobe。具体来说,从 FFmpeg Wiki 运行 this bash command

import subprocess

def get_length(filename):
    result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
                             "format=duration", "-of",
                             "default=noprint_wrappers=1:nokey=1", filename],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
    return float(result.stdout)

【讨论】:

  • 你也可以使用-print_format json-show_streams 然后json.loads 得到一个包含所有ffprobe信息的字典。这也需要解析 ffprobe 打印出的额外信息(还没有找到隐藏它的选项)
  • @dennmat: -loglevel 不是 -log_level(至少使用 ffprobe 2.3.3)
  • python wrapper ffprobe 可能也很方便,int(float(FFProbe(filename).video[0].duration) * 1000) 让您获得毫秒数
  • 使用ffprobe Python 模块,它的from ffprobe import FFProbe; FFProbe('path/to/file').streams[0].duration
  • 运行此代码时出现错误File Not Found。虽然文件存在。
【解决方案2】:

据此报道https://www.reddit.com/r/moviepy/comments/2bsnrq/is_it_possible_to_get_the_length_of_a_video/

你可以使用moviepy模块

from moviepy.editor import VideoFileClip
clip = VideoFileClip("my_video.mp4")
print( clip.duration )

【讨论】:

  • ffpobe 比 moviepy 快 10 倍。
  • 谢谢。为我工作。
  • 这对我来说足够快而且简单。谢谢你。请注意,此代码的输出单位是秒。
  • 也适用于 URL。
【解决方案3】:

2020 年答案

解决方案:

  1. opencv 0.0065 秒 ✔
  2. ffprobe 0.0998 秒
  3. moviepy 2.8239 秒

OpenCV 方法:

def with_opencv(filename):
    import cv2
    video = cv2.VideoCapture(filename)

    duration = video.get(cv2.CAP_PROP_POS_MSEC)
    frame_count = video.get(cv2.CAP_PROP_FRAME_COUNT)

    return duration, frame_count

用法: print(with_opencv('my_video.webm'))


其他:

ffprobe方法:

def with_ffprobe(filename):
    import subprocess, json

    result = subprocess.check_output(
            f'ffprobe -v quiet -show_streams -select_streams v:0 -of json "{filename}"',
            shell=True).decode()
    fields = json.loads(result)['streams'][0]

    duration = fields['tags']['DURATION']
    fps      = eval(fields['r_frame_rate'])
    return duration, fps

moviepy方法:

def with_moviepy(filename):
    from moviepy.editor import VideoFileClip
    clip = VideoFileClip(filename)
    duration       = clip.duration
    fps            = clip.fps
    width, height  = clip.size
    return duration, fps, (width, height)

【讨论】:

  • 您的 OpenCV 方法确实很有趣。然而,它似乎容易出错,经常返回 0 值,显然是由于编解码器。我无法正确计算单个文件。如果您对此添加几句话,我很乐意投票=)
  • 似乎可以通过读取 fps 来修复:fps = video.get(cv2.CAP_PROP_FPS) duration = frame_count / fps
  • 对于我的视频,OpenCV 代码返回 0 持续时间。但是帧计数值很好,因此可以调用fps = video.get(cv2.CAP_PROP_FPS) 并通过frame_count/fps 获取持续时间。 OpenCV 方法比使用 ffmpeg 的子进程更快,但是您必须在系统中安装很多东西才能使其正常工作。对我来说,“pymediainfo”似乎快了两倍
  • opencv不可靠,见github官方issuegithub.com/opencv/opencv/issues/15749#issuecomment-796512723
【解决方案4】:

为了让事情更简单一些,下面的代码将输出放到 JSON

你可以通过probe(filename)来使用它,或者通过duration(filename)来获取持续时间:

json_info     = probe(filename)
secondes_dot_ = duration(filename) # float number of seconds

它在Ubuntu 14.04 上工作,当然ffprobe 安装在其中。该代码未针对速度或美观目的进行优化,但它可以在我的机器上运行,希望对您有所帮助。

#
# Command line use of 'ffprobe':
#
# ffprobe -loglevel quiet -print_format json \
#         -show_format    -show_streams \
#         video-file-name.mp4
#
# man ffprobe # for more information about ffprobe
#

import subprocess32 as sp
import json


def probe(vid_file_path):
    ''' Give a json from ffprobe command line

    @vid_file_path : The absolute (full) path of the video file, string.
    '''
    if type(vid_file_path) != str:
        raise Exception('Gvie ffprobe a full file path of the video')
        return

    command = ["ffprobe",
            "-loglevel",  "quiet",
            "-print_format", "json",
             "-show_format",
             "-show_streams",
             vid_file_path
             ]

    pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT)
    out, err = pipe.communicate()
    return json.loads(out)


def duration(vid_file_path):
    ''' Video's duration in seconds, return a float number
    '''
    _json = probe(vid_file_path)

    if 'format' in _json:
        if 'duration' in _json['format']:
            return float(_json['format']['duration'])

    if 'streams' in _json:
        # commonly stream 0 is the video
        for s in _json['streams']:
            if 'duration' in s:
                return float(s['duration'])

    # if everything didn't happen,
    # we got here because no single 'return' in the above happen.
    raise Exception('I found no duration')
    #return None


if __name__ == "__main__":
    video_file_path = "/tmp/tt1.mp4"
    duration(video_file_path) # 10.008

【讨论】:

  • 在返回 jason.loads(out) 之前,应该将 out 解码为 str。 out = out.decode('utf-8')
  • 喜欢这个解决方案
【解决方案5】:

找到这个新的 python 库:https://github.com/sbraz/pymediainfo

获取持续时间:

from pymediainfo import MediaInfo
media_info = MediaInfo.parse('my_video_file.mov')
#duration in milliseconds
duration_in_ms = media_info.tracks[0].duration

以上代码已针对有效的 mp4 文件进行了测试并且可以正常工作,但您应该进行更多检查,因为它严重依赖 MediaInfo 的输出。

【讨论】:

  • 需要单独的库 (libmediainfo)。
  • 这是我发现的最快的,甚至比moviepy或ffprobe还要快。你是英雄,谢谢!!
  • 您可能应该使用max([float(track.duration) for track in MediaInfo.parse('my_video_file.mov').tracks]) 来检查所有曲目,因为第一首曲目不一定必须与实际“整个”视频的长度相同。
【解决方案6】:
from subprocess import check_output

file_name = "movie.mp4"

#For Windows
a = str(check_output('ffprobe -i  "'+file_name+'" 2>&1 |findstr "Duration"',shell=True)) 

#For Linux
#a = str(check_output('ffprobe -i  "'+file_name+'" 2>&1 |grep "Duration"',shell=True)) 

a = a.split(",")[0].split("Duration:")[1].strip()

h, m, s = a.split(':')
duration = int(h) * 3600 + int(m) * 60 + float(s)

print(duration)

【讨论】:

  • 投反对票很好,但请礼貌解释原因!上面发布的代码是对 OP 提出的问题的明确解决方案
  • 在 Windows 机器上,这个答案不是假设用户在路径上有 ffprobe 吗?我知道您只是在强调 Windows 上缺少 grep,但要使用确切的语法,它需要编辑环境变量或需要从与 ffprobe 相同的目录运行。
【解决方案7】:

我想出的一个功能。这基本上是仅使用ffprobe 参数

from subprocess import  check_output, CalledProcessError, STDOUT 


def getDuration(filename):

    command = [
        'ffprobe', 
        '-v', 
        'error', 
        '-show_entries', 
        'format=duration', 
        '-of', 
        'default=noprint_wrappers=1:nokey=1', 
        filename
      ]

    try:
        output = check_output( command, stderr=STDOUT ).decode()
    except CalledProcessError as e:
        output = e.output.decode()

    return output


fn = '/app/648c89e8-d31f-4164-a1af-034g0191348b.mp4'
print( getDuration(  fn ) )

像这样输出持续时间:

7.338000

【讨论】:

  • 这个解决方案替换了我之前的代码。一次复制和粘贴,我的程序又开始工作了。干杯。
【解决方案8】:

据报道这里 https://www.reddit.com/r/moviepy/comments/2bsnrq/is_it_possible_to_get_the_length_of_a_video/

你可以使用moviepy模块

from moviepy.editor import VideoFileClip 
clip = VideoFileClip("my_video.mp4") 
print( clip.duration )

如果您尝试获取文件夹中多个视频的持续时间,则会崩溃并出现以下错误: AttributeError: 'AudioFileClip' 对象没有属性 'reader'

所以,为了避免你需要添加

clip.close()

基于此: https://zulko.github.io/moviepy/_modules/moviepy/video/io/VideoFileClip.html

所以代码看起来像这样:

from moviepy.editor import VideoFileClip
clip = VideoFileClip("my_video.mp4")
print( clip.duration )
clip.close()

干杯! :)

【讨论】:

    【解决方案9】:

    https://github.com/kkroening/ffmpeg-python (pip install ffmpeg-python --user) 使用现代方法。别忘了也安装ffmpeg

    获取视频信息:

    import ffmpeg
    
    info=ffmpeg.probe(filename)
    
    print(f"duration={info['format']['duration']}")
    print(f"framerate={info['streams'][0]['avg_frame_rate']}")
    

    使用ffmpeg-python 包也可以轻松创建、编辑和应用过滤器到视频。

    【讨论】:

      【解决方案10】:

      上面的 pymediainfo 答案真的帮助了我。谢谢。

      作为初学者,确实需要一段时间才能找出缺少的内容(sudo apt install mediainfo)以及如何以其他方式处理属性(见下文)。

      因此这个额外的例子:

      # sudo apt install mediainfo
      # pip3 install pymediainfo
      from pymediainfo import MediaInfo
      media_info = MediaInfo.parse('/home/pi/Desktop/a.mp4')
      for track in media_info.tracks:
          #for k in track.to_data().keys():
          #    print("{}.{}={}".format(track.track_type,k,track.to_data()[k]))
          if track.track_type == 'Video':
              print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
              print("{} width                 {}".format(track.track_type,track.to_data()["width"]))
              print("{} height                {}".format(track.track_type,track.to_data()["height"]))
              print("{} duration              {}s".format(track.track_type,track.to_data()["duration"]/1000.0))
              print("{} duration              {}".format(track.track_type,track.to_data()["other_duration"][3][0:8]))
              print("{} other_format          {}".format(track.track_type,track.to_data()["other_format"][0]))
              print("{} codec_id              {}".format(track.track_type,track.to_data()["codec_id"]))
              print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
          elif track.track_type == 'Audio':
              print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
              print("{} format                {}".format(track.track_type,track.to_data()["format"]))
              print("{} codec_id              {}".format(track.track_type,track.to_data()["codec_id"]))
              print("{} channel_s             {}".format(track.track_type,track.to_data()["channel_s"]))
              print("{} other_channel_s       {}".format(track.track_type,track.to_data()["other_channel_s"][0]))
              print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
      print("********************************************************************")
      
      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      Video width                 1920
      Video height                1080
      Video duration              383.84s
      Video duration              00:06:23
      Video other_format          AVC
      Video codec_id              avc1
      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      Audio format                AAC
      Audio codec_id              mp4a-40-2
      Audio channel_s             2
      Audio other_channel_s       2 channels
      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      

      【讨论】:

        【解决方案11】:

        打开 cmd 终端并安装 python 包:mutagen 使用此命令

        python -m pip install mutagen

        然后使用此代码获取视频时长及其大小:

        import os
        from mutagen.mp4 import MP4
        
        audio = MP4("filePath")
        
        print(audio.info.length)
        print(os.path.getsize("filePath"))
        

        【讨论】:

        • 对我来说,它返回 0.0 用于 5:30 的视频文件。不处理 AVI,并且需要针对不同文件类型/扩展名的不同对象。
        【解决方案12】:

        对于喜欢使用 mediainfo 程序的任何人:

        import json
        import subprocess
        
        #===============================
        def getMediaInfo(mediafile):
            cmd = "mediainfo --Output=JSON %s"%(mediafile)
            proc = subprocess.Popen(cmd, shell=True,
                stderr=subprocess.PIPE, stdout=subprocess.PIPE)
            stdout, stderr = proc.communicate()
            data = json.loads(stdout)
            return data
        
        #===============================
        def getDuration(mediafile):
            data = getMediaInfo(mediafile)
            duration = float(data['media']['track'][0]['Duration'])
            return duration
        

        【讨论】:

          【解决方案13】:

          在函数中使用 ffprobe 以秒为单位返回视频的持续时间。

          def video_duration(filename):
              import subprocess
              secs = subprocess.check_output(f'ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "{filename}"', shell=True).decode()
              return secs
          

          【讨论】:

            【解决方案14】:

            这是我导出的方法。cv2 方式适用于 mp4、wmv 和 flv,这是我需要的:

            try:
                import cv2  # opencv-python - optional if using ffprobe
            except ImportError:
                cv2 = None
            
            import subprocess
            
            def get_playback_duration(video_filepath, method='cv2'):  # pragma: no cover
                """
                Get video playback duration in seconds and fps
                """
                if method == 'cv2':  # Use opencv-python
                    video = cv2.VideoCapture(video_filepath)
                    fps = video.get(cv2.CAP_PROP_FPS)
                    duration_seconds = video.get(cv2.CAP_PROP_FRAME_COUNT) / fps
                else:
                    result = subprocess.check_output(f'ffprobe -v quiet -show_streams -select_streams v:0 -of json '
                                                     f'"{video_filepath}"', shell=True).decode()
                    fields = json.loads(result)['streams'][0]
                    duration_seconds = fields['tags']['DURATION']
                    fps = eval(fields['r_frame_rate'])
                return duration_seconds, fps
            

            ffprobe 不适用于 flv。

            【讨论】:

              猜你喜欢
              • 2016-02-15
              • 2015-09-10
              • 1970-01-01
              • 1970-01-01
              • 2018-05-15
              • 2016-07-29
              • 2011-04-25
              • 1970-01-01
              • 2012-04-28
              相关资源
              最近更新 更多