-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert320mp3.py
56 lines (43 loc) · 2.1 KB
/
convert320mp3.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
# This script will:
# 1. loop through all SUBDIRECTORIES in the CURRENT DIRECTORY
# 2. loop through all the files
# 3. check if their extensions match {formats}
# 4. convert each file to 320kbs mp3 with ffmpeg OVERWRITING if the file already exists
# 5. remove the original file
# while logging stuff to ./converted.txt
import datetime
import os
import ffmpeg
# add more formats if you want
formats = ["wav", "m4a", "flac"]
input(f"Read what this script does. Hit return to continue.")
# just for the sake of keeping the logs organized
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("converted.txt", "a") as f:
f.write(f"########## {timestamp}\n")
# get all subdirectories and loop through them
for subdir in os.listdir():
if os.path.isdir(subdir):
print(f"Entering {subdir}...")
with open("converted.txt", "a") as f:
f.write(f"### {subdir}\n")
# get all files in the subdir and loop through them
for file in os.listdir(subdir):
# get the ext
extension = file.split(".")[-1]
# if the file extension is in the list of formats, convert it to mp3
if extension in formats:
# get file name w/o extension, split(".")[-1] doesnt work if there are dots in the filename (e.g. "01. Ghosts 'n' Stuff.wav")
name = file.rsplit(".",1)[0]
print(f"Converting {file} to mp3...")
# convert it to mp3 @ 320kbps, overwrite if file already exists
stream = ffmpeg.input(f"{subdir}/{file}")
stream = ffmpeg.output(stream, f"{subdir}/{name}.mp3", audio_bitrate=320000)
stream = ffmpeg.overwrite_output(stream)
ffmpeg.run(stream)
# log affected files to a text file
with open("converted.txt", "a") as f:
f.write(f"{file}\n--> {name}.mp3\n")
# remove the original file
print(f"Removing {file}...")
os.remove(f"{subdir}/{file}")