如何在 Python 中获取视频的持续时间? [英] How to get the duration of a video in Python?

查看:75
本文介绍了如何在 Python 中获取视频的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to get the video duration in Python. The video formats that I need to get are MP4, Flash video, AVI, and MOV... I have a shared hosting solution, so I have no FFmpeg support.

推荐答案

可以使用外部命令 ffprobe 为此.具体来说,从 FFmpeg Wiki 运行 这个 bash 命令:

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)

这篇关于如何在 Python 中获取视频的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆