Answers.Body: <p>ffprobe can output json, making it a lot simpler:</p>
  <pre><code>import json
  from subprocess import run, PIPE, DEVNULL
  import sys
  
  def ffprobe_json(filename):
      proc = run(
          [ &quot;ffprobe&quot;, &quot;-hide_banner&quot;, &quot;-print_format&quot;, &quot;json&quot;, &quot;-show_format&quot;, &quot;-show_streams&quot;, filename ],
          stdin=DEVNULL, stdout=PIPE, stderr=PIPE
      )
      proc.check_returncode()
      return json.loads(proc.stdout.decode(sys.getdefaultencoding()))
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-17T04:02:56.997Z
Answers.LastActivityDate: 2021-08-17T04:02:56.997Z
Answers.UserDisplayName: stribika
Body: <p>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.</p>
  <p>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:</p>
  <ol>
  <li>programmatically extract parameters of the source video (I use ffprobe)</li>
  <li>run my editing pipeline, one of which is my ffmpeg-plugin, then</li>
  <li>re-compress the video using the parameters extracted in step 1.</li>
  </ol>
  <p>I've managed to automate big part of steps 1 &amp; 3 with python, and I have a series of calls to extract elements of each video, my code  looks like this:</p>
  <pre><code>color_string = 'ffprobe -v error -select_streams v:0 -show_entries stream=' \
                     'color_space,color_primaries,color_range -of default=nw=1:nk=1 &quot;' + filename + '&quot;'
  # 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]
  </code></pre>
  <p>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.</p>
  <p><strong>My question is</strong>, 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.</p>
  <p>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.</p>
  <p><em>I originally posted this question on superuser site, and was advised by</em> @Tetsujin <em>to migrate my question to this site, since it is specifically video related.</em></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-15T05:22:26.897Z
Comments.Text: 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.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-16T07:30:07.430Z
Comments.Text: @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
Comments.UserDisplayName: QRrabbit
Tags: <ffmpeg><codec><compression><ffprobe>
Title: ffmpeg and ffprobe existing library or an add-on, pros/cons of building one myself
UserDisplayName: QRrabbit
ThreadText: # ffmpeg and ffprobe existing library or an add-on, pros/cons of building one myself
  
  (Asked by: username_0 on 2021-08-14)
  
  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.*
  
  **Tags:** ffmpeg, codec, compression, ffprobe
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_0**: @username_1 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  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-08-17. Score: 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()))
  
  ```
  
  
  ---
  
  