Upload my_zingmp3.py
Browse files- my_zingmp3.py +188 -0
my_zingmp3.py
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# sudo apt install ffmpeg
|
3 |
+
# ffmpeg-python > ffmpeg? # don't install both
|
4 |
+
|
5 |
+
#!pip install -q yt-dlp
|
6 |
+
#!pip install -q ffmpeg-python
|
7 |
+
#!pip install -q ffmpeg
|
8 |
+
#!pip install -q pydub
|
9 |
+
|
10 |
+
#!pip install ffmpeg demucs yt_dlp diffq
|
11 |
+
#!pip install ffmpeg-python demucs yt_dlp diffq
|
12 |
+
|
13 |
+
import os
|
14 |
+
import glob
|
15 |
+
import re
|
16 |
+
import subprocess
|
17 |
+
from pathlib import Path
|
18 |
+
import time
|
19 |
+
import logging
|
20 |
+
from tqdm import tqdm
|
21 |
+
import subprocess
|
22 |
+
|
23 |
+
################################################################################
|
24 |
+
def phrase_found(line,x_phrase):
|
25 |
+
""" """
|
26 |
+
x_found = []
|
27 |
+
for phrase in x_phrase:
|
28 |
+
if phrase.lower() in line.lower():
|
29 |
+
x_found.append(phrase)
|
30 |
+
return x_found
|
31 |
+
|
32 |
+
|
33 |
+
def remove_line(in_file, out_file,x_phrase):
|
34 |
+
"""
|
35 |
+
x_phrase = ['lam-truong','lan-ngoc','bang-kieu','huy-mc','trang-phap','ngoc-anh','hoang-dung','phuong-dung','thanh-ha','erik','anh-tuan']
|
36 |
+
remove_line('/content/Thu_Phuong_links.txt', 'new_links.txt',x_phrase)
|
37 |
+
|
38 |
+
"""
|
39 |
+
x_buffer = []
|
40 |
+
lines = open(in_file).readlines()
|
41 |
+
for line in lines:
|
42 |
+
found = phrase_found(line,x_phrase)
|
43 |
+
if len(found) > 0:
|
44 |
+
print(f'{found}\t{line}')
|
45 |
+
else:
|
46 |
+
x_buffer.append(line)
|
47 |
+
|
48 |
+
with open(out_file,"w") as file:
|
49 |
+
for line in x_buffer:
|
50 |
+
file.write(line)
|
51 |
+
|
52 |
+
################################################################################
|
53 |
+
|
54 |
+
def check_subtitle(url):
|
55 |
+
"""
|
56 |
+
"""
|
57 |
+
#import subprocess
|
58 |
+
|
59 |
+
cmd = f"yt-dlp --list-subs {url}"
|
60 |
+
result = subprocess.getoutput(cmd)
|
61 |
+
#print(result)
|
62 |
+
last_line = result.splitlines()[-1]
|
63 |
+
#print(last_line)
|
64 |
+
if 'no subtitles' in last_line:
|
65 |
+
return False
|
66 |
+
else:
|
67 |
+
return True
|
68 |
+
|
69 |
+
|
70 |
+
def zingmp3_download(url, output_folder,subtitle='yes'):
|
71 |
+
"""
|
72 |
+
"""
|
73 |
+
import os
|
74 |
+
import subprocess
|
75 |
+
|
76 |
+
os.makedirs(output_folder, exist_ok=True)
|
77 |
+
|
78 |
+
if subtitle.lower() == 'yes' and check_subtitle(url) == True:
|
79 |
+
command1 = f"yt-dlp -x '--write-subs' --audio-quality 'best' --audio-format mp3 -o '{output_folder}/%(title)s.%(ext)s' {url}"
|
80 |
+
#command1 = f"yt-dlp -x '--write-subs' --audio-quality 'best' --audio-format mp3 -o '{output_folder}/%(title)s_%(id)s.%(ext)s' {url}"
|
81 |
+
subprocess.call(command1, shell=True)
|
82 |
+
else:
|
83 |
+
command2 = f"yt-dlp -x --audio-quality 'best' --audio-format mp3 -o '{output_folder}/%(title)s_%(id)s.%(ext)s' {url}"
|
84 |
+
subprocess.call(command2, shell=True)
|
85 |
+
|
86 |
+
|
87 |
+
################################################################################
|
88 |
+
def rename_lrc(zingmp3_dir):
|
89 |
+
"""
|
90 |
+
rename *.origin.lrc --> *.lrc
|
91 |
+
|
92 |
+
"""
|
93 |
+
|
94 |
+
#import glob
|
95 |
+
#import re
|
96 |
+
#import os
|
97 |
+
|
98 |
+
files = glob.glob(f'{zingmp3_dir}/*.lrc')
|
99 |
+
for current_file in files:
|
100 |
+
if ".origin" in current_file:
|
101 |
+
new_file = current_file.replace(".origin","")
|
102 |
+
#print(new_file)
|
103 |
+
os.rename(current_file, new_file)
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
def zingmp3_full(url_links,singer):
|
108 |
+
"""
|
109 |
+
Download + Tar + saved to 'upload' folder
|
110 |
+
url_links = '/content/Anh_Tuyet_links.txt'
|
111 |
+
singer = 'Anh_Tuyet'
|
112 |
+
zingmp3_full(url_links,singer)
|
113 |
+
|
114 |
+
"""
|
115 |
+
import os
|
116 |
+
from tqdm import tqdm
|
117 |
+
|
118 |
+
#os.makedirs(singer, exist_ok=True)
|
119 |
+
links = open(url_links).readlines()
|
120 |
+
links.sort()
|
121 |
+
|
122 |
+
print('Downloading...')
|
123 |
+
for url in tqdm(links):
|
124 |
+
zingmp3_download(url, singer,subtitle='yes')
|
125 |
+
|
126 |
+
print('Rename *.lrc ...')
|
127 |
+
rename_lrc(singer)
|
128 |
+
|
129 |
+
print('Compressing...')
|
130 |
+
os.makedirs('upload', exist_ok=True)
|
131 |
+
os.system(f'tar czf upload/{singer}.tar.gz {singer}')
|
132 |
+
print(f'Finished. Saved to upload/{singer}.tar.gz')
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
################################################################################
|
137 |
+
def name_no_ext(folder,ext='txt'):
|
138 |
+
""" """
|
139 |
+
import os
|
140 |
+
x_name = []
|
141 |
+
files = os.listdir(folder)
|
142 |
+
for file in files:
|
143 |
+
if file.endswith(f'.{ext}'):
|
144 |
+
name = os.path.splitext(file)[0]
|
145 |
+
x_name.append(name)
|
146 |
+
return x_name
|
147 |
+
|
148 |
+
|
149 |
+
def the_same_name(dir_1,dir_2,ext1,ext2):
|
150 |
+
"""
|
151 |
+
the_same_name(dir_1,dir_2,'TextGrid','mp3')
|
152 |
+
|
153 |
+
"""
|
154 |
+
x1_name = name_no_ext(dir_1,ext1)
|
155 |
+
x2_name = name_no_ext(dir_2,ext2)
|
156 |
+
common = set(x1_name) & set(x2_name)
|
157 |
+
return list(common)
|
158 |
+
|
159 |
+
|
160 |
+
def remove_unpaired_files(folder_path,ext1,ext2):
|
161 |
+
"""
|
162 |
+
remove_unpaired_files('/content/songs','mp3','lrc')
|
163 |
+
|
164 |
+
"""
|
165 |
+
x_common = the_same_name(folder_path,folder_path,ext1,ext2)
|
166 |
+
|
167 |
+
files = glob.glob(f'{folder_path}/*')
|
168 |
+
len(files)
|
169 |
+
for file in files:
|
170 |
+
name = os.path.basename(file)
|
171 |
+
name = os.path.splitext(name)[0]
|
172 |
+
if name not in x_common:
|
173 |
+
print(file)
|
174 |
+
os.system(f'rm -f "{file}"')
|
175 |
+
|
176 |
+
|
177 |
+
################################################################################
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
+
|
186 |
+
|
187 |
+
|
188 |
+
|