File size: 4,624 Bytes
369fa58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

# 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}"')


################################################################################