Detecting AV1-encoded videos with Python
In my previous post, I wrote about how I’ve saved some AV1-encoded videos that I can’t play on my iPhone. Eventually, I’ll upgrade to a new iPhone which supports AV1, but in the meantime, I want to convert all of those videos to an older codec. The problem is finding all the affected videos – I don’t want to wait until I want to watch a video before discovering it won’t play.
I already use pytest to run some checks on my media library: are all the files in the right place, is the metadata in the correct format, do I have any misspelt tags, and so on. I wanted to write a new test that would check for AV1-encoded videos, so I could find and convert them in bulk.
In this post, I’ll show you two ways to check if a video is encoded using AV1, and a test I wrote to find any such videos inside a given folder.
Getting the video codec with ffprobe
In my last post, I wrote an ffprobe command that prints some information about a video, including the codec. (ffprobe is a companion tool to the popular video converter FFmpeg.)
$ ffprobe -v error -select_streams v:0 \ -show_entries stream=codec_name,profile,level,bits_per_raw_sample \ -of default=noprint_wrappers=1 "input.mp4" codec_name=av1 profile=Main level=8 bits_per_raw_sample=N/A
I can tweak this command to print just the codec name:
$ ffprobe -v error -select_streams v:0 \ -show_entries stream=codec_name \ -of csv=print_section=0 "input.mp4" av1
To run this command from Python, I call the check_output function from the subprocess module. This checks the command completes successfully, then returns the output as a string. I can check if the output is the string av1 :
... continue reading