Last active
November 27, 2023 17:57
-
-
Save misodengaku/902e022a3a68b994bf1bd880d1c4d6a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import sys | |
import os | |
import json | |
from datetime import datetime, timedelta | |
from pprint import pprint | |
output = subprocess.run(["lsdvd", "-x", "-Oy", sys.argv[1]], capture_output=True, text=True).stdout | |
lsdvd = {} | |
exec(output) | |
json.dump(lsdvd, open('lsdvd.json', 'w')) | |
tmpfiles = ["lsdvd.json"] | |
subprocess.run(["sudo", "mount", "-o", "loop", sys.argv[1], "tmp"]) | |
l = [x for x in os.walk("tmp/VIDEO_TS")] | |
filelist = list(filter(lambda x: x.startswith("VTS") and x.endswith(".VOB") and not x.endswith("_0.VOB"), l[0][2])) | |
i = 1 | |
while True: | |
fl = list(filter(lambda x: x.startswith("VTS_%02d_" % i), filelist)) | |
if not len(fl): | |
break | |
print(fl) | |
vobs = ["tmp/VIDEO_TS/%s" % x for x in fl] | |
ifs = "concat:"+"|".join(vobs) | |
subprocess.run(["ffmpeg", "-y", "-i", ifs, "-c", "copy", "vts_%d.ts" % i]) | |
tmpfiles.append("vts_%d.ts" % i) | |
i += 1 | |
is_chapter_generated = False | |
last_vts = 0 | |
last_length = 0 | |
lsdvd["track"] = sorted(lsdvd["track"], key=lambda x: (x["vts"], x["cell"][0]["first_sector"])) | |
for track in lsdvd["track"]: | |
vobs = [] | |
current_time = datetime(year=2000, month=1, day=1, hour=0, minute=0, second=0, microsecond=0) | |
c = ["CHAPTER00=00:00:00.000", "CHAPTER00NAME=0"] | |
track_id = int(track["ix"]) | |
for chapter in track["chapter"]: | |
file_id = int(track["vts"]) | |
chapter_id = int(chapter["ix"]) | |
dvd_len = float(chapter["length"]) | |
current_time += timedelta(seconds=dvd_len) | |
c.append("CHAPTER%02d=%s" % (chapter_id, current_time.strftime("%H:%M:%S.%f"))) | |
c.append("CHAPTER%02dNAME=%d" % (chapter_id, chapter_id)) | |
with open("chapter.txt", "w") as f: | |
f.write('\n'.join(c)) | |
is_chapter_generated = True | |
vts = int(track["vts"]) | |
length = int(track["length"]) | |
if vts != last_vts: | |
subprocess.run(["ffmpeg", "-fflags", "+genpts", "-y", "-i", "vts_%d.ts" % file_id, "-t", str(track["length"]), "-c", "copy", "track_%d.mkv" % track_id]) | |
last_length = length | |
else: | |
subprocess.run(["ffmpeg", "-fflags", "+genpts", "-y", "-ss", str(last_length), "-i", "vts_%d.ts" % file_id, "-t", str(track["length"]), "-c", "copy", "track_%d.mkv" % track_id]) | |
last_length += length | |
last_vts = vts | |
tmpfiles.append("track_%d.mkv" % track_id) | |
subprocess.run(["mkvmerge", "-o", "%s_%d.mkv" % (lsdvd["title"], track_id), "--chapters", "chapter.txt", "track_%d.mkv" % track_id]) | |
if is_chapter_generated: | |
tmpfiles.append("chapter.txt") | |
subprocess.run(["sudo", "umount", "tmp"]) | |
for tmpfile in tmpfiles: | |
os.remove(tmpfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment