-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102218082.py
127 lines (105 loc) · 4.28 KB
/
102218082.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
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
import sys
from pytube import YouTube
from youtubesearchpython import VideosSearch
from moviepy.editor import concatenate_audioclips, AudioFileClip
def download_videos(singer_name, num_videos):
search_query = f"{singer_name} songs"
print(f"Searching for {num_videos} videos of {singer_name}...")
# YouTube search function and download
videos_search = VideosSearch(search_query, limit=num_videos)
results = videos_search.next()
# Print the entire results response for debugging
print("Search results:", results)
# Check if 'result' is in the response
if 'result' in results:
video_urls = [result['link'] for result in results['result']]
else:
print("No results found or invalid response.")
return []
if not video_urls:
print("No video URLs found.")
return []
video_paths = []
for idx, video_url in enumerate(video_urls):
try:
yt = YouTube(video_url)
print(f"Downloading {yt.title}...")
stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
if stream:
video_file = f"{singer_name}_video_{idx + 1}.mp4"
stream.download(filename=video_file)
video_paths.append(video_file)
else:
print(f"Stream not found for video: {video_url}")
except Exception as e:
print(f"An error occurred while downloading: {e}")
return video_paths
def convert_to_audio(video_paths):
audio_paths = []
for video_path in video_paths:
try:
yt_video = AudioFileClip(video_path)
audio_file = video_path.replace('.mp4', '.mp3')
yt_video.write_audiofile(audio_file)
audio_paths.append(audio_file)
yt_video.close()
print(f"Converted {video_path} to {audio_file}.")
except Exception as e:
print(f"An error occurred during conversion: {e}")
return audio_paths
def cut_audio(audio_paths, cut_duration):
cut_audio_paths = []
for audio_path in audio_paths:
try:
audio_clip = AudioFileClip(audio_path)
cut_audio_file = audio_path.replace('.mp3', f'_cut.mp3')
cut_audio = audio_clip.subclip(0, cut_duration)
cut_audio.write_audiofile(cut_audio_file)
cut_audio_paths.append(cut_audio_file)
audio_clip.close()
print(f"Cut audio file created: {cut_audio_file}.")
except Exception as e:
print(f"An error occurred while cutting audio: {e}")
return cut_audio_paths
def merge_audios(cut_audio_paths, output_file):
try:
audio_clips = [AudioFileClip(audio) for audio in cut_audio_paths]
final_clip = concatenate_audioclips(audio_clips)
final_clip.write_audiofile(output_file)
for clip in audio_clips:
clip.close()
final_clip.close()
print(f"Merged audio saved as {output_file}.")
except Exception as e:
print(f"An error occurred while merging audios: {e}")
def main():
# Check for the correct number of parameters
if len(sys.argv) != 5:
print("Usage: python <program.py> <SingerName> <NumberOfVideos> <AudioDuration> <OutputFileName>")
sys.exit(1)
# Read command line arguments
singer_name = sys.argv[1]
try:
num_videos = int(sys.argv[2])
audio_duration = int(sys.argv[3])
except ValueError:
print("Number of videos and audio duration must be integers.")
sys.exit(1)
output_file_name = sys.argv[4]
# Check for valid input values
if num_videos <= 10:
print("Number of videos must be greater than 10.")
sys.exit(1)
if audio_duration <= 20:
print("Audio duration must be greater than 20 seconds.")
sys.exit(1)
# Main processing steps
video_paths = download_videos(singer_name, num_videos)
if not video_paths:
print("No videos downloaded. Exiting.")
sys.exit(1)
audio_paths = convert_to_audio(video_paths)
cut_audio_paths = cut_audio(audio_paths, audio_duration)
merge_audios(cut_audio_paths, output_file_name)
if __name__ == "__main__":
main()