python - 如何在不下载完整视频的情况下获取在线视频的时长?

标签 python video ffmpeg ffprobe

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

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 标签。我们可以做些什么来获得它的持续时间吗?

最佳答案

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

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

关于python - 如何在不下载完整视频的情况下获取在线视频的时长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26974921/

相关文章:

python - 如何将函数的返回值放入 QLineEdit 中?

javascript - 为什么 html video 标签无法加载我的视频?

ios - Apple 针对内联视频播放器的 iOS 政策是什么?

ffmpeg - 显示字幕列表,获取每个字幕轨道的 id 和名称

python - Hadoop:在 Ubuntu 12.04 中通过 NameNode 格式化 HDFS 文件系统

python - 找到最后一场比赛时得到空列表

php - Laravel 无法加载 FFProbe?

ffmpeg 连接导致第二个输入文件的输出错误/乱码

python - 在 Python 中强制单元测试用例出错

visual-c++ - 微软媒体基金会网络摄像头界面