Easily Detect Video ColorSpace with ffmpeg and Python on Ubuntu

When googling for “ffmpeg colorspace” I found endless results on how to convert colorspace, but few on how to simply detect and print out the colorspace for a video.

import pexpect, sysdef get_colorspace(path):
re_PS1 = 'bash-[.0-9]+[$#] $'
proc = pexpect.spawn('bash --norc')
if len(sys.argv) != 1:
proc.logfile_read = sys.stdout
proc.sendline("ffmpeg -i " + path)
#print(proc.before) # print ffmpeg output
if "709" in str(proc.before):
return "709"
elif "2020" in str(proc.before):
return "2020"
return "unknown"

Here, I fetch the text output from the ffmpeg call. I pass a video in with path.

I search the output for colorspaces I am interested in. If I find the 709 string, I return 709 for the 709 colorspace (common for 4K). If I find 2020 in the output string, I return 2020, for the 2020 colorspace (common for UHD video)

Useful for going through videos and doing some process to them according to colorspace.

Btw if you enjoy my tutorials, coding notes and other content, I always appreciate endorsements on my LinkedIn: https://www.linkedin.com/in/ashleytharp/

XR Developer at HookBang