-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge_mp3.py
89 lines (65 loc) · 2.75 KB
/
merge_mp3.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
#!/usr/bin/env python3
import argparse
import glob
import os
import time
from pathlib import Path
from pydub import AudioSegment
description = """
Merges specified mp3 files into a single file or if no mp3 files are specified,
merges all mp3 files in the current directory.
"""
parser = argparse.ArgumentParser(prog=os.path.basename(__file__), description=description)
parser.add_argument("--files", "-f", nargs="+", help="The files to be merged (Merged in the order of input).")
parser.add_argument("--dir", "-d", nargs=1, help="The directory that contains the files to be merged \
(Instead of specifying individual files).")
parser.add_argument("--sort", "-s", nargs="*", help="Sort input files before merge.")
parser.add_argument("--output", "-o", nargs=1, help="Output path for the merged mp3 file.")
args = parser.parse_args()
if __name__ == "__main__":
files = args.files
directory = None if args.dir == None else str((args.dir)[0])
sort = True if args.sort != None else False
output = None if args.output == None else str((args.output)[0])
if directory != None:
if not os.path.exists(directory):
raise Exception(f"Directory {directory} does not exist.")
if not directory.endswith("/"):
directory = directory + "/"
files = glob.glob(directory + "*.mp3")
if len(files) == 0:
print(f"No mp3 files in {directory} directory.")
exit()
ans = str(input(f"You sure you want to merge {len(files)} mp3 files in the current directory? (y/n) "))
if ans.upper() != "Y":
exit()
elif files == None: # No files selected
files = glob.glob("*.mp3")
if len(files) == 0:
print("No mp3 files in the current directory.")
exit()
ans = str(input(f"You sure you want to merge {len(files)} mp3 files in the current directory? (y/n) "))
if ans.upper() != "Y":
exit()
if sort:
files = sorted(files)
if output == None:
output = "merged_mp3_files.mp3"
else:
if not output.endswith(".mp3"):
raise Exception(f"'{output}' does not end with '.mp3'.")
try:
Path(os.path.dirname(output)).mkdir(parents=True, exist_ok=True)
except Exception:
raise Exception("Could not create output path.")
print("Merging, please wait...")
timer = time.perf_counter() # Elapsed time timer
merged = AudioSegment.empty()
fConcat = list()
for f in files:
name = AudioSegment.from_mp3(f)
fConcat.extend([name])
for f in fConcat:
merged += f
merged.export(output, format="mp3")
print(f"Elapsed time to merge mp3 files: {time.perf_counter() - timer}")