my_scripts / my_zingmp3.py
binhnx8's picture
Upload my_zingmp3.py
369fa58 verified
# sudo apt install ffmpeg
# ffmpeg-python > ffmpeg? # don't install both
#!pip install -q yt-dlp
#!pip install -q ffmpeg-python
#!pip install -q ffmpeg
#!pip install -q pydub
#!pip install ffmpeg demucs yt_dlp diffq
#!pip install ffmpeg-python demucs yt_dlp diffq
import os
import glob
import re
import subprocess
from pathlib import Path
import time
import logging
from tqdm import tqdm
import subprocess
################################################################################
def phrase_found(line,x_phrase):
""" """
x_found = []
for phrase in x_phrase:
if phrase.lower() in line.lower():
x_found.append(phrase)
return x_found
def remove_line(in_file, out_file,x_phrase):
"""
x_phrase = ['lam-truong','lan-ngoc','bang-kieu','huy-mc','trang-phap','ngoc-anh','hoang-dung','phuong-dung','thanh-ha','erik','anh-tuan']
remove_line('/content/Thu_Phuong_links.txt', 'new_links.txt',x_phrase)
"""
x_buffer = []
lines = open(in_file).readlines()
for line in lines:
found = phrase_found(line,x_phrase)
if len(found) > 0:
print(f'{found}\t{line}')
else:
x_buffer.append(line)
with open(out_file,"w") as file:
for line in x_buffer:
file.write(line)
################################################################################
def check_subtitle(url):
"""
"""
#import subprocess
cmd = f"yt-dlp --list-subs {url}"
result = subprocess.getoutput(cmd)
#print(result)
last_line = result.splitlines()[-1]
#print(last_line)
if 'no subtitles' in last_line:
return False
else:
return True
def zingmp3_download(url, output_folder,subtitle='yes'):
"""
"""
import os
import subprocess
os.makedirs(output_folder, exist_ok=True)
if subtitle.lower() == 'yes' and check_subtitle(url) == True:
command1 = f"yt-dlp -x '--write-subs' --audio-quality 'best' --audio-format mp3 -o '{output_folder}/%(title)s.%(ext)s' {url}"
#command1 = f"yt-dlp -x '--write-subs' --audio-quality 'best' --audio-format mp3 -o '{output_folder}/%(title)s_%(id)s.%(ext)s' {url}"
subprocess.call(command1, shell=True)
else:
command2 = f"yt-dlp -x --audio-quality 'best' --audio-format mp3 -o '{output_folder}/%(title)s_%(id)s.%(ext)s' {url}"
subprocess.call(command2, shell=True)
################################################################################
def rename_lrc(zingmp3_dir):
"""
rename *.origin.lrc --> *.lrc
"""
#import glob
#import re
#import os
files = glob.glob(f'{zingmp3_dir}/*.lrc')
for current_file in files:
if ".origin" in current_file:
new_file = current_file.replace(".origin","")
#print(new_file)
os.rename(current_file, new_file)
def zingmp3_full(url_links,singer):
"""
Download + Tar + saved to 'upload' folder
url_links = '/content/Anh_Tuyet_links.txt'
singer = 'Anh_Tuyet'
zingmp3_full(url_links,singer)
"""
import os
from tqdm import tqdm
#os.makedirs(singer, exist_ok=True)
links = open(url_links).readlines()
links.sort()
print('Downloading...')
for url in tqdm(links):
zingmp3_download(url, singer,subtitle='yes')
print('Rename *.lrc ...')
rename_lrc(singer)
print('Compressing...')
os.makedirs('upload', exist_ok=True)
os.system(f'tar czf upload/{singer}.tar.gz {singer}')
print(f'Finished. Saved to upload/{singer}.tar.gz')
################################################################################
def name_no_ext(folder,ext='txt'):
""" """
import os
x_name = []
files = os.listdir(folder)
for file in files:
if file.endswith(f'.{ext}'):
name = os.path.splitext(file)[0]
x_name.append(name)
return x_name
def the_same_name(dir_1,dir_2,ext1,ext2):
"""
the_same_name(dir_1,dir_2,'TextGrid','mp3')
"""
x1_name = name_no_ext(dir_1,ext1)
x2_name = name_no_ext(dir_2,ext2)
common = set(x1_name) & set(x2_name)
return list(common)
def remove_unpaired_files(folder_path,ext1,ext2):
"""
remove_unpaired_files('/content/songs','mp3','lrc')
"""
x_common = the_same_name(folder_path,folder_path,ext1,ext2)
files = glob.glob(f'{folder_path}/*')
len(files)
for file in files:
name = os.path.basename(file)
name = os.path.splitext(name)[0]
if name not in x_common:
print(file)
os.system(f'rm -f "{file}"')
################################################################################