-2
ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 (filename) 

is how to retrieve the codec_name of the first video stream in a file via FFprobe.

Replace v:0 with a:0 to get the first audio stream, and it returns the codec_name of the first audio stream in the file.

And replace v:0 with s:0 to get the codec_name of the first subtitle stream in the file.

Awesome.

What isn't awesome, is having to call FFprobe three times for these operations and having to hit the file three times.

Is there a way to call FFProbe once, and have FFProbe return the codec_name of the first video, audio, and subtitle stream (instead of separate calls)?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Chase Westlye
  • 381
  • 2
  • 6
  • 20
  • Why is this tagged as a Python question? What you show here is obviously a command-line invocation of the `ffprobe` program. – Karl Knechtel Aug 26 '23 at 03:15
  • There is returning the streams as a JSON object, and parsing the JSON: ffprobe -loglevel error -show_streams -of json {video} I did not like this approach as you sort of had to know the stream number for each of the first stream types (video, audio, subtitle). I guess I could loop through the JSON and figure it out, but that would be a PITA, and I am hoping there is a more elegant way – Chase Westlye Aug 26 '23 at 03:15
  • @KarlKnechtel I'm running this in Python, and my original example was my existing spaghetti code: vcodec = sp.run(shlex.split(f'ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 {video}'), capture_output=True).stdout.decode('utf8').strip() After typing up the post and adding tags, I decided against posting the python example as it's not great code, and distracts from the main question, but forgot to remote the python flag – Chase Westlye Aug 26 '23 at 03:17
  • Fair enough, removed it for you. FWIW, my quick research implies that you're intended to take the `-show_streams -of` approach, although there are other formats possible besides JSON if they're more convenient for you, and you can select which streams to show information about. Please see the [documentation](https://ffmpeg.org/ffprobe.html#Main-options). Also FWIW, there exist Python bindings for ffmpeg functionality, although I can't vouch for any specific PyPI package. – Karl Knechtel Aug 26 '23 at 03:21

0 Answers0