pythonffmpegffmpeg-python

ffmpeg-python: Test if video clip has audio


Im using ffmpeg-python to do some video transformations.

If I have the following:

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()
)

When I run it, I get the following error:

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.

The above works if the video has audio


Solution

  • You can test if audio stream exists using ffmpeg.probe

    Example:

    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!')