|
| 1 | +from flask import Flask, request, redirect, render_template, send_file |
| 2 | +import zipfile |
| 3 | +import numpy as np |
| 4 | +import smtplib |
| 5 | +import concurrent.futures # for multithreading compilation |
| 6 | + |
| 7 | +# import sys |
| 8 | +# import re |
| 9 | +from pytube import YouTube # downloading the mp4 file |
| 10 | +from pydub import AudioSegment # creating small snippets |
| 11 | +from pydub.utils import make_chunks # making small chunks out of it |
| 12 | +import os |
| 13 | +from email.mime.multipart import MIMEMultipart # Emailing the output to the user |
| 14 | +from email.mime.base import MIMEBase |
| 15 | +from email.utils import COMMASPACE, formatdate |
| 16 | +from email import encoders # Encoding the attachment |
| 17 | +from zipfile import ZipFile |
| 18 | +from youtube_search import ( |
| 19 | + YoutubeSearch, |
| 20 | +) # Bypassing the Youtube API with the another API |
| 21 | +import pandas as pd |
| 22 | + |
| 23 | +app = Flask(__name__) |
| 24 | + |
| 25 | + |
| 26 | +@app.route("/", methods=["GET", "POST"]) |
| 27 | +def index(): |
| 28 | + if request.method == "POST": |
| 29 | + singer = request.form["singer"] |
| 30 | + Number_vid = int(request.form["Number_vid"]) |
| 31 | + duration = int(request.form["duration"]) |
| 32 | + Email = request.form["email"] |
| 33 | + with concurrent.futures.ThreadPoolExecutor() as executor: |
| 34 | + executor.submit(process_audio, singer, Number_vid, duration, Email) |
| 35 | + # process_audio(singer,Number_vid,duration) |
| 36 | + # email=request.form['email'] |
| 37 | + return redirect("/success") |
| 38 | + return render_template("index.html") |
| 39 | + |
| 40 | + |
| 41 | +def process_audio(singer, Number_vid, duration, Email): |
| 42 | + singer1 = singer + "songs" |
| 43 | + results1 = YoutubeSearch(singer1, max_results=Number_vid).to_dict() |
| 44 | + data = pd.DataFrame(results1) |
| 45 | + for i in range(1, (data["url_suffix"].count())): |
| 46 | + data["url_suffix"][i] = "https://www.youtube.com" + data["url_suffix"][i] |
| 47 | + links = data["url_suffix"] |
| 48 | + for i in links: |
| 49 | + # yt = YouTube(str(i)) |
| 50 | + yt = YouTube(i, use_oauth=True, allow_oauth_cache=True) |
| 51 | + # print(yt.title) |
| 52 | + # if(yt.length<100): |
| 53 | + # extract only audio |
| 54 | + video = yt.streams.filter(file_extension="mp4", only_audio=True).first() |
| 55 | + # download the file |
| 56 | + out_file = video.download() |
| 57 | + # save the file |
| 58 | + base, ext = os.path.splitext(out_file) |
| 59 | + new_file = base + ".mp4" |
| 60 | + os.rename(out_file, new_file) |
| 61 | + audio_files = [] |
| 62 | + for file in os.listdir(): |
| 63 | + if file.endswith(".mp4"): |
| 64 | + audio = AudioSegment.from_file(file, "mp4") |
| 65 | + audio_file = file.replace(".mp4", ".wav") |
| 66 | + audio.export(audio_file, format="wav") |
| 67 | + audio_files.append(audio_file) |
| 68 | + chunks = [] |
| 69 | + # print(audio_files) |
| 70 | + for audio_file in audio_files: |
| 71 | + chunk = make_chunks( |
| 72 | + AudioSegment.from_file(audio_file, format="wav"), |
| 73 | + chunk_length=duration * 1000, |
| 74 | + ) |
| 75 | + id = np.random.randint(0, len(chunk)) |
| 76 | + chunk = chunk[id] |
| 77 | + # print(chunk) |
| 78 | + chunks.append(chunk) |
| 79 | + merged = AudioSegment.empty() |
| 80 | + for chunk in chunks: |
| 81 | + merged += chunk |
| 82 | + merged.export("output.mp3", format="mp3") |
| 83 | + dir = os.getcwd() |
| 84 | + test = os.listdir(dir) |
| 85 | + for item in test: |
| 86 | + if item.endswith(".mp4"): |
| 87 | + os.remove(os.path.join(dir, item)) |
| 88 | + if item.endswith(".wav"): |
| 89 | + os.remove(os.path.join(dir, item)) |
| 90 | + with zipfile.ZipFile("output.zip", "w") as zipf: |
| 91 | + zipf.write("output.mp3") |
| 92 | + msg = MIMEMultipart() |
| 93 | + msg["From"] = "noobbobby241@gmail.com" |
| 94 | + msg["To"] = COMMASPACE.join([Email]) |
| 95 | + msg["Date"] = formatdate(localtime=True) |
| 96 | + msg["Subject"] = "Downloaded and Converted Audio" |
| 97 | + |
| 98 | + with open("output.zip", "rb") as f: |
| 99 | + part = MIMEBase("application", "octet-stream") |
| 100 | + part.set_payload(f.read()) |
| 101 | + |
| 102 | + encoders.encode_base64(part) |
| 103 | + part.add_header("Content-Disposition", "attachment", filename="output.zip") |
| 104 | + msg.attach(part) |
| 105 | + smtp = smtplib.SMTP("smtp.gmail.com", 587) |
| 106 | + smtp.ehlo() |
| 107 | + smtp.starttls() |
| 108 | + smtp.ehlo() |
| 109 | + smtp.login("noobbobby241@gmail.com", "cliwqftyqujpisht") |
| 110 | + smtp.sendmail("noobbobby241@gmail.com", [Email], msg.as_string()) |
| 111 | + print("Email Sent") |
| 112 | + |
| 113 | + |
| 114 | +@app.route("/success") |
| 115 | +def success(): |
| 116 | + return render_template("success.html") |
| 117 | + |
| 118 | + |
| 119 | +# @app.route('/download') |
| 120 | +# def download(): |
| 121 | +# #@after_this_request |
| 122 | +# #def cleanup(response): |
| 123 | +# #cleanup_directory() |
| 124 | +# #return response |
| 125 | +# return send_file('output.zip', as_attachment=True) |
| 126 | +# def cleanup_directory(): |
| 127 | +# test = os.listdir() |
| 128 | +# for item in test: |
| 129 | +# if item.endswith(".mp3"): |
| 130 | +# os.remove(os.path.join(os.getcwd(), item)) |
| 131 | +# if item.endswith(".zip"): |
| 132 | +# os.remove(os.path.join(os.getcwd(), item)) |
| 133 | +if __name__ == "__main__": |
| 134 | + app.debug = True |
| 135 | + app.run(host="0.0.0.0", port=5000) |
0 commit comments