Spaces:
Runtime error
Runtime error
File size: 7,553 Bytes
dfa1e3d 52480d5 dfa1e3d 52480d5 dfa1e3d |
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 190 191 192 193 194 195 196 197 198 199 |
import os
from email import encoders
import subprocess
import sys
from pytube import YouTube
import moviepy.editor as mp
import zipfile
import streamlit as st
import email, smtplib, ssl
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from youtube_search import YoutubeSearch
# def download_video(singer, num_videos, output_dir):
# print(f"Downloading {num_videos} videos of {singer}...")
# try:
# os.makedirs(output_dir, exist_ok=True)
# # Search for videos using YoutubeSearch
# results = YoutubeSearch(singer + " songs", max_results=num_videos).to_dict()
# downloaded = 0
# for video in results:
# if downloaded >= num_videos:
# break
# try:
# yt = YouTube("https://www.youtube.com" + video['url_suffix'])
# stream = yt.streams.filter(only_audio=True).first()
# if stream:
# stream.download(output_dir)
# downloaded += 1
# print(f"Downloading video {downloaded}...")
# else:
# print(f"No available audio stream for video {video['title']}")
# except Exception as e:
# print(f"Error downloading video {video['title']}: {e}")
# print("Download complete.")
# except Exception as e:
# print(f"Error downloading videos: {e}")
# sys.exit(1)
def download_video(singer, num_videos, output_dir):
print(f"Downloading {num_videos} videos of {singer}...")
try:
os.makedirs(output_dir, exist_ok=True)
# Search for videos using YoutubeSearch
results = YoutubeSearch(singer + " songs", max_results=num_videos).to_dict()
downloaded = 0
for video in results:
if downloaded >= num_videos:
break
try:
yt = YouTube("https://www.youtube.com" + video['url_suffix'])
stream = yt.streams.filter(only_audio=True).first()
if stream:
stream.download(output_dir)
downloaded += 1
print(f"Downloading video {downloaded}...")
else:
print(f"No available audio stream for video {video['title']}")
except Exception as e:
print(f"Error downloading video {video['title']}: {e}")
print("Download complete.")
except Exception as e:
print(f"Error downloading videos: {e}")
sys.exit(1)
def cut_audio(input_dir, output_dir, duration):
print(f"Cutting first {duration} seconds of audio...")
try:
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(input_dir):
if filename.endswith(".mp3"):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename.replace(".mp3", "_cut.mp3"))
subprocess.run(["ffmpeg", "-ss", "00:00:00", "-t", str(duration), "-i", input_path, "-acodec", "copy", output_path], check=True)
print("Cutting complete.")
except Exception as e:
print(f"Error cutting audio: {e}")
sys.exit(1)
def merge_audio(input_dir, output_file):
print("Merging audio files...")
try:
with open(output_file, "wb") as outfile:
for file in os.listdir(input_dir):
if file.endswith(".mp3"):
with open(os.path.join(input_dir, file), "rb") as infile:
outfile.write(infile.read())
print("Merging complete.")
except Exception as e:
print(f"Error merging audio files: {e}")
sys.exit(1)
def convert_to_audio(input_dir, output_dir):
print("Converting videos to audio...")
try:
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(input_dir):
if filename.endswith(".mp4"):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename.replace(".mp4", ".mp3"))
subprocess.run(["ffmpeg", "-i", input_path, "-vn", "-acodec", "libmp3lame", "-y", output_path], check=True)
print("Conversion complete.")
except Exception as e:
print(f"Error converting videos to audio: {e}")
sys.exit(1)
def main(singer, num_videos, cut_duration, output_file, email):
input_dir = "temp_videos"
output_dir = "temp_audio"
download_video(singer, num_videos, input_dir)
convert_to_audio(input_dir, output_dir)
cut_audio(output_dir, output_dir, cut_duration)
merge_audio(output_dir, output_file)
# Clean up temporary directories
os.system(f"rm -rf {input_dir}")
os.system(f"rm -rf {output_dir}")
# Zip the audio file
zip_file = output_file + ".zip"
with zipfile.ZipFile(zip_file, 'w') as zipObj:
zipObj.write(output_file)
# Send email with the zip file attached
send_email(email, zip_file)
print("Mashup complete!")
def send_email(receiver_email, attachment_path):
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "averma3_be21@thapar.edu" # Enter your email address
password = "bads opsw ssss tcru" # Enter your email password
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = "Your Mashup Audio File!"
# Add body to email
message.attach(MIMEText("Your mashup has been created. Please find the attached zip file.", "plain"))
# Open and attach the zip file
with open(attachment_path, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header with the filename
part.add_header(
"Content-Disposition",
f"attachment; filename= {attachment_path}",
)
# Add attachment to message
message.attach(part)
# Create a secure SSL context
context = ssl.create_default_context()
# Send email
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
if __name__ == "__main__":
st.title("Mashup Web Service by Aman Verma,TIET :)")
st.write("Enter the details below to create a mashup of songs and receive it via email.")
form = st.form(key='my_form')
name = form.text_input(label='Enter singer name 🎤')
output_file = form.text_input(label='Enter output file name with .mp3 extension')
num_videos = form.number_input(label='Enter number of videos', min_value=1, step=1)
cut_duration = form.number_input(label='Enter cut duration in seconds', min_value=1, step=1)
email = form.text_input(label='Enter your email-id')
submit_button = form.form_submit_button(label='Submit')
if submit_button:
if not name or not output_file or not email:
st.warning('Please enter all the required fields!')
else:
st.success('Mashup request submitted successfully! Please wait for the email with the mashup file.')
# Call main function to create mashup and send email
main(name, num_videos, cut_duration, output_file, email)
|