如何在 Python 中获取视频的时长?
- 2025-03-12 08:54:00
- admin 原创
- 29
问题描述:
我需要在 Python 中获取视频的时长。我需要获取的视频格式是MP4、Flash 视频、AVI和 MOV... 我有一个共享托管解决方案,因此不支持FFmpeg。
你有什么建议?
谢谢!
解决方案 1:
您可以使用外部命令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)
解决方案 2:
(2020 年答案)
解决方案:
opencv
0.0065 秒✔ffprobe
0.0998 秒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)
解决方案 3:
据此处报道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 )
解决方案 4:
找到这个新的 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 的输出。
解决方案 5:
为了让事情变得简单一点,下面的代码将输出转换为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 \n# -show_format -show_streams \n# 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
解决方案 6:
使用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
包还可以轻松创建、编辑视频并应用过滤器。
解决方案 7:
注意:需要ffprobe
安装并可用PATH
。
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)
解决方案 8:
我想出的一个函数。这基本上只使用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
解决方案 9:
上述 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
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
解决方案 10:
据此处报道
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()
干杯!:)
解决方案 11:
参考@Nikolay Gogol使用 opencv-python (cv2)的回答:
他的方法对我来说不起作用(Python 3.8.10,opencv-python==4.5.5.64),并且评论说在这种情况下不能使用 opencv,这也不是事实。
CAP_PROP_POS_MSEC为您提供 VideoCapture 当前帧的毫秒数,而不是视频的总毫秒数,因此当仅加载视频时,这显然是 0。
但我们实际上可以通过帧率和总帧数来计算视频的总毫秒数:
import cv2
video = cv2.VideoCapture("video.mp4")
# the frame rate or frames per second
frame_rate = video.get(cv2.CAP_PROP_FPS)
# the total number of frames
total_num_frames = video.get(cv2.CAP_PROP_FRAME_COUNT)
# the duration in seconds
duration = total_num_frames / frame_rate
解决方案 12:
打开 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"))
解决方案 13:
这是我今天在产品中使用的东西,使用 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
"This epic classic car collection centres on co.webm"
:param video_filepath: str, path to video file
:param method: str, method cv2 or default ffprobe
"""
if method == 'cv2': # Use opencv-python
video = cv2.VideoCapture(video_filepath)
fps = video.get(cv2.CAP_PROP_FPS)
frame_count = video.get(cv2.CAP_PROP_FRAME_COUNT)
duration_seconds = frame_count / fps if fps else 0
else: # ffprobe
result = subprocess.check_output(
f'ffprobe -v quiet -show_streams -select_streams v:0 -of json "{video_filepath}"', shell=True).decode()
fields = json.loads(result)['streams'][0]
duration_seconds = fields['tags'].get('DURATION')
fps = eval(fields.get('r_frame_rate'))
return duration_seconds, fps
ffprobe
无法工作flv
,我无法让任何东西工作webm
。除此之外,它工作得很好,今天正在生产中使用。
解决方案 14:
对于喜欢使用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
解决方案 15:
在函数中使用 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