| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import subprocess, logging
- class Probe:
- encoding = 'utf-8'
- ffprobe_exec = 'ffprobe'
-
- def __init__(self, ffprobe_exec='ffprobe', logLevel=logging.WARN):
- self.logger = logging.getLogger(__name__)
- self.logger.setLevel(logLevel)
- if ffprobe_exec:
- self.ffprobe_exec = ffprobe_exec
- def probe(self, path):
- self.logger.debug(f"Probing: {path}")
- streams = []
- # Sanity
- if not path.exists():
- self.logger.warn(f"No such file: {path}")
- return streams
- # ffprobe
- args = [ self.ffprobe_exec, '-hide_banner', '-show_streams', path ]
- self.logger.debug(f"ffprobe command: {args}")
- probe = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- self.logger.debug(f"ffprobe result: {probe}")
- data = probe.stdout.decode(self.encoding)
- if probe.returncode != 0:
- self.logger.error(f"ffprobe error ({probe.returncode}): {probe.stderr.decode(self.encoding)}")
- self.logger.debug(f"ffprobe stdout: {data}")
- return steams
- # Parse streams
- prefix = None
- stream = None
- for line in data.splitlines():
- if line == "[STREAM]":
- stream = {}
- elif line == "[/STREAM]":
- streams.append(stream)
- steam = None
- elif line == "[SIDE_DATA]":
- prefix = 'SIDE_DATA'
- elif line == "[/SIDE_DATA]":
- prefix = None
- elif stream is not None:
- try:
- (key, val) = line.split('=', 1)
- if prefix is not None:
- key = f"{prefix}:{key}"
- except ValueError:
- self.logger.error(f"Skipping unexpected stream data ({path}): {line}")
- continue
- stream[key] = val
- # Return
- return streams
|