Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,120 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
# Install
|
| 6 |
-
subprocess.run(["
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def download_video(url, output_format):
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
except Exception as e:
|
| 23 |
return f"β Error: {str(e)}", None
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 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()
|