Spaces:
Runtime error
Runtime error
Campfireman
commited on
Commit
•
f3c9c16
1
Parent(s):
c814b40
Update youtube_vid_audio_grab_N_take.py
Browse files
youtube_vid_audio_grab_N_take.py
CHANGED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import os
|
3 |
+
import validators
|
4 |
+
import datatime
|
5 |
+
from moviepy.editor import *
|
6 |
+
import gradio as gr
|
7 |
+
from huggingface_hub import snapshot_download
|
8 |
+
import huggingdace_hub as hh
|
9 |
+
from pytube import YouTube
|
10 |
+
|
11 |
+
global notify
|
12 |
+
notify = ""
|
13 |
+
global temp_addr
|
14 |
+
temp_addr = "/yt_download/" + datatime.datetime.now()
|
15 |
+
|
16 |
+
def clean_up():
|
17 |
+
hh.delete_folder("/yt_download")
|
18 |
+
os.mkdir("/yt_download")
|
19 |
+
|
20 |
+
def convert_video_to_audio_ffmpeg(video_file, output_ext="mp3"):
|
21 |
+
"""Converts video to audio directly using `ffmpeg` command
|
22 |
+
with the help of subprocess module"""
|
23 |
+
filename, ext = os.path.splitext(video_file)
|
24 |
+
subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
|
25 |
+
stdout=subprocess.DEVNULL,
|
26 |
+
stderr=subprocess.STDOUT)
|
27 |
+
return (filename + "." + ext)
|
28 |
+
|
29 |
+
def get_video(url):
|
30 |
+
if(!validators.url(url)):
|
31 |
+
notify = "Invalid URL!"
|
32 |
+
return False
|
33 |
+
yt = YouTube(url)
|
34 |
+
mp4_files = yt.streams.filter(file_extension="mp4")
|
35 |
+
os.mkdir(temp_addr)
|
36 |
+
print("Temp buffer folder created.")
|
37 |
+
|
38 |
+
# Download
|
39 |
+
mp4_360p_files = mp4_files.get_by_resolution("360p")
|
40 |
+
mp4_720p_files = mp4_files.get_by_resolution("720p")
|
41 |
+
mp4_1080p_files = mp4_files.get_by_resolution("1080p")
|
42 |
+
|
43 |
+
mp4_360p_files.download(temp_addr)
|
44 |
+
mp4_720p_files.download(temp_addr)
|
45 |
+
mp4_1080p_files.download(temp_addr)
|
46 |
+
|
47 |
+
notify = "Video(s) fetched successfuly."
|
48 |
+
return True
|
49 |
+
|
50 |
+
def extract_audio():
|
51 |
+
for file in os.listdir("temp_addr"):
|
52 |
+
if file.endswith(".mp4"):
|
53 |
+
f_addr = temp_addr + "/" + convert_video_to_audio_ffmpeg(file)
|
54 |
+
print("Current audio address:" + f_addr)
|
55 |
+
|
56 |
+
snapshot_download(repo_id="Campfireman/YouTubeAudioGrabNTake", allow_patterns="*.mp3")
|
57 |
+
notify = "Sucess. Download request will be pulled up soon. "
|
58 |
+
|
59 |
+
def extrator_pipeline(url):
|
60 |
+
clean_up()
|
61 |
+
get_video(url)
|
62 |
+
extract_audio()
|
63 |
+
return notity
|
64 |
+
|
65 |
+
demo = gr.Interface(fn=extrator_pipeline, inputs="text", outputs="text")
|
66 |
+
|