-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertToMp3.py
More file actions
188 lines (151 loc) · 6.72 KB
/
ConvertToMp3.py
File metadata and controls
188 lines (151 loc) · 6.72 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
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
import os
import argparse
import re
import sys
import shutil
import subprocess
def list_flac_files(input_dir):
return [f for f in os.listdir(input_dir) if f.lower().endswith('.flac')]
def parse_track_info(base):
"""
Extract track number and name from filename base.
Returns (track_number, track_name)
"""
# Pattern: <Artist>-<track number>-<track name>
match = re.match(r'^(.+?)-\s*(\d+)\s*-\s*(.+)$', base)
if match:
return match.group(2).strip(), match.group(3).strip()
# Pattern: <track number>-<track name>
match = re.match(r'^(\d+)\s*-\s*(.+)$', base)
if match:
return match.group(1).strip(), match.group(2).strip()
return None, base.strip()
def clean_album_name(folder_name):
# Remove patterns like "(1999)", "[1999]", "1999"
cleaned = re.sub(r"[\(\[]?\b\d{4}\b[\)\]]?", "", folder_name)
return cleaned.strip()
def check_ffmpeg():
# Check for ffmpeg in PATH or local directory
ffmpeg_path = shutil.which('ffmpeg')
if not ffmpeg_path:
local_ffmpeg = os.path.join(os.getcwd(), 'ffmpeg')
if os.path.isfile(local_ffmpeg) and os.access(local_ffmpeg, os.X_OK):
ffmpeg_path = local_ffmpeg
if not ffmpeg_path:
print("ffmpeg not found in PATH or local directory. Aborting.")
return None
return ffmpeg_path
def convert_flac_to_mp3(input_dir, output_dir, bitrate, artist):
ffmpeg_path = check_ffmpeg()
if not ffmpeg_path:
return
folder_name = os.path.basename(os.path.normpath(input_dir))
album_name = clean_album_name(folder_name)
tracks_numbered = all_tracks_numbered(input_dir)
print(f"Artist: {artist}")
print(f"Album: {album_name}")
os.makedirs(output_dir, exist_ok=True)
for filename in list_flac_files(input_dir):
flac_path = os.path.join(input_dir, filename)
base = os.path.splitext(filename)[0]
track_number, track_name = parse_track_info(base)
if tracks_numbered and not track_number:
print(f"Warning: tracks are not properly numbered. Using track name: {track_name}")
# Sanitize filename for filesystem
safe_name = re.sub(r'[\\/:"*?<>|]+', '', track_name)
safe_name = re.sub(r'\s+', ' ', safe_name).strip()
mp3_path = os.path.join(output_dir, f"{safe_name}.mp3")
if os.path.exists(mp3_path):
print(f"Error: output file already exists, skipping: {mp3_path}")
continue
cmd = [
ffmpeg_path,
'-i', flac_path,
'-b:a', bitrate,
'-y', # overwrite output if needed
'-metadata', f'artist={artist}',
'-metadata', f'album={album_name}'
]
if track_number:
cmd += ['-metadata', f'track={track_number}']
cmd += [mp3_path]
try:
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode == 0:
print(f"Converted: {filename} -> {mp3_path}")
else:
print(f"Failed to convert {filename}: {result.stderr.decode().strip()}")
except Exception as e:
print(f"Failed to convert {filename}: {e}")
def detect_artist_from_filenames(input_dir):
files = list_flac_files(input_dir)
if not files:
print("No .flac files found in the input directory.")
return None
# Pattern: <Artist>-<track number>-<track name>
pattern = re.compile(r'^(.+?)-\s*\d+\s*-\s*.+$', re.IGNORECASE)
artists = set()
for f in files:
base = os.path.splitext(f)[0]
m = pattern.match(base)
if not m:
return None
artists.add(m.group(1).strip())
if len(artists) == 1:
return artists.pop()
return None
# Check if all tracks are numbered in the format <Artist>-<track number>-<track name>.
# In some cases, the artist may not be present, but will still follow the pattern <track number>-<track name>. This is also acceptable, but the track number must be present and sequential starting from 1.
# This also checks that all files match the pattern, which is necessary for auto-detection to work properly.
def all_tracks_numbered(input_dir):
files = list_flac_files(input_dir)
if not files:
print("No .flac files found in the input directory.")
return False
pattern = re.compile(r'^(.+?-\s*)?(\d+)\s*-\s*.+$', re.IGNORECASE)
track_numbers = set()
for f in files:
base = os.path.splitext(f)[0]
m = pattern.match(base)
if not m:
print(f"Warning: file does not match expected pattern and may not be properly numbered: {f}")
return False
track_numbers.add(int(m.group(2)))
# Check if track numbers are sequential starting from 1
expected_tracks = set(range(1, max(track_numbers) + 1))
if track_numbers != expected_tracks:
print(f"Warning: track numbers are not sequential starting from 1. Detected track numbers: {sorted(track_numbers)}")
return False
return True
if __name__ == "__main__":
ffmpeg_path = check_ffmpeg()
if not ffmpeg_path:
sys.exit(0)
parser = argparse.ArgumentParser(description="Convert FLAC files to MP3 format.")
parser.add_argument("input_dir", type=str, help="Path to the input directory containing FLAC files.")
parser.add_argument("--artist", type=str, required=False, help="Artist name (optional). If omitted the script will try to auto-detect from filenames.")
parser.add_argument("--output_dir", type=str, default=None, help="Output directory (default: input_dir/mp3).")
parser.add_argument("--bitrate", type=str, default="192k", help="Bitrate for MP3 files (default: 192k).")
args = parser.parse_args()
output_directory = args.output_dir or os.path.join(args.input_dir, "mp3")
# Determine artist: use provided, else attempt autodetect and prompt
if args.artist:
artist = args.artist
else:
detected = detect_artist_from_filenames(args.input_dir)
if not detected:
print("Could not detect a consistent artist from filenames. Aborting without conversion.")
sys.exit(0)
# Prompt user to confirm
try:
resp = input(f"Detected artist '{detected}' from filenames. Use this artist? [Y/n]: ").strip().lower()
except KeyboardInterrupt:
print("\nUser cancelled. Aborting.")
sys.exit(0)
if resp in ('', 'y', 'yes'):
all_tracks_numbered(args.input_dir) # Warn if tracks are not properly numbered, but continue anyway
artist = detected
else:
print("Artist not confirmed. Aborting without conversion.")
sys.exit(0)
convert_flac_to_mp3(args.input_dir, output_directory, args.bitrate, artist)