-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_processing.py
83 lines (71 loc) · 2.01 KB
/
audio_processing.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
import json
import re
import subprocess
import logging
logger = logging.getLogger(__name__)
def extract_metadata(file_path):
meta_data = subprocess.run(
[
"ffprobe",
"-v",
"quiet",
"-print_format",
"json",
"-show_format",
"-show_streams",
file_path,
],
check=True,
capture_output=True,
).stdout
return json.loads(meta_data)
def extract_url(file_path):
logger.debug(f"Trying to extract URL from {file_path}")
metadata = extract_metadata(file_path)
try:
url = metadata["format"]["tags"]["comment"]
except KeyError as e:
file = file_path.replace('"', '\\"')
logger.warning(f'KeyError: {e} in the metadata of "{file}"')
url = None
return url
def getid(url):
video_id = subprocess.run(
["yt-dlp", "--get-id", url], text=True, check=True, capture_output=True
).stdout.strip()
return video_id
def yt_dlp(url):
logger.debug(f"Downloading {url}")
output = subprocess.run(
[
"yt-dlp",
"--format",
"bestaudio.2/bestaudio/best.2/best",
# "--format-sort",
# "+size,+br,+res,+fps",
"--extract-audio",
"--paths",
"./audios",
"--output",
"%(id)s.%(ext)s",
"--embed-metadata",
"--no-write-description",
"--no-write-info-json",
"--no-write-playlist-metafiles",
"--no-write-comments",
"--no-write-thumbnail",
"--no-write-subs",
"--no-write-auto-subs",
url,
],
text=True,
check=True,
capture_output=True,
).stdout
destination = re.findall(
r"\[(?:.*)\] (?:Destination: (.*)|(.*) has already been downloaded)",
output,
)[-1]
destination = destination[0] or destination[1]
logger.debug(f"Downloaded {url} to {destination}")
return destination