๐Ÿ python public

Untitled Paste

Guest 17h ago 15 views Code Paste
Raw
๐Ÿ python
1
import sys
2
import os
3
import json
4
import subprocess
5
 
6
 
7
input_file = sys.argv[1]
8
output_file = os.path.splitext(input_file)[0] + ".mp4"
9
 
10
 
11
ffprobe = subprocess.run(
12
    [
13
        "ffprobe",
14
        "-v",
15
        "quiet",
16
        "-print_format",
17
        "json",
18
        "-show_streams",
19
        input_file,
20
    ],
21
    capture_output=True,
22
    text=True,
23
    check=True,
24
)
25
 
26
streams = json.loads(ffprobe.stdout)["streams"]
27
 
28
 
29
video_tag = None
30
subtitles = []
31
 
32
 
33
for s in streams:
34
 
35
    codec = s.get("codec_name", "").lower()
36
 
37
    if s.get("codec_type") == "video":
38
 
39
        dovi = any(
40
            x.get("side_data_type") == "DOVI configuration record"
41
            for x in s.get("side_data_list", [])
42
        )
43
 
44
        if codec in ("hevc", "h265"):
45
            video_tag = "dvh1" if dovi else "hvc1"
46
 
47
        elif codec in ("h264", "avc"):
48
            video_tag = "dva1" if dovi else "avc1"
49
 
50
 
51
    elif (
52
        s.get("codec_type") == "subtitle"
53
        and codec == "subrip"
54
    ):
55
        subtitles.append(s["index"])
56
 
57
 
58
cmd = [
59
    "ffmpeg",
60
    "-i",
61
    input_file,
62
    "-map",
63
    "0:v",
64
    "-map",
65
    "0:a",
66
]
67
 
68
 
69
for s in subtitles:
70
    cmd += ["-map", f"0:{s}"]
71
 
72
 
73
cmd += [
74
    "-c:v",
75
    "copy",
76
    "-c:a",
77
    "copy",
78
]
79
 
80
 
81
if subtitles:
82
    cmd += [
83
        "-c:s",
84
        "mov_text",
85
    ]
86
else:
87
    cmd += [
88
        "-sn",
89
    ]
90
 
91
 
92
cmd += [
93
    "-dn",
94
    "-strict",
95
    "unofficial",
96
    "-movflags",
97
    "+faststart",
98
    "-tag:v",
99
    video_tag,
100
    "-brand",
101
    "mp42",
102
    output_file,
103
]
104
 
105
 
106
print(" ".join(cmd))
107
 
108
subprocess.run(cmd, check=True)

Paste Info

ID
5l4Be7
Type
Code Paste
Size
1.5 KB
Lines
108
Views
15
Created
17h ago

Share This Paste