Choggbuster/vob_demux.py

63 lines
1.7 KiB
Python
Raw Normal View History

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",
"-strict","-2",
"-fflags","+genpts",
"-probesize", str(0x7fffffff),
"-analyzeduration", str(0x7fffffff),
"-i",path,
"-scodec","copy",
"-vcodec","copy",
"-acodec","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_subtitle":
SP.check_call([
"mencoder",path,"-vobsuboutindex",str(idx),"-vobsubout", outfile,"-nosound","-ovc", "copy", "-o",os.devnull
])
continue
print(idx,hex_id,codec_name,codec)
cmd+=["-map",f"0:#{hex_id}",outfile+f".{ext}"]
need_ffmpeg=True
if need_ffmpeg:
SP.check_call(cmd)