launch-calcium commited on
Commit
7545ca5
Β·
verified Β·
1 Parent(s): 4158be7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -28
app.py CHANGED
@@ -1,43 +1,120 @@
1
  import gradio as gr
2
  import subprocess
3
  import os
 
4
 
5
- # Install yt-dlp (this runs once when the space starts)
6
- subprocess.run(["pip", "install", "yt-dlp"], check=True)
 
7
 
8
- import yt_dlp
9
-
10
- def download_video(url, output_format):
11
  try:
12
- ydl_opts = {
13
- 'format': output_format or 'best',
14
- 'outtmpl': '/tmp/%(title)s.%(ext)s',
15
- }
16
-
17
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
18
- info = ydl.extract_info(url, download=True)
19
- filename = ydl.prepare_filename(info)
20
- return f"βœ“ Downloaded: {info['title']}", filename
 
 
 
21
 
22
  except Exception as e:
23
  return f"βœ— Error: {str(e)}", None
24
 
25
- # Create Gradio interface
26
- with gr.Blocks() as demo:
27
- gr.Markdown("# YouTube Downloader with yt-dlp")
 
 
 
 
 
 
 
 
 
28
 
29
- with gr.Row():
30
- url_input = gr.Textbox(label="Video URL", placeholder="https://www.youtube.com/watch?v=...")
31
- format_input = gr.Textbox(label="Format (optional)", placeholder="best, worst, 18, etc.")
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- download_btn = gr.Button("Download", variant="primary")
34
- output_text = gr.Textbox(label="Status", interactive=False)
35
- output_file = gr.File(label="Downloaded File")
 
 
 
36
 
37
- download_btn.click(
38
- fn=download_video,
39
- inputs=[url_input, format_input],
40
- outputs=[output_text, output_file]
41
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  demo.launch()
 
1
  import gradio as gr
2
  import subprocess
3
  import os
4
+ from pathlib import Path
5
 
6
+ # Install ffmpeg (runs once when space starts)
7
+ subprocess.run(["apt-get", "update"], check=False)
8
+ subprocess.run(["apt-get", "install", "-y", "ffmpeg"], check=False)
9
 
10
+ def convert_audio(audio_file, output_format):
 
 
11
  try:
12
+ if audio_file is None:
13
+ return "βœ— No file uploaded", None
14
+
15
+ # Create output filename
16
+ input_path = audio_file
17
+ output_path = f"/tmp/output.{output_format}"
18
+
19
+ # Run FFmpeg command
20
+ cmd = ["ffmpeg", "-i", input_path, "-y", output_path]
21
+ subprocess.run(cmd, capture_output=True, check=True)
22
+
23
+ return f"βœ“ Converted to {output_format.upper()}", output_path
24
 
25
  except Exception as e:
26
  return f"βœ— Error: {str(e)}", None
27
 
28
+ def extract_audio(video_file):
29
+ try:
30
+ if video_file is None:
31
+ return "βœ— No file uploaded", None
32
+
33
+ output_path = "/tmp/extracted_audio.mp3"
34
+
35
+ # Extract audio from video
36
+ cmd = ["ffmpeg", "-i", video_file, "-q:a", "0", "-map", "a", "-y", output_path]
37
+ subprocess.run(cmd, capture_output=True, check=True)
38
+
39
+ return "βœ“ Audio extracted", output_path
40
 
41
+ except Exception as e:
42
+ return f"βœ— Error: {str(e)}", None
43
+
44
+ def compress_video(video_file, quality):
45
+ try:
46
+ if video_file is None:
47
+ return "βœ— No file uploaded", None
48
+
49
+ output_path = "/tmp/compressed_video.mp4"
50
+ crf_value = int(quality) # 0-51, lower = better quality
51
+
52
+ # Compress video
53
+ cmd = ["ffmpeg", "-i", video_file, "-crf", str(crf_value), "-y", output_path]
54
+ subprocess.run(cmd, capture_output=True, check=True)
55
+
56
+ return "βœ“ Video compressed", output_path
57
 
58
+ except Exception as e:
59
+ return f"βœ— Error: {str(e)}", None
60
+
61
+ # Create Gradio interface
62
+ with gr.Blocks(title="FFmpeg Tools") as demo:
63
+ gr.Markdown("# FFmpeg Media Tools")
64
 
65
+ with gr.Tabs():
66
+ # Tab 1: Audio Conversion
67
+ with gr.Tab("Audio Converter"):
68
+ gr.Markdown("Convert audio between formats")
69
+ audio_input = gr.Audio(label="Upload Audio", type="filepath")
70
+ format_select = gr.Dropdown(
71
+ choices=["mp3", "wav", "aac", "flac", "ogg"],
72
+ value="mp3",
73
+ label="Output Format"
74
+ )
75
+ audio_convert_btn = gr.Button("Convert", variant="primary")
76
+ audio_status = gr.Textbox(label="Status", interactive=False)
77
+ audio_output = gr.File(label="Download")
78
+
79
+ audio_convert_btn.click(
80
+ fn=convert_audio,
81
+ inputs=[audio_input, format_select],
82
+ outputs=[audio_status, audio_output]
83
+ )
84
+
85
+ # Tab 2: Extract Audio from Video
86
+ with gr.Tab("Extract Audio"):
87
+ gr.Markdown("Extract audio track from video files")
88
+ video_input = gr.Video(label="Upload Video")
89
+ extract_btn = gr.Button("Extract Audio", variant="primary")
90
+ extract_status = gr.Textbox(label="Status", interactive=False)
91
+ extract_output = gr.File(label="Download MP3")
92
+
93
+ extract_btn.click(
94
+ fn=extract_audio,
95
+ inputs=[video_input],
96
+ outputs=[extract_status, extract_output]
97
+ )
98
+
99
+ # Tab 3: Video Compression
100
+ with gr.Tab("Video Compressor"):
101
+ gr.Markdown("Compress video files (lower CRF = better quality)")
102
+ video_comp_input = gr.Video(label="Upload Video")
103
+ quality_slider = gr.Slider(
104
+ minimum=18,
105
+ maximum=51,
106
+ value=28,
107
+ step=1,
108
+ label="Quality (CRF: 18=high, 51=low)"
109
+ )
110
+ compress_btn = gr.Button("Compress", variant="primary")
111
+ compress_status = gr.Textbox(label="Status", interactive=False)
112
+ compress_output = gr.File(label="Download")
113
+
114
+ compress_btn.click(
115
+ fn=compress_video,
116
+ inputs=[video_comp_input, quality_slider],
117
+ outputs=[compress_status, compress_output]
118
+ )
119
 
120
  demo.launch()