import sys import os import json import subprocess as SP def get_streams(path): proc = SP.Popen( [ "ffprobe", "-probesize", str(0x7FFFFFFF), "-analyzeduration", str(0x7FFFFFFF), "-v", "fatal", "-i", path, "-show_streams", "-show_format", "-print_format", "json", ], stdout=SP.PIPE, stdin=SP.DEVNULL, bufsize=0, ) data = json.load(proc.stdout) ret = proc.wait() if ret != 0: return [], {} return data["streams"], data["format"] types = { "mpeg2video": "m2v", "ac3": "ac3", "dvd_subtitle": "sup", } def demux(path): folder = os.path.dirname(path) basename = os.path.splitext(os.path.basename(path))[0] streams, fmt = get_streams(path) cmd = [ "ffmpeg", "-y", # "-fflags","+genpts+igndts", "-probesize", str(0x7FFFFFFF), "-analyzeduration", str(0x7FFFFFFF), "-i", path, "-strict", "-2", "-vcodec", "copy", "-acodec", "copy", "-scodec", "copy", ] need_ffmpeg = False for stream in streams: codec = stream["codec_name"] ext = types.get(codec, codec) idx = stream["index"] hex_id = stream["id"] codec_name = stream["codec_long_name"] outfile = os.path.join(folder, f"{basename}_{idx}_{hex_id}") if codec=="dvd_nav_packet": continue print(idx, hex_id, codec_name, codec) if codec == "dvd_subtitle": SP.check_call([ "mencoder",path,"-vobsuboutindex",str(idx),"-vobsubout", outfile,"-nosound","-ovc", "copy", "-o",os.devnull ]) continue cmd += ["-map", f"0:#{hex_id}", "-strict", "-2", outfile + f".{ext}"] need_ffmpeg = True if need_ffmpeg: SP.check_call(cmd) if __name__=="__main__": demux(sys.argv[1])