ffprobe-python icon indicating copy to clipboard operation
ffprobe-python copied to clipboard

Use ffprobe's -print_format json to parse, and also let ffprobe handle the "file" instead

Open ghost opened this issue 5 years ago • 3 comments

Just found this recently and I think it would be best to use -print_format json instead of parsing the normal output. I also noticed that it checks whether the file existed or not. This unfortunately prevents it from working with HTTP and FTP.

ghost avatar Apr 27 '20 09:04 ghost

@Mz49 I just ended up using this.

import json
import subprocess

def _ffprobe(file_path):
    command = (
        'ffprobe -v quiet -print_format json -show_format -show_streams '
        f'{file_path}'
    )
    result = subprocess.run(command.split(), capture_output=True)
    if result.returncode == 0 and file_path.exists():
        return json.loads(result.stdout)
    raise RuntimeError(
        f'FFProbe failed for {file_path}, output: {result.stderr}'
    )

richardARPANET avatar Dec 14 '20 21:12 richardARPANET

I'm doing something like this myself in another project and was about to suggest it:

cmd = "ffprobe -v quiet -print_format json -show_streams"
args = shlex.split(cmd)
args.append(info.FILENAME)
ffprobeOutput = subprocess.check_output(args).decode('utf-8')
ffprobeOutput = json.loads(ffprobeOutput)

If it simplifies parsing for you at all

bigntallmike avatar Mar 10 '21 20:03 bigntallmike

If anyone's looking for a longer-term solution, I've:

  • implemented the aboveffprobe -print_format json command-line
  • enabled support for remote media streams (as the ffprobe program already does)
  • made the checks for file-existence optional

plus a bunch of other bug-fixes, features, and improvements, in a fork: https://github.com/jboy/ffprobe3-python3

(Python3 only, sorry.)

jboy avatar Aug 13 '23 15:08 jboy