-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulktranscode_core.py
63 lines (59 loc) · 2.3 KB
/
bulktranscode_core.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
import os
import subprocess
import shutil
# Mapping of codecs to ffmpeg codec names and file extensions.
CODEC_NAME = {
"aac": "aac",
"flac": "flac",
"opus": "libopus",
"mp3": "libmp3lame",
"vorbis": "libvorbis",
}
FILE_EXTENSION = {
"aac": ".aac",
"flac": ".flac",
"opus": ".opus",
"mp3": ".mp3",
"vorbis": ".ogg",
}
def gather_files(source_folder, destination_folder, source_codec, target_codec, copy_other_files):
"""
Walk through the source folder and prepare a list of files to process.
Returns a list of tuples: (input_file, output_file, mode) where mode is "transcode" or "copy".
"""
files_to_process = []
ext1 = FILE_EXTENSION[source_codec]
ext2 = FILE_EXTENSION[target_codec]
for root, _, files in os.walk(source_folder):
relative_path = os.path.relpath(root, source_folder)
out_dir = os.path.join(destination_folder, relative_path)
os.makedirs(out_dir, exist_ok=True)
for file in files:
input_path = os.path.join(root, file)
if file.endswith(ext1):
output_file = file.replace(ext1, ext2)
output_path = os.path.join(out_dir, output_file)
if not os.path.exists(output_path):
files_to_process.append((input_path, output_path, "transcode"))
elif copy_other_files:
output_path = os.path.join(out_dir, file)
if not os.path.exists(output_path):
files_to_process.append((input_path, output_path, "copy"))
return files_to_process
def process_file(input_file, output_file, mode, target_codec):
"""
Process a single file by transcoding it or copying it.
"""
if mode == "transcode":
if target_codec == "opus":
subprocess.run(["opusenc",input_file,output_file])
elif input_file[-4:] == "opus":
subprocess.run(["ffmpeg","-i",input_file,"-map_metadata","0:s:a:0",output_file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
else:
subprocess.run([
'ffmpeg', '-i', input_file,
'-acodec', CODEC_NAME[target_codec],
output_file
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
elif mode == "copy":
shutil.copy2(input_file, output_file)