Upload 2 files
Browse files- app.py +84 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
def compress_video(input_path, target_bitrate, target_width):
|
| 7 |
+
# Probe the original video to get resolution
|
| 8 |
+
probe = ffmpeg.probe(input_path)
|
| 9 |
+
video_streams = [stream for stream in probe['streams'] if stream['codec_type'] == 'video']
|
| 10 |
+
if not video_streams:
|
| 11 |
+
raise ValueError("No video stream found in the uploaded file.")
|
| 12 |
+
|
| 13 |
+
orig_width = int(video_streams[0]['width'])
|
| 14 |
+
orig_height = int(video_streams[0]['height'])
|
| 15 |
+
|
| 16 |
+
# Calculate new height to maintain aspect ratio
|
| 17 |
+
new_width = int(target_width)
|
| 18 |
+
new_height = int(orig_height * new_width / orig_width)
|
| 19 |
+
# Ensure height is even (ffmpeg requirement)
|
| 20 |
+
if new_height % 2 != 0:
|
| 21 |
+
new_height += 1
|
| 22 |
+
|
| 23 |
+
# Create a temporary output file
|
| 24 |
+
out_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 25 |
+
output_path = out_file.name
|
| 26 |
+
out_file.close()
|
| 27 |
+
|
| 28 |
+
# Run ffmpeg to compress and resize
|
| 29 |
+
(
|
| 30 |
+
ffmpeg
|
| 31 |
+
.input(input_path)
|
| 32 |
+
.output(
|
| 33 |
+
output_path,
|
| 34 |
+
video_bitrate=f'{target_bitrate}k',
|
| 35 |
+
vf=f'scale={new_width}:{new_height}',
|
| 36 |
+
audio_bitrate='128k',
|
| 37 |
+
preset='medium'
|
| 38 |
+
)
|
| 39 |
+
.run(overwrite_output=True)
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
return output_path
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def process_video(input_path, target_bitrate, target_width):
|
| 46 |
+
# Compress the video and return the path for download
|
| 47 |
+
compressed_path = compress_video(input_path, target_bitrate, target_width)
|
| 48 |
+
return compressed_path
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def main():
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("# Video Compressor App")
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
input_video = gr.Video(label="Upload Video")
|
| 57 |
+
target_bitrate = gr.Slider(
|
| 58 |
+
minimum=100,
|
| 59 |
+
maximum=5000,
|
| 60 |
+
step=100,
|
| 61 |
+
value=1000,
|
| 62 |
+
label="Target Video Bitrate (kbps)"
|
| 63 |
+
)
|
| 64 |
+
target_width = gr.Slider(
|
| 65 |
+
minimum=240,
|
| 66 |
+
maximum=1920,
|
| 67 |
+
step=16,
|
| 68 |
+
value=640,
|
| 69 |
+
label="Target Width (px)"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
compress_button = gr.Button("Compress")
|
| 73 |
+
output_video = gr.File(label="Download Compressed Video")
|
| 74 |
+
|
| 75 |
+
compress_button.click(
|
| 76 |
+
fn=process_video,
|
| 77 |
+
inputs=[input_video, target_bitrate, target_width],
|
| 78 |
+
outputs=output_video
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
demo.launch()
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
ffmpeg-python
|