Sin*_*ion 42
您可能需要调用外部程序.ffprobe
可以为您提供这些信息:
import subprocess
def getLength(filename):
result = subprocess.Popen(["ffprobe", filename],
stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
return [x for x in result.stdout.readlines() if "Duration" in x]
Run Code Online (Sandbox Code Playgroud)
小智 26
(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
Run Code Online (Sandbox Code Playgroud)
用法: 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
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
mob*_*cdi 18
正如此处报道的那样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 )
Run Code Online (Sandbox Code Playgroud)
And*_*510 16
为了使事情更容易,以下代码将输出放到JSON.
您可以使用它来使用它probe(filename)
,或通过使用获得持续时间duration(filename)
:
json_info = probe(filename)
secondes_dot_ = duration(filename) # float number of seconds
Run Code Online (Sandbox Code Playgroud)
它适用于Ubuntu 14.04
当然ffprobe
安装的地方.代码没有针对速度或美观目的进行优化,但它适用于我的机器,希望它有所帮助.
#
# 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
Run Code Online (Sandbox Code Playgroud)
avi*_*zil 13
使用现代方法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']}")
Run Code Online (Sandbox Code Playgroud)
使用ffmpeg-python
包还可以轻松创建、编辑滤镜并将其应用于视频。
找到这个新的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
Run Code Online (Sandbox Code Playgroud)
上面的代码是针对有效的mp4文件进行测试的,但是你应该做更多的检查,因为它严重依赖于MediaInfo的输出.
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)
Run Code Online (Sandbox Code Playgroud)
我想出的一个功能。这基本上只使用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 ) )
Run Code Online (Sandbox Code Playgroud)
输出持续时间如下:
7.338000
Run Code Online (Sandbox Code Playgroud)
据报道 https://www.reddit.com/r/moviepy/comments/2bsnrq/is_it_possible_to_get_the_length_of_a_video/
你可以使用 moviepy 模块
Run Code Online (Sandbox Code Playgroud)from moviepy.editor import VideoFileClip clip = VideoFileClip("my_video.mp4") print( clip.duration )
如果您尝试获取文件夹中许多视频的持续时间,则会崩溃并给出错误:AttributeError:“AudioFileClip”对象没有属性“reader”
因此,为了避免这种情况,您需要添加
clip.close()
Run Code Online (Sandbox Code Playgroud)
基于此: 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()
Run Code Online (Sandbox Code Playgroud)
干杯! :)
归档时间: |
|
查看次数: |
41620 次 |
最近记录: |