File size: 2,377 Bytes
f3c9c16
 
 
2ca415f
217e03e
f3c9c16
 
dac062c
63cf0fd
f3c9c16
 
 
 
 
d3ba29a
dc1735d
f3c9c16
 
d3ba29a
 
f3c9c16
 
 
 
 
 
 
 
 
 
 
198606c
f3c9c16
 
 
 
 
 
 
 
 
 
 
 
580fa68
 
 
f3c9c16
 
0e72faf
f3c9c16
 
 
065d9bd
f3c9c16
 
 
dc1735d
f3c9c16
dac062c
f3c9c16
0e72faf
f3c9c16
dc1735d
 
 
f3c9c16
67a5fbe
f3c9c16
dc1735d
0e72faf
f3c9c16
4835123
 
 
 
 
 
13ba853
4835123
6717855
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
import sys
import os
import validators
import datetime
import subprocess
from moviepy.editor import *
import gradio as gr
from huggingface_hub import snapshot_download
import huggingface_hub as hh
from pytube import YouTube

global notify
notify = ""
global temp_addr
temp_addr = "./yt_download" # + str(int(datetime.datetime.timestamp))
temp_audio_addr = ""

def clean_up():
    os.rmdir("./yt_download")
    os.mkdir("./yt_download")

def convert_video_to_audio_ffmpeg(video_file, output_ext="mp3"):
    """Converts video to audio directly using `ffmpeg` command
    with the help of subprocess module"""
    filename, ext = os.path.splitext(video_file)
    subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"], 
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.STDOUT)
    return (filename + "." + ext)

def get_video(url):
    if(validators.url(url) == False):
        notify = "Invalid URL!"
        return False
    yt = YouTube(url)
    mp4_files = yt.streams.filter(file_extension="mp4")
    os.mkdir(temp_addr)
    print("Temp buffer folder created.")
    
    # Download
    mp4_360p_files = mp4_files.get_by_resolution("360p")
    mp4_720p_files = mp4_files.get_by_resolution("720p")
    mp4_1080p_files = mp4_files.get_by_resolution("1080p")
    
    if(mp4_360p_files): mp4_360p_files.download(temp_addr)
    if(mp4_720p_files): mp4_720p_files.download(temp_addr)
    if(mp4_1080p_files): mp4_1080p_files.download(temp_addr)

    notify = "Video(s) fetched successfuly."
    print(notify)
    return True

def extract_audio():
    for file in os.listdir(temp_addr):
        if file.endswith(".mp4"):
            f_addr = temp_addr + "/" + convert_video_to_audio_ffmpeg(file)
            print("Current audio address:" + f_addr)
        
            
    # snapshot_download(repo_id="Campfireman/YouTubeAudioGrabNTake", allow_patterns="*.mp3")
    notify = "Sucess. Download request will be pulled up soon. "
    print(notify)

    # For debugging use
    print(f_addr)

def extrator_pipeline(url):
    #clean_up()
    get_video(url)
    temp_audio_addr = extract_audio()
    return notify

demo = gr.Interface(
    fn=extrator_pipeline, 
    inputs="text", 
    outputs=gr.Audio(source = "upload"),
    examples=[
        [os.path.join(os.path.dirname(__file__),temp_audio_addr)]
    ]
)
demo.launch()