Search code examples
videoffmpegalphaffprobetranscoding

A good way to detect alpha channel in a video using ffmpeg/ffprobe


In my Django application, uploaded video files are transcoded into some particular format using ffmpeg.

I now need a way to reliably detect whether uploaded videos have alpha channel or not. I normally use ffprobe for getting video metadata. Could you point me in the right direction?


Solution

  • You can do this in two steps, using ffprobe.

    #1

    ffprobe -v 0 -select_streams v:0 -show_entries stream=pix_fmt -of compact=p=0:nk=1 "$FILE"
    

    This will print the pixel format of the video stream. All pixel formats with alpha component have a in their name but not all formats with a have alpha. So, run..

    #2

    ffprobe -v 0 -show_entries pixel_format=name:flags=alpha -of compact=p=0  | grep "$PIX_FMT|" | grep -oP "(?<=alpha=)\d"
    

    where $PIX_FMT is the readout printed in step 1. The result will be 1 or 0.