ffmpeg-python:测试视频剪辑是否有音频

Pau*_*aul 3 python ffmpeg ffmpeg-python

我使用 ffmpeg-python 进行一些视频转换。

如果我有以下情况:

infiles = []
infile = ffmpeg.input("/tmp/xxx.mp4")
infiles.append(
    infile['v']
    .filter('scale', size='1920x1080', force_original_aspect_ratio='decrease')
    .filter('pad', '1920', '1080', '(ow-iw)/2', '(oh-ih)/2')
)
infiles.append(infile['a'])

(
    ffmpeg
    .concat(
        *infiles, v=1, a=1, unsafe=True)
    .output(out_tmp_file)
    .run()
)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我收到以下错误:

Stream specifier ':a' in filtergraph description [0:v]scale=force_original_aspect_ratio=decrease:size=1920x1080[s0];[s0]pad=1920:1080:(ow-iw)/2:(oh-ih)/2[s1];[s1][0:a]concat=a=1:n=1:unsafe=True:v=1[s2] matches no streams.
Run Code Online (Sandbox Code Playgroud)

如果视频有音频,以上方法有效

Rot*_*tem 7

您可以使用测试音频流是否存在ffmpeg.probe

例子:

import ffmpeg

# Run ffprobe on the specified file and return a JSON representation of the output.
# https://kkroening.github.io/ffmpeg-python/
# Use select_streams='a' for getting only audio streams information
p = ffmpeg.probe('in.mp4', select_streams='a');

# If p['streams'] is not empty, clip has an audio stream
if p['streams']:
    print('Video clip has audio!')
Run Code Online (Sandbox Code Playgroud)