首页 > 解决方案 > 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()
)

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

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.

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

标签: python-3.xffmpeg

解决方案


您可以使用以下方法测试音频流是否存在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!')

推荐阅读