gpt4 book ai didi

Python - 使用ffprobe获取视频的持续时间并将视频分成几部分

转载 作者:行者123 更新时间:2023-12-04 23:24:41 25 4
gpt4 key购买 nike

我正在尝试用 ffmpeg 分割视频,实现 python 脚本。

我收到此错误:

Command '['ffprobe', '-i', 'path_to_my_video/MVI_0731.MP4', '-hide_banner']' returned non-zero exit status 1.

这是我用来分割视频的代码:
for video_file in video_files:
try:
# Call "ffprobe" to get duration of input video.
ffprobe_process = subprocess.run(args=["ffprobe", "-i", video_file, "-hide_banner"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
shell=True)

# "ffprobe" writes to stderr instead of stdout!
duration_string = extract_duration_from_ffprobe_output(ffprobe_process.stderr)
duration_in_seconds = duration_string_to_seconds(duration_string)

# Make start_stop_list
start_stop_list = read_start_stop_list(start_stop_lists[nbr_video])
nbr_video += 1
total_pieces = int(len(start_stop_list))

这是导致问题的行:
ffprobe_process = subprocess.run(args=["ffprobe", "-i", video_file, "-hide_banner"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
shell=True)

当我将其更改为这行代码时:
ffprobe_process = subprocess.run(args=['ffprobe', '-i', video_file, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")])

它有效,我的意思是,脚本在该行之后,但随后在下一行引发以下错误:
470.240000
expected string or bytes-like object

470.240000 是我视频的正确持续时间。因此,我更改它的新行效果更好,但仍然无法以某种方式使用我的代码。

任何人都知道我该如何解决这个问题?

最佳答案

我找到了解决方案。

这是有效的代码:

for video_file in video_files:
try:
# Call "ffprobe" to get duration of input video.
ffprobe_process = subprocess.run(args=["ffprobe", "-i", video_file, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")])

# "ffprobe" writes to stderr instead of stdout!
#duration_string = extract_duration_from_ffprobe_output(ffprobe_process.stderr)
duration_string = str(ffprobe_process)
#duration_in_seconds = duration_string_to_seconds(duration_string)
duration_in_seconds = duration_string

# Make start_stop_list
start_stop_list = read_start_stop_list(start_stop_lists[nbr_video])
nbr_video += 1
total_pieces = int(len(start_stop_list))
#pdb.set_trace()

关于Python - 使用ffprobe获取视频的持续时间并将视频分成几部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52936411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com