0

I work with video creation, animation, or fix video elements for several broadcasters in my area. My pipeline includes ffmpeg suite of libraries, and I have written one plugin specifically for ffmpeg to take care of one of the elements of my video editing.

One thing I found myself struggling with is to preserve the format of the original video, and since this is one of the requirements to return edited video in the same format as the source, my biggest challenge is to:

  1. programmatically extract parameters of the source video (I use ffprobe)
  2. run my editing pipeline, one of which is my ffmpeg-plugin, then
  3. re-compress the video using the parameters extracted in step 1.

I've managed to automate big part of steps 1 & 3 with python, and I have a series of calls to extract elements of each video, my code looks like this:

color_string = 'ffprobe -v error -select_streams v:0 -show_entries stream=' \
                   'color_space,color_primaries,color_range -of default=nw=1:nk=1 "' + filename + '"'
# following running of subprocess:
color = subprocess.run(color_string, shell=True, capture_output=True)
# then parse:
color_array = str(color.stdout).split('\\n')
    color_primaries = color_array[1]
    color_range = color_array[2]

The problem is, every broadcaster uses their own coding standards, and while I don't care about audio at all, meaning, I can -c:a copy, all the video parameters are different from broadcaster to broadcaster. progressive/interlaced, DVCPRO which sometimes includes screen sizes of 1280x1080 (which drives me insane), prores with all its profiles and even deeper with -qscale and -bits_per_mb options, some codecs have vtag and some don't, and there are about 100+ others. And my list of ffprobe queries keeps growing, I have about 1k lines of extraction and then putting it all together for ffmpeg to run.

My question is, is there a library to extract video parameters from a source, then once the processing is done, use that library to populate all the arguments for ffmpeg? As it turned out, I'm building one for myself, but I have a feeling I'm wasting my time.

I know that many adobe applications use this logic, and provide an option to preserve source on re-encoding a video, but I am building one with python, and I believe this would be a great utility application as an open source, since there are simply too many options to consider for one person.

I originally posted this question on superuser site, and was advised by @Tetsujin to migrate my question to this site, since it is specifically video related.

2
  • Which parameters do you wish to extract? Certain ones are universal, like resolution or bit depth and will be printed by ffprobe whereas others are codec-specific and are only relevant for the decoder.
    – Gyan
    Aug 15, 2021 at 5:22
  • @Gyan thank you so much for your response. Well, the resolution is printed by the ffprobe, that's correct, and then I use that information to encode back. That is especially important if I do DVCPRO formats with weird ratio. But then, things like interlaced vs. progressive, top or bottom first, etc. For ProRes, this is profile + vtag, + sometimes qscale or -bits_per_mb, which I started using to match the filesize, more or less. For x264, a whole bunch of other parameters to keep track of. + timecodes with or w/o dropframe, -color_primaries, -color_trc, & -colorspace, max & minrate
    – QRrabbit
    Aug 16, 2021 at 7:30

1 Answer 1

1

ffprobe can output json, making it a lot simpler:

import json
from subprocess import run, PIPE, DEVNULL
import sys

def ffprobe_json(filename):
    proc = run(
        [ "ffprobe", "-hide_banner", "-print_format", "json", "-show_format", "-show_streams", filename ],
        stdin=DEVNULL, stdout=PIPE, stderr=PIPE
    )
    proc.check_returncode()
    return json.loads(proc.stdout.decode(sys.getdefaultencoding()))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.