ffprobe-python
ffprobe-python copied to clipboard
Use ffprobe's -print_format json to parse, and also let ffprobe handle the "file" instead
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.
@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}'
)
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
If anyone's looking for a longer-term solution, I've:
- implemented the above
ffprobe -print_format jsoncommand-line - enabled support for remote media streams (as the
ffprobeprogram 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.)