-
Notifications
You must be signed in to change notification settings - Fork 0
/
flac2wav.py
110 lines (84 loc) · 2.4 KB
/
flac2wav.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
import argparse
import subprocess
import os
from pathlib import Path
import unicodedata
INSTALL_DIR = Path(__file__).parent.absolute()
PROCESS_DIR = os.getcwd()
parser = argparse.ArgumentParser()
parser.add_argument(
dest="POSITIONAL_INPUT_PATH", nargs="?", help="the path to the input file"
)
parser.add_argument(
"-i", "--input", dest="INPUT_PATH", help="the path to the input file"
)
parser.add_argument(
"-o",
"--output",
dest="OUTPUT_PATH",
default="output"
if str(INSTALL_DIR) == PROCESS_DIR or INSTALL_DIR.as_posix() == PROCESS_DIR
else ".",
help="the path to the output folder",
)
args = parser.parse_args()
INPUT_PATH = args.POSITIONAL_INPUT_PATH or args.INPUT_PATH
OUTPUT_PATH = (
args.OUTPUT_PATH
if os.path.isabs(args.OUTPUT_PATH)
else os.path.join(PROCESS_DIR, args.OUTPUT_PATH)
)
def strip_accents(text):
text = unicodedata.normalize("NFKD", text)
text = text.encode("ascii", "ignore")
text = text.decode("utf-8")
return str(text)
def convert():
subprocess.run(["ffmpeg", "-i", INPUT_PATH, "-c:a", CODEC, f"{FILE_NAME}.wav"])
def get_codec():
print("Getting codec...")
if BIT_DEPTH == 16:
codec = "pcm_s16le"
elif BIT_DEPTH == 24:
codec = "pcm_s24le"
elif BIT_DEPTH == 32:
codec = "pcm_s32le"
else:
raise ValueError(f"Unsupported bit depth: {BIT_DEPTH}")
print(f"codec={codec}")
print("Done.")
return codec
def get_bit_depth():
print("Extracting bit depth...")
bit_depth = int(
subprocess.check_output(
[
"ffprobe",
"-v",
"error",
"-select_streams",
"a",
"-show_entries",
"stream=bits_per_raw_sample",
"-of",
"default=noprint_wrappers=1:nokey=1",
INPUT_PATH,
]
).split()[0]
)
print(f"bits_per_sample={bit_depth}")
print("Done.")
return bit_depth
def main():
global BASE_PATH, FILE_EXTENSION
BASE_PATH = os.path.basename(INPUT_PATH)
FILE_EXTENSION = os.path.splitext(BASE_PATH)[1]
global FILE_NAME, INPUT_DIR, FILE_PATH
FILE_NAME = strip_accents(BASE_PATH.removesuffix(FILE_EXTENSION))
global BIT_DEPTH, CODEC
BIT_DEPTH = get_bit_depth()
CODEC = get_codec()
convert()
if __name__ == "__main__":
main()