-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-media-files.py
351 lines (282 loc) · 10.4 KB
/
convert-media-files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
""" Script to process convert youtube-dl downloads to mp4,
before importing them into TubeArchivist.
Ensure you have a backup of your files before running this script!!!
This script will process all files in the specified directory and convert them to mp4.
it will also do the following:
- extract embedded thumbnails to jpg
- extract subtitles to vtt
- rename files to only contain the video id in the filename
- convert mkv files to mp4
- DELETE old mkv files
Required packages: yt-dlp, pillow, tqdm, ffmpeg_progress_yield
Usage:
python convert-media-files.py <input_dir>
input_dir: directory containing the media files to process
Example:
My input directory has the following layout below,
so the command would be: `python3 main.py /channels/Channel_name`
.
├── channels
│ ├── Channel_name
│ │ ├── Video - 20200701
│ │ │ ├── Video [abcdefRU123].mkv
│ │ │ └── Video [abcdefRU123].info.json
Todo:
- Handle vp9 with opus audio in webm files. This requires `-strict -2` flag in ffmpeg command.
- Add support for more video formats?
- Improve error, progress and logging messages.
"""
import json
import os
import re
import shutil
import sys
import subprocess
from ffmpeg_progress_yield import FfmpegProgress
from PIL import Image
from tqdm import tqdm
from yt_dlp.utils import ISO639Utils
EXT_MAP = {
"media": [".mkv", ".webm"],
"metadata": [".info.json"],
"thumb": [".jpg", ".png", ".webp"],
"subtitle": [".vtt"],
}
def get_streams(media_file):
"""get streams from media file"""
streams_raw = subprocess.run(
[
"ffprobe",
"-v",
"quiet",
"-print_format",
"json",
"-show_streams",
media_file,
],
capture_output=True,
check=True,
)
streams = json.loads(streams_raw.stdout.decode())
return streams
def dump_subtitle(idx, media_file, sub_path):
"""extract subtitle from media file"""
subprocess.run(
[
"ffmpeg",
"-nostats",
"-loglevel",
"error",
"-y",
"-i",
media_file,
"-map",
f"0:{idx}",
sub_path,
],
check=True,
)
def get_mp4_thumb_type(media_file):
"""detect filetype of embedded thumbnail"""
streams = get_streams(media_file)
for stream in streams["streams"]:
if stream["codec_name"] in ["png", "jpg"]:
return stream["codec_name"]
return False
def dump_mp4_thumb(media_file, thumb_type):
"""save cover to disk"""
_, ext = os.path.splitext(media_file)
new_path = f"{media_file.rstrip(ext)}.{thumb_type}"
subprocess.run(
[
"ffmpeg",
"-i",
media_file,
"-map",
"0:v",
"-map",
"-0:V",
"-c",
"copy",
new_path,
],
check=True,
)
return new_path
def get_mkv_thumb_stream(media_file):
"""get cover stream from mkv file"""
streams = get_streams(media_file)
attachments = [i for i in streams["streams"] if i["codec_type"] == "attachment"]
for idx, stream in enumerate(attachments):
tags = stream["tags"]
if "mimetype" in tags and tags["filename"].startswith("cover"):
_, ext = os.path.splitext(tags["filename"])
return idx, ext
return None, None
def dump_mpv_thumb(media_file, idx, thumb_type):
"""write cover to disk for mkv"""
_, media_ext = os.path.splitext(media_file)
new_path = f"{media_file.rstrip(media_ext)}{thumb_type}"
subprocess.run(
[
"ffmpeg",
"-v",
"quiet",
f"-dump_attachment:t:{idx}",
new_path,
"-i",
media_file,
],
check=False,
)
return new_path
def extract_thumbnail(video_dict):
"""Extracts thumbnail from video_dict. If no thumbnail is found, it will try to extract one."""
if video_dict["thumb"]:
return video_dict["thumb"]
media_file = video_dict["media"]
base_name, ext = os.path.splitext(media_file)
if os.path.exists(base_name + ".jpg"):
return base_name + ".jpg"
elif os.path.exists(base_name + ".png"):
return base_name + ".png"
new_path = None
if ext == ".mkv":
idx, thumb_type = get_mkv_thumb_stream(media_file)
if idx is not None:
new_path = dump_mpv_thumb(media_file, idx, thumb_type)
elif ext == ".mp4":
thumb_type = get_mp4_thumb_type(media_file)
if thumb_type:
new_path = dump_mp4_thumb(media_file, thumb_type)
if new_path:
return new_path
def extract_video_id(filename):
"""Extracts video ID from the filename which is enclosed in square brackets."""
base_name, _ = os.path.splitext(filename)
id_search = re.search(r"\[([a-zA-Z0-9_-]{11})\]", base_name)
if id_search:
youtube_id = id_search.group(1)
return youtube_id
return None
def categorize_files(directory):
"""Categorize files based on EXT_MAP and return the desired dictionaries."""
grouped_files = {}
for root, _, files in os.walk(directory):
for file in files:
full_path = os.path.join(root, file)
video_id = extract_video_id(file)
if video_id:
if video_id not in grouped_files:
grouped_files[video_id] = {
"media": False,
"metadata": False,
"thumb": False,
"subtitle": False,
"video_id": video_id,
}
for category, extensions in EXT_MAP.items():
if any(file.endswith(ext) for ext in extensions):
if category == "subtitle":
if grouped_files[video_id]["subtitle"] == False:
grouped_files[video_id]["subtitle"] = []
grouped_files[video_id]["subtitle"].append(full_path)
else:
grouped_files[video_id][category] = full_path
return list(grouped_files.values())
def main():
"""Process files specified in input_dir"""
if len(sys.argv) < 2:
print("No input directory specified")
return
input_dir = sys.argv[1]
if not os.path.exists(input_dir):
print(f"Directory {input_dir} does not exist")
return
grouped_videos = categorize_files(input_dir)
grouped_videos = [
v for v in grouped_videos if v["media"] and v["video_id"] is not None
]
unique_videos = list({v["video_id"]: v for v in grouped_videos}.values())
print(f"Found {len(unique_videos)} videos to process")
with tqdm(total=len(grouped_videos), position=0, leave=True) as pbar:
for idx, current_video in enumerate(grouped_videos):
pbar.set_description_str(
f"Processing {current_video['video_id']} ({idx+1}/{len(grouped_videos)})"
)
if not current_video["media"] or current_video["media"].endswith(".mp4"):
pbar.write(f"Skipping video {current_video['video_id']}")
continue
video_id = current_video["video_id"]
media_path = current_video["media"]
root_video_path = os.path.dirname(os.path.abspath(current_video["media"]))
# process thumbnail
thumb_path = extract_thumbnail(current_video)
if thumb_path:
pbar.set_postfix_str("Extracting thumbnail")
ext = os.path.splitext(thumb_path)[1]
new_thumb_path = os.path.join(root_video_path, f"[{video_id}].jpg")
if ext != ".jpg":
img_raw = Image.open(thumb_path)
img_raw.convert("RGB").save(new_thumb_path)
os.remove(thumb_path)
else:
os.rename(thumb_path, new_thumb_path)
# process subtitles
streams = get_streams(media_path)
for idx, stream in enumerate(streams["streams"]):
pbar.set_postfix_str("Processing subtitles")
if stream["codec_type"] == "subtitle":
lang = ISO639Utils.long2short(stream["tags"]["language"])
sub_path = f"[{video_id}].{lang}.vtt"
dump_subtitle(
idx, media_path, os.path.join(root_video_path, sub_path)
)
# convert to mp4
pbar.set_postfix_str("Converting video")
current_video_path = current_video["media"]
new_video_path = os.path.join(root_video_path, f"[{video_id}].mp4")
cmd = [
"ffmpeg",
"-y",
"-threads",
"0",
"-i",
current_video_path,
"-codec",
"copy",
new_video_path,
]
with tqdm(
total=100, position=1, leave=False, desc="Converting video"
) as pbar2:
ff = FfmpegProgress(cmd)
for progress in ff.run_command_with_progress():
pbar2.update(progress - pbar2.n)
# rename info.json file
pbar.set_postfix_str("Renaming metadata")
metadata_path = current_video["metadata"]
new_metadata_path = os.path.join(root_video_path, f"[{video_id}].info.json")
os.rename(metadata_path, new_metadata_path)
# remove old video file
os.remove(current_video_path)
pbar.update(1)
print("Finished processing videos")
if __name__ == "__main__":
if shutil.which("ffmpeg") is None:
raise FileNotFoundError("Couldn't find ffmpeg!")
confirm = input(
"This script will process all files in the specified directory and convert them to mp4. \
\nOld video files will be deleted after conversion.\n\nAre you sure you want to continue? (y/n): "
)
if confirm.lower() != "y":
print("Aborted")
sys.exit(0)
confirm2 = input(
"Ensure you have a backup of your files before running this script!!! \
\n\nAre you sure you want to continue? (y/n): "
)
if confirm2.lower() != "y":
print("Aborted")
sys.exit(0)
main()