-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetaudio
More file actions
99 lines (84 loc) · 3.32 KB
/
setaudio
File metadata and controls
99 lines (84 loc) · 3.32 KB
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
#!/usr/bin/env python3
"""
setaudio: Batch set the audio language tag for MKV and MP4 files in a folder.
This script lists all .mkv and .mp4 files in the current directory and allows you to select which files to process (or press Enter to select all).
You can then choose a language from a menu (default is English), and the script will set the audio language tag for each selected file:
- For MKV files, it uses mkvpropedit to set the language of the first audio track.
- For MP4 files, it uses MP4Box to set the language.
Usage:
python3 setaudio
# Follow the prompts to select files and language.
Requirements:
- mkvpropedit (part of mkvtoolnix) for MKV files
- MP4Box for MP4 files
- Both must be installed and in your PATH
This is useful for ensuring your media files have the correct audio language tag for compatibility with media players and libraries.
"""
import os
import subprocess
# List of languages
languages = [
("English", "eng"),
("Portuguese", "por"),
("Spanish", "spa"),
("German", "ger"),
("French", "fre"),
("Norwegian", "nor"),
("Swedish", "swe"),
("Korean", "kor"),
("Japanese", "jpn"),
("Chinese", "chi"),
("Russian", "rus"),
("Italian", "ita"),
("Dutch", "dut"),
("Danish", "dan"),
("Finnish", "fin"),
("Polish", "pol"),
("Czech", "ces"),
("Hungarian", "hun"),
("Greek", "ell"),
("Turkish", "tur")
]
# Function to list all .mkv and .mp4 files
def list_files():
print("Listing video files in the current directory:")
files = [f for f in os.listdir() if f.endswith('.mkv') or f.endswith('.mp4')]
for i, file in enumerate(files, 1):
print(f"{i}: {file}")
return files
# Function to choose files
def choose_files(files):
chosen_files = input("Enter the numbers of the files you want to process, comma separated (leave empty to select all): ")
if not chosen_files:
return files
else:
chosen_indices = [int(x.strip()) - 1 for x in chosen_files.split(',')]
return [files[i] for i in chosen_indices]
def choose_language():
print("Choose the language for audio tracks:")
for i, (lang, code) in enumerate(languages, 1):
print(f"{i}: {lang}")
choice = input("Enter the number corresponding to the desired language [1]: ").strip()
if not choice:
return languages[0][1] # Default to English (first in the list)
return languages[int(choice) - 1][1] # Return the ISO code
# Function to apply the language change to a file
def apply_language(file, language_code):
if file.endswith('.mkv'):
print(f"Setting audio language to {language_code} for {file}")
subprocess.run(['mkvpropedit', '--edit', 'track:a1', '--set', f'language={language_code}', file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
elif file.endswith('.mp4'):
print(f"Setting audio language to {language_code} for {file}")
subprocess.run(['MP4Box', '-lang', language_code, file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
else:
print(f"Unsupported file type for {file}")
# Main function
def main():
files = list_files()
chosen_files = choose_files(files)
language = choose_language()
for file in chosen_files:
apply_language(file, language)
print("Language setting completed.")
if __name__ == "__main__":
main()