File size: 4,741 Bytes
f7b3397
da8f353
24811bb
8ce4f62
087c136
24811bb
6c7ed02
4b1708b
6c7ed02
 
24811bb
 
 
 
6c7ed02
5806219
 
 
 
 
 
6c7ed02
5806219
1d6256c
5806219
 
6c7ed02
24811bb
 
 
 
 
 
 
 
 
 
 
 
6c7ed02
 
da8f353
24811bb
 
 
 
 
8ce4f62
 
 
 
 
 
ec6f947
8ce4f62
 
 
 
 
 
 
 
ec6f947
6f98724
36a5b29
ec6f947
36a5b29
ec6f947
 
75fe251
ec6f947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5806219
9d38abe
ec6f947
f38c2a4
24811bb
f38c2a4
f112c87
ec6f947
 
 
ba69b7e
 
ec6f947
 
205045b
8ce4f62
 
 
5806219
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
import gradio as gr
import os
import yt_dlp
import markdown

def downloader(video_url, audio_format, audio_name=None):
    # Ensure the directory exists
    os.makedirs('audios', exist_ok=True)
    
    # Use a temporary placeholder for the output file
    if audio_name:
        temp_output_path = f"audios/{audio_name}.%(ext)s"
    else:
        temp_output_path = f"audios/%(title)s.%(ext)s"
    
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': audio_format,
        }],
        'outtmpl': temp_output_path,
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([video_url])
    
    # Find the downloaded file and rename it if audio_name is provided
    if audio_name:
        temp_file = temp_output_path.replace('%(ext)s', audio_format)
        final_output_path = f"audios/{audio_name}.{audio_format}"
        os.rename(temp_file, final_output_path)
    else:
        final_output_path = None
        # Since the file is already named by title, find it by the pattern
        for file in os.listdir('audios'):
            if file.endswith(f".{audio_format}"):
                final_output_path = os.path.join('audios', file)
                break
    
    return final_output_path

# Example usage:
# downloader('https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'mp3', 'my_audio')
# downloader('https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'mp3')


# Function to read the markdown file and convert it to HTML
def read_markdown_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        md_content = file.read()
    html_content = markdown.markdown(md_content)
    return html_content

# Path to your markdown file
markdown_file_path = 'notread.md'

# Read and convert the markdown content
html_content = read_markdown_file(markdown_file_path)

# Read and convert the markdown content
html_content = read_markdown_file(markdown_file_path)

logschart = """
    ### Changelog:

    #### 2.0 - 2024-05-16

    ##### Added
      - **change webui**: change the old webui to new version!
    - **Directory Check**: Added a check to ensure the `audios` directory exists before attempting to save files to it.
      - `os.makedirs('audios', exist_ok=True)`

    ##### Changed
    - **Output Template Placeholder**: Updated `outtmpl` to use a temporary placeholder for the file extension.
      - `outtmpl: f"audios/{audio_name}.%(ext)s"`
    - **File Renaming**: Added logic to rename the temporary output file to the final desired name and extension.
      - `temp_file = temp_output_path.replace('%(ext)s', audio_format)`
      - `final_output_path = f"audios/{audio_name}.{audio_format}`
      - `os.rename(temp_file, final_output_path)`
    - **Return Correct Path**: The `downloader` function now returns the final output path to ensure Gradio can find and load the audio file correctly.
      - `return final_output_path`

    ##### Fixed
    - **File Not Found Error**: Resolved the issue where the file could not be found due to incorrect output path handling.

    ### Summary of Changes

    1. **Ensured Directory Exists**: Added code to create the `audios` directory if it doesn't exist to prevent file saving errors.
    2. **Output Path Handling**: Modified the output template to use a placeholder for the file extension, which yt-dlp will replace with the actual extension upon downloading.
    3. **File Renaming Logic**: After downloading the audio, the code now renames the file to match the desired audio name and format.
    4. **Correct File Path Return**: The correct file path is now returned from the `downloader` function, ensuring that the Gradio `gr.Audio` component can properly display and play the downloaded audio file.

    These changes collectively ensure that the audio file is downloaded correctly, renamed appropriately, and made accessible to the Gradio interface for playback.
    """


with gr.Blocks() as demo:
    gr.Markdown("# YouTube Downloader 2.0")
    with gr.Row():
        video_url = gr.Textbox(label="YouTube video link")
    with gr.Row():
        audio_name = gr.Textbox(label="Audio name of YouTube audio", info="this is optional")
        audio_format = gr.Radio(["wav", "flac", "mp3"], label="Select the output format", value=" wav")
    with gr.Row():
        output = gr.Audio(label="Output")
    with gr.Row():
        download_button = gr.Button("Download")
        download_button.click(downloader, inputs=[video_url, audio_format, audio_name], outputs=output)
    with gr.Group():
        with gr.Row():
            gr.Markdown(logschart)
            with gr.Row():
                gr.HTML(html_content)
                
demo.launch()