【问题标题】:How to get a online video's duration without downloading the full video?如何在不下载完整视频的情况下获取在线视频的时长?
【发布时间】:2015-01-14 11:55:05
【问题描述】:

要获取视频的时长和分辨率,我有这个函数:

def getvideosize(url, verbose=False):
try:
    if url.startswith('http:') or url.startswith('https:'):
        ffprobe_command = ['ffprobe', '-icy', '0', '-loglevel', 'repeat+warning' if verbose else 'repeat+error', '-print_format', 'json', '-select_streams', 'v', '-show_streams', '-timeout', '60000000', '-user-agent', BILIGRAB_UA, url]
    else:
        ffprobe_command = ['ffprobe', '-loglevel', 'repeat+warning' if verbose else 'repeat+error', '-print_format', 'json', '-select_streams', 'v', '-show_streams', url]
    logcommand(ffprobe_command)
    ffprobe_process = subprocess.Popen(ffprobe_command, stdout=subprocess.PIPE)
    try:
        ffprobe_output = json.loads(ffprobe_process.communicate()[0].decode('utf-8', 'replace'))
    except KeyboardInterrupt:
        logging.warning('Cancelling getting video size, press Ctrl-C again to terminate.')
        ffprobe_process.terminate()
        return 0, 0
    width, height, widthxheight, duration = 0, 0, 0, 0
    for stream in dict.get(ffprobe_output, 'streams') or []:
        if dict.get(stream, 'duration') > duration:
            duration = dict.get(stream, 'duration')
        if dict.get(stream, 'width')*dict.get(stream, 'height') > widthxheight:
            width, height = dict.get(stream, 'width'), dict.get(stream, 'height')
    if duration == 0:
        duration = 1800
    return [[int(width), int(height)], int(float(duration))+1]
except Exception as e:
    logorraise(e)
    return [[0, 0], 0]

但有些在线视频没有duration 标签。我们可以做些什么来获得它的持续时间吗?

【问题讨论】:

  • 一般不可能。在许多情况下,时长信息并不容易获得(需要实际的媒体解析)或位于文件末尾。

标签: python video ffmpeg ffprobe


【解决方案1】:

我知道这个问题很老了,但有更好的方法来解决这个问题。

通过将 einverne 的答案与一些实际的 Python(在本例中为 Python 3.5)相结合,我们可以创建一个简短的函数来返回视频中的秒数(持续时间)。

import subprocess

def get_duration(file):
    """Get the duration of a video using ffprobe."""
    cmd = ['ffprobe', '-i', file, '-show_entries', 'format=duration',
           '-v', 'quiet', '-of', 'csv="p=0"']
    output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
    output = float(output)
    return round(output)

然后调用这个函数:

video_length_in_seconds = get_duration('/path/to/your/file') # mp4, avi, etc

这将为您提供总秒数,四舍五入到最接近的整秒。因此,如果您的视频为 30.6 秒,这将返回 31

FFMpeg 命令ffprobe -i video_file_here -show_entries format=duration -v quiet -of csv="p=0" 将为您获取视频时长,不应下载整个视频。

【讨论】:

    【解决方案2】:
    import cv2
    data = cv2.VideoCapture('https://v.buddyku.id/ugc/m3YXvl-61837b3d8a0706e1ee0ab139.mp4')
    frames = data.get(cv2.CAP_PROP_FRAME_COUNT)
    fps = int(data.get(cv2.CAP_PROP_FPS))
    seconds = int(frames / fps)
    print("duration in seconds:", seconds)
    

    【讨论】:

      【解决方案3】:

      如果您有视频本身的直接链接,例如http://www.dl.com/xxx.mp4,您可以直接使用ffprobe 获取该视频的时长,方法是:

      ffprobe -i some_video_direct_link -show_entries format=duration -v quiet -of csv="p=0"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-22
        • 1970-01-01
        • 2019-07-17
        • 1970-01-01
        • 2013-06-18
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        相关资源
        最近更新 更多