你可以使用外部命令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)
-show_streams
之后使用-print_format json
,然后将结果json.loads
成一个漂亮的字典,其中包含所有ffprobe信息。这也需要解析ffprobe打印出的额外信息(尚未找到隐藏它的选项)。 - dennmat-loglevel
不是 -log_level
(至少在 ffprobe 2.3.3 中是这样的) - Vonffprobe
Python 模块,可以通过以下代码获取文件的时长:from ffprobe import FFProbe; FFProbe('path/to/file').streams[0].duration
。 - kmonsoor(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)
fps = video.get(cv2.CAP_PROP_FPS)
duration = frame_count / fps
- pzelaskofps = video.get(cv2.CAP_PROP_FPS)
并通过 frame_count/fps
获取持续时间。 OpenCV方法比使用ffmpeg的子进程更快,但您需要在系统中安装许多东西才能使其正常工作。 对我来说,'pymediainfo'似乎快了近一倍。 - kostr22根据此处报道,您可以使用moviepy模块来获取视频的长度。
from moviepy.editor import VideoFileClip
clip = VideoFileClip("my_video.mp4")
print( clip.duration )
probe(filename)
来使用它,或者通过使用duration(filename)
来获取持续时间。json_info = probe(filename)
secondes_dot_ = duration(filename) # float number of seconds
ffprobe
的Ubuntu 14.04
上运行。该代码并非为速度或美观而优化,但它可以在我的机器上工作,希望它能有所帮助。#
# 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
out = out.decode('utf-8')
- Soorena发现这个新的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
以上代码已经测试过并且可以正常运行,但是你应该进行更多的检查,因为它在很大程度上依赖于MediaInfo的输出。
max([float(track.duration) for track in MediaInfo.parse('my_video_file.mov').tracks])
来检查所有轨道,因为第一条轨道的长度不一定与整个视频的长度相同。 - Nexarius使用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
包,您还可以轻松创建、编辑和应用视频过滤器。
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)
ffprobe
添加到了环境变量中吗?我知道你只是想强调Windows上缺少grep
的问题,但要使用那个确切的语法,就需要编辑环境变量或者在与ffprobe
相同的目录下运行。 - Lovethenakedgun如果你试图获取一个文件夹中多个视频的持续时间,可能会崩溃并显示以下错误: AttributeError: 'AudioFileClip'对象没有属性'reader'As reported here https://www.reddit.com/r/moviepy/comments/2bsnrq/is_it_possible_to_get_the_length_of_a_video/
you could use the moviepy module
from moviepy.editor import VideoFileClip clip = VideoFileClip("my_video.mp4") print( clip.duration )
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()
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
这位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
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++