innoai commited on
Commit
7f9ec21
·
verified ·
1 Parent(s): a75d8a8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from moviepy.editor import VideoFileClip
3
+ import os
4
+
5
+ def video_to_mp3(video_path, bitrate):
6
+ if video_path is None:
7
+ return None, None, gr.Info("Please upload a video file.")
8
+
9
+ try:
10
+ # Read the video file
11
+ video = VideoFileClip(video_path)
12
+
13
+ # Check if the video has an audio track
14
+ if video.audio is None:
15
+ video.close()
16
+ return None, None, gr.Warning("The uploaded video does not contain an audio track.")
17
+
18
+ # Get the filename without extension
19
+ base_name = os.path.splitext(os.path.basename(video_path))[0]
20
+
21
+ # Set the output MP3 file path
22
+ audio_path = os.path.join(os.path.dirname(video_path), f"{base_name}.mp3")
23
+
24
+ # Extract audio and save as MP3 file with specified bitrate
25
+ video.audio.write_audiofile(audio_path, bitrate=bitrate)
26
+
27
+ # Explicitly release resources
28
+ video.close()
29
+
30
+ return audio_path, audio_path, gr.Info("Conversion successful!")
31
+ except Exception as e:
32
+ return None, None, gr.Error(f"Error processing video file: {str(e)}")
33
+
34
+ # Create Gradio interface
35
+ iface = gr.Interface(
36
+ fn=video_to_mp3,
37
+ inputs=[
38
+ gr.Video(label="Upload Video File"),
39
+ gr.Dropdown(["128k", "192k", "256k"], value="192k", label="Bitrate")
40
+ ],
41
+ outputs=[
42
+ gr.Audio(type="filepath", label="Preview MP3"),
43
+ gr.File(label="Download MP3 File"),
44
+ gr.Markdown(label="Status")
45
+ ],
46
+ title="Convert Video to MP3 | Free Online Video to Audio Converter",
47
+ description="Upload a video file, choose the bitrate, convert it to an MP3 audio file, and download.",
48
+ article="""
49
+ ## How to Use
50
+ 1. **Upload Video File**: Click the "Upload Video File" button and select the video you want to convert.
51
+ 2. **Choose Bitrate**: Select the desired bitrate from the dropdown menu. Higher bitrates will produce better audio quality but larger file sizes.
52
+ 3. **Convert**: Once the video is uploaded and the bitrate is selected, the conversion process will start automatically.
53
+ 4. **Preview and Download**: After conversion, you can preview the MP3 file and download it to your device.
54
+
55
+ Note: The video must contain an audio track for conversion to be successful.
56
+ """,
57
+ analytics_enabled=False,
58
+ allow_flagging="never" # Disable user flagging
59
+ ).queue(default_concurrency_limit=4, max_size=20)
60
+
61
+ # Launch Gradio app
62
+ if __name__ == "__main__":
63
+ iface.launch()