尝试组合多个拆分的音频剪辑时出现 ffmpeg 错误
ffmpeg error when trying to combine multiple splitted audio clips
我正在尝试使用 python 中的 ffmpeg
和 subprocess
从给定的音频中拆分几个剪辑并将它们组合起来。
该命令在我的终端中有效:
ffmpeg -i input.mp3 \
-af "aselect='between(t, 4, 6)+between(t, 70, 80)', asetpts=N/SR/TB" out.wav
然而,当我尝试使用 subprocess
复制相同内容时,出现以下错误:
[NULL @ 0x7fc5f600a600] Unable to find a suitable output format for 'between(t, 4, 6)+between(t, 70, 80)'
between(t, 4, 6)+between(t, 70, 80): Invalid argument
CompletedProcess(args=['ffmpeg', '-i', 'input.mp3', '-af', '"aselect=\'', 'between(t, 4, 6)+between(t, 70, 80)', "', ", 'asetpts = N/SR/TB"', 'out.wav'], returncode=1)
代码:
import subprocess
input_url = 'input.mp3'
output_file = 'out.wav'
clips = [[4, 6], [70,80]] #start, end
clip_parts = []
for clip_stamps in clips:
start, end = clip_stamps
temp_part = "between" + "(" + "t, " + str(start) + ", " + str(end) + ")"
if clip_parts is None:
clip_parts = [temp_part]
else:
clip_parts.append(temp_part)
subprocess.run([
"ffmpeg",
"-i",
input_url,
"-af",
"\"aselect='",
'+'.join(clip_parts),
"', ",
"asetpts = N/SR/TB\"",
output_file
])
关于subprocess.run
有两个问题:
subprocess.run
自动在带有特殊字符的参数周围添加 "
个字符。
比如"\"aselect='
变成了"\"aselect='"
,所以多了"\"
- 列表中的每个元素都是一个命令行参数。
"aselect='between(t, 4, 6)+between(t, 70, 80)', asetpts=N/SR/TB"
是单个参数,不应拆分为 ["\"aselect='"
、'+'.join(clip_parts)
、"', "
、"asetpts = N/SR/TB\""
].
为了检查实际的命令行,您可以添加-report
参数,并检查FFmpeg创建的日志文件。
您的命令(-report
)被翻译成:
ffmpeg -i input.mp3 -af "\"aselect='" "between(t, 4, 6)+between(t, 70, 80)" "', " "asetpts = N/SR/TB\"" out.wav -report
如您所见,有额外的 "
个字符。
更正后的代码:
import subprocess
input_url = 'input.mp3'
output_file = 'out.wav'
clips = [[4, 6], [70,80]] #start, end
clip_parts = []
for clip_stamps in clips:
start, end = clip_stamps
temp_part = "between" + "(" + "t, " + str(start) + ", " + str(end) + ")"
if clip_parts is None:
clip_parts = [temp_part]
else:
clip_parts.append(temp_part)
subprocess.run([
"ffmpeg",
"-i",
input_url,
"-af",
"aselect='" + '+'.join(clip_parts) + "', " + "asetpts=N/SR/TB",
output_file
])
我正在尝试使用 python 中的 ffmpeg
和 subprocess
从给定的音频中拆分几个剪辑并将它们组合起来。
该命令在我的终端中有效:
ffmpeg -i input.mp3 \
-af "aselect='between(t, 4, 6)+between(t, 70, 80)', asetpts=N/SR/TB" out.wav
然而,当我尝试使用 subprocess
复制相同内容时,出现以下错误:
[NULL @ 0x7fc5f600a600] Unable to find a suitable output format for 'between(t, 4, 6)+between(t, 70, 80)'
between(t, 4, 6)+between(t, 70, 80): Invalid argument
CompletedProcess(args=['ffmpeg', '-i', 'input.mp3', '-af', '"aselect=\'', 'between(t, 4, 6)+between(t, 70, 80)', "', ", 'asetpts = N/SR/TB"', 'out.wav'], returncode=1)
代码:
import subprocess
input_url = 'input.mp3'
output_file = 'out.wav'
clips = [[4, 6], [70,80]] #start, end
clip_parts = []
for clip_stamps in clips:
start, end = clip_stamps
temp_part = "between" + "(" + "t, " + str(start) + ", " + str(end) + ")"
if clip_parts is None:
clip_parts = [temp_part]
else:
clip_parts.append(temp_part)
subprocess.run([
"ffmpeg",
"-i",
input_url,
"-af",
"\"aselect='",
'+'.join(clip_parts),
"', ",
"asetpts = N/SR/TB\"",
output_file
])
关于subprocess.run
有两个问题:
subprocess.run
自动在带有特殊字符的参数周围添加"
个字符。
比如"\"aselect='
变成了"\"aselect='"
,所以多了"\"
- 列表中的每个元素都是一个命令行参数。
"aselect='between(t, 4, 6)+between(t, 70, 80)', asetpts=N/SR/TB"
是单个参数,不应拆分为 ["\"aselect='"
、'+'.join(clip_parts)
、"', "
、"asetpts = N/SR/TB\""
].
为了检查实际的命令行,您可以添加-report
参数,并检查FFmpeg创建的日志文件。
您的命令(-report
)被翻译成:
ffmpeg -i input.mp3 -af "\"aselect='" "between(t, 4, 6)+between(t, 70, 80)" "', " "asetpts = N/SR/TB\"" out.wav -report
如您所见,有额外的 "
个字符。
更正后的代码:
import subprocess
input_url = 'input.mp3'
output_file = 'out.wav'
clips = [[4, 6], [70,80]] #start, end
clip_parts = []
for clip_stamps in clips:
start, end = clip_stamps
temp_part = "between" + "(" + "t, " + str(start) + ", " + str(end) + ")"
if clip_parts is None:
clip_parts = [temp_part]
else:
clip_parts.append(temp_part)
subprocess.run([
"ffmpeg",
"-i",
input_url,
"-af",
"aselect='" + '+'.join(clip_parts) + "', " + "asetpts=N/SR/TB",
output_file
])