A good way to detect alpha channel in a video using ffmpeg/ffprobe [closed]
Asked Answered
M

2

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?

Maskanonge answered 2/9, 2021 at 11:31 Comment(1)
I’m voting to close this question because , as the ffmpeg tag states: Only questions about programmatic use of the FFmpeg libraries, API, or tools are on topic. Questions about interactive use of the command line tool should be asked on superuser.com or video.stackexchange.com. Please delete this.Jemappes
V
8

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.

Videogenic answered 2/9, 2021 at 12:11 Comment(0)
C
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

Conclave answered 6/9, 2022 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.