File size: 2,528 Bytes
7f9ec21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from moviepy.editor import VideoFileClip
import os

def video_to_mp3(video_path, bitrate):
    if video_path is None:
        return None, None, gr.Info("Please upload a video file.")
    
    try:
        # Read the video file
        video = VideoFileClip(video_path)
        
        # Check if the video has an audio track
        if video.audio is None:
            video.close()
            return None, None, gr.Warning("The uploaded video does not contain an audio track.")
        
        # Get the filename without extension
        base_name = os.path.splitext(os.path.basename(video_path))[0]
        
        # Set the output MP3 file path
        audio_path = os.path.join(os.path.dirname(video_path), f"{base_name}.mp3")
        
        # Extract audio and save as MP3 file with specified bitrate
        video.audio.write_audiofile(audio_path, bitrate=bitrate)
        
        # Explicitly release resources
        video.close()
        
        return audio_path, audio_path, gr.Info("Conversion successful!")
    except Exception as e:
        return None, None, gr.Error(f"Error processing video file: {str(e)}")

# Create Gradio interface
iface = gr.Interface(
    fn=video_to_mp3,
    inputs=[
        gr.Video(label="Upload Video File"),
        gr.Dropdown(["128k", "192k", "256k"], value="192k", label="Bitrate")
    ],
    outputs=[
        gr.Audio(type="filepath", label="Preview MP3"),
        gr.File(label="Download MP3 File"),
        gr.Markdown(label="Status")
    ],
    title="Convert Video to MP3 | Free Online Video to Audio Converter",
    description="Upload a video file, choose the bitrate, convert it to an MP3 audio file, and download.",
    article="""
    ## How to Use
    1. **Upload Video File**: Click the "Upload Video File" button and select the video you want to convert.
    2. **Choose Bitrate**: Select the desired bitrate from the dropdown menu. Higher bitrates will produce better audio quality but larger file sizes.
    3. **Convert**: Once the video is uploaded and the bitrate is selected, the conversion process will start automatically.
    4. **Preview and Download**: After conversion, you can preview the MP3 file and download it to your device.
    
    Note: The video must contain an audio track for conversion to be successful.
    """,
    analytics_enabled=False,
    allow_flagging="never"  # Disable user flagging
).queue(default_concurrency_limit=4, max_size=20)

# Launch Gradio app
if __name__ == "__main__":
    iface.launch()