Upload 2 files
Browse files- download.py +91 -0
- links.csv +26 -0
download.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
from yt_dlp import YoutubeDL
|
3 |
+
import pandas as pd
|
4 |
+
import os
|
5 |
+
import platform
|
6 |
+
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
|
7 |
+
|
8 |
+
#sheet_id = "1yMeqGmhDEwLvz956L-AL6rOhREo9WNvmO00-sacGkEg"
|
9 |
+
#gid = "469944202"
|
10 |
+
#df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?gid={gid}&format=csv")
|
11 |
+
|
12 |
+
df = pd.read_csv('./links.csv')
|
13 |
+
|
14 |
+
def ensure_hhmmss_format(t):
|
15 |
+
parts = t.split(':')
|
16 |
+
if len(parts) == 2:
|
17 |
+
return f"00:{t}"
|
18 |
+
return t
|
19 |
+
|
20 |
+
def create_dir(string):
|
21 |
+
try:
|
22 |
+
os.makedirs(string)
|
23 |
+
except Exception as e:
|
24 |
+
...
|
25 |
+
|
26 |
+
def trim_audio(input_file, start_time, end_time, output_file):
|
27 |
+
try:
|
28 |
+
ffmpeg_extract_subclip(input_file, float(start_time), float(end_time), targetname=output_file)
|
29 |
+
except Exception as e:
|
30 |
+
print(f"moviepy error: {e}")
|
31 |
+
print("Falling back to direct ffmpeg call")
|
32 |
+
|
33 |
+
# Detect the OS
|
34 |
+
current_platform = platform.system()
|
35 |
+
|
36 |
+
# Adjust command if on Windows (use 'ffmpeg.exe' if needed)
|
37 |
+
ffmpeg_cmd = 'ffmpeg'
|
38 |
+
if current_platform == 'Windows':
|
39 |
+
ffmpeg_cmd = 'ffmpeg.exe'
|
40 |
+
|
41 |
+
command = [
|
42 |
+
ffmpeg_cmd,
|
43 |
+
'-y',
|
44 |
+
'-i', input_file,
|
45 |
+
'-ss', str(start_time),
|
46 |
+
'-to', str(end_time),
|
47 |
+
'-c', 'copy',
|
48 |
+
output_file
|
49 |
+
]
|
50 |
+
subprocess.run(command, check=True)
|
51 |
+
|
52 |
+
def download_audio(youtube_url, output_file):
|
53 |
+
ydl_opts = {
|
54 |
+
'format': 'bestaudio/best',
|
55 |
+
'postprocessors': [{
|
56 |
+
'key': 'FFmpegExtractAudio',
|
57 |
+
'preferredcodec': 'mp3',
|
58 |
+
'preferredquality': '192',
|
59 |
+
}],
|
60 |
+
'outtmpl': output_file,
|
61 |
+
}
|
62 |
+
|
63 |
+
with YoutubeDL(ydl_opts) as ydl:
|
64 |
+
ydl.download([youtube_url])
|
65 |
+
|
66 |
+
for k,v in df.T.to_dict().items():
|
67 |
+
lst = v['timestamps'].replace("(",'').replace(')','').split(',')
|
68 |
+
start,end = lst
|
69 |
+
start = ensure_hhmmss_format(start)
|
70 |
+
end = ensure_hhmmss_format(end)
|
71 |
+
|
72 |
+
unique_name = k
|
73 |
+
uid = v['id']
|
74 |
+
category = v['category']
|
75 |
+
|
76 |
+
create_dir(f"{category}/{uid}")
|
77 |
+
|
78 |
+
youtube_url = 'https://www.youtube.com/watch?v=nEiCCaawtHY'
|
79 |
+
output_file = "audio"
|
80 |
+
|
81 |
+
download_audio(v['url'],f"{category}/{uid}/{unique_name}")
|
82 |
+
|
83 |
+
trim_audio(f"{category}/{uid}/{unique_name}.mp3", start, end, f"{category}/{uid}/{unique_name}_{v['name'.replace(' ','_')]}_voices.mp3")
|
84 |
+
|
85 |
+
|
86 |
+
# clean raw voices
|
87 |
+
for root, dirs, files in os.walk(os.getcwd()):
|
88 |
+
for file in files:
|
89 |
+
file_path = os.path.join(root, file)
|
90 |
+
if "voices" not in file_path and file_path.endswith(".mp3"):
|
91 |
+
os.remove(file_path)
|
links.csv
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
id,category,name,url,timestamps
|
2 |
+
1,Celebrities,Morgan Freeman,https://www.youtube.com/watch?v=nEiCCaawtHY,"(0:11,0:28)"
|
3 |
+
2,Celebrities,Scarlett Johansson,https://www.youtube.com/watch?v=YGqQk12jBoA,"(1:33,1:51)"
|
4 |
+
3,Celebrities,David Attenborough,https://www.youtube.com/watch?v=fyYpExl8AJU,"(0:05,3:18)"
|
5 |
+
4,Celebrities,Tom Hanks,https://www.youtube.com/watch?v=F4GGxcHijNk,"(1:24,5:47)"
|
6 |
+
5,Celebrities,Emma Watson,https://www.youtube.com/watch?v=2C4c7VR8X7I,"(0:03,3:32)"
|
7 |
+
6,Characters,Batman,https://www.youtube.com/watch?v=cA02UWAA-pA,"(0:01,1:19)"
|
8 |
+
7,Characters,Spongebob,https://m.youtube.com/watch?v=FMSgWt8XvNY,"(0:31,0:42)"
|
9 |
+
8,Characters,Darth Vader,https://m.youtube.com/watch?v=pjpabV4kmAc,"(0:00,0:34)"
|
10 |
+
9,Characters,Homer Simpson,https://m.youtube.com/watch?v=KhKIKehqds8,"(1:24,1:43)"
|
11 |
+
10,Characters,Mario,https://www.youtube.com/watch?v=FrsXkCIZsq4,"(0:23,0:56)"
|
12 |
+
11,Streamers,PewDiePie,https://www.youtube.com/watch?v=8hp6Ea_Cphc,"(0:03,2:35)"
|
13 |
+
12,Streamers,Pokimane,https://www.youtube.com/watch?v=vdvlCcwBIA8,"(0:52,1:41)"
|
14 |
+
13,Streamers,Ninja,https://www.youtube.com/watch?v=f-TuRnogRDA,"(0:01,0:44)"
|
15 |
+
14,Streamers,Shroud,https://www.youtube.com/watch?v=9bJKR8kpJ2k,"(12:07,14:07)"
|
16 |
+
15,Streamers,Tfue,https://www.youtube.com/watch?v=5goOTtfg5xc,"(0:01,2:00)"
|
17 |
+
16,Politicians,Barack Obama,https://www.youtube.com/watch?v=77pnVFLkUjM,"(0:00,0:21)"
|
18 |
+
17,Politicians,Donald Trump,https://www.youtube.com/watch?v=QQezb5YDRSc,"(0:18,7:17)"
|
19 |
+
18,Politicians,Angela Merkel,https://www.youtube.com/watch?v=KhezfXh8CqQ,"(0:21,1:02)"
|
20 |
+
19,Politicians,Justin Trudeau,https://www.youtube.com/watch?v=ulDYH87K_VU,"(0:01,1:56)"
|
21 |
+
20,Politicians,Emmanuel Macron,https://www.youtube.com/watch?v=o1Z99tTOAH0,"(0:01,1:28)"
|
22 |
+
21,Athletes,Serena Williams,https://www.youtube.com/watch?v=NiGZ6ngNAxU,"(0:03,0:23)"
|
23 |
+
22,Athletes,Michael Jordan,https://www.youtube.com/watch?v=awnZsC2ZcHE,"(1:46,4:32)"
|
24 |
+
23,Athletes,Lionel Messi,https://www.youtube.com/watch?v=YXtZYbBagGA,"(0:23,0:36)"
|
25 |
+
24,Athletes,LeBron James,https://www.youtube.com/watch?v=YonmfL9GvOw,"(2:01,2:58)"
|
26 |
+
25,Athletes,Usain,https://www.youtube.com/watch?v=aGLxSn0g6DQ,"(0:16,1:15)"
|