Choggbuster/vob_demux.py

87 lines
2.1 KiB
Python
Raw Normal View History

import sys
import os
import json
import subprocess as SP
def get_streams(path):
2021-10-28 19:27:15 +00:00
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"]
2021-10-28 19:27:15 +00:00
types = {
"mpeg2video": "m2v",
"ac3": "ac3",
"dvd_subtitle": "sup",
}
2021-10-28 19:27:15 +00:00
def demux(path):
2021-10-28 19:27:15 +00:00
folder = os.path.dirname(path)
basename = os.path.splitext(os.path.basename(path))[0]
streams, fmt = get_streams(path)
cmd = [
"ffmpeg",
"-y",
2021-10-28 19:27:15 +00:00
# "-fflags","+genpts+igndts",
"-probesize",
str(0x7FFFFFFF),
"-analyzeduration",
str(0x7FFFFFFF),
"-i",
path,
"-strict",
"-2",
"-vcodec",
"copy",
"-acodec",
"copy",
"-scodec",
"copy",
]
2021-10-28 19:27:15 +00:00
need_ffmpeg = False
for stream in streams:
2021-10-28 19:27:15 +00:00
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
2021-10-28 19:27:15 +00:00
cmd += ["-map", f"0:#{hex_id}", "-strict", "-2", outfile + f".{ext}"]
need_ffmpeg = True
if need_ffmpeg:
2021-10-28 19:27:15 +00:00
SP.check_call(cmd)
if __name__=="__main__":
demux(sys.argv[1])