5

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?

Alex Oat
  • 63
  • 1
  • 2
  • 8

2 Answers2

7

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.

Gyan
  • 85,394
  • 9
  • 169
  • 201
1

This is how I did it. Turn off the banner, and set the -loglevel to error only, and pipe the results to grep for "pix_fmt":

ffprobe -hide_banner -loglevel error -show_entries \
   stream "input.png" | grep "pix_fmt"

Returns pix_fmt=rgba

For a JPEG, or other file missing the alpha transparency channel, expect an output of: pix_fmt=yuvj444p

Benji
  • 310
  • 3
  • 12