File size: 2,139 Bytes
ae202f9
b1fdcc2
ae202f9
b1fdcc2
 
ae202f9
 
b1fdcc2
ae202f9
b1fdcc2
 
ae202f9
 
 
b1fdcc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae202f9
b1fdcc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
import os
import re
import string
import time
from typing import List

import streamlit as st
import yt_dlp
from pytube import Search

logger = logging.getLogger("pytube")
logger.setLevel(logging.ERROR)


def _sanitize_filename(filename):
    safe_chars = "-_.() %s%s" % (
        re.escape(string.ascii_letters),
        re.escape(string.digits),
    )
    safe_filename = re.sub(f"[^{safe_chars}]", "_", filename)
    return safe_filename.strip()


@st.cache_data(show_spinner=False)
def download_audio_from_youtube(url, output_path):
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    with yt_dlp.YoutubeDL() as ydl:
        info_dict = ydl.extract_info(url, download=False)
    if info_dict.get("duration", 0) > 360:
        st.error("Song is too long. Please use a song no longer than 6 minutes.")
        return
    video_title = info_dict.get("title", None)
    video_title = _sanitize_filename(video_title)
    ydl_opts = {
        "format": "bestaudio/best",
        "postprocessors": [
            {
                "key": "FFmpegExtractAudio",
                "preferredcodec": "mp3",
                "preferredquality": "192",
            }
        ],
        "outtmpl": os.path.join(output_path, video_title),
        #'quiet': True,
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    return f"{video_title}.mp3"


@st.cache_data(show_spinner=False)
def query_youtube(query: str) -> Search:
    return Search(query)


def search_youtube(query: str) -> List:
    if len(query) > 3:
        time.sleep(0.5)
        search = query_youtube(query + " lyrics")
        st.session_state.search_results = search.results
        video_options = [video.title for video in st.session_state.search_results]
        st.session_state.video_options = video_options
    else:
        video_options = []
    return video_options


def get_youtube_url(title: str) -> str:
    video = st.session_state.search_results[st.session_state.video_options.index(title)]
    return video.embed_url


def check_if_is_youtube_url(url: str) -> bool:
    return url.startswith("http")