Upload 3 files
Browse files- Dockerfile +25 -0
- app.py +123 -0
- requirements.txt +2 -0
Dockerfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Install ffmpeg dengan support H.265
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
ffmpeg \
|
| 8 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 9 |
+
|
| 10 |
+
# Copy requirements dan install dependencies
|
| 11 |
+
COPY requirements.txt .
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Copy aplikasi
|
| 15 |
+
COPY app.py .
|
| 16 |
+
|
| 17 |
+
# Expose port untuk Gradio
|
| 18 |
+
EXPOSE 7860
|
| 19 |
+
|
| 20 |
+
# Set environment variables
|
| 21 |
+
ENV GRADIO_SERVER_NAME="0.0.0.0"
|
| 22 |
+
ENV GRADIO_SERVER_PORT=7860
|
| 23 |
+
|
| 24 |
+
# Jalankan aplikasi
|
| 25 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
def get_video_info(input_path):
|
| 8 |
+
"""Ambil informasi video"""
|
| 9 |
+
try:
|
| 10 |
+
probe = ffmpeg.probe(input_path)
|
| 11 |
+
video_info = next(s for s in probe['streams'] if s['codec_type'] == 'video')
|
| 12 |
+
return video_info
|
| 13 |
+
except:
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
def compress_video(input_video, quality_preset, target_size):
|
| 17 |
+
"""Kompres video dengan kualitas terjaga"""
|
| 18 |
+
if input_video is None:
|
| 19 |
+
return None, "Silakan upload video terlebih dahulu"
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
# Buat file temporary untuk output
|
| 23 |
+
output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
|
| 24 |
+
|
| 25 |
+
# Dapatkan info video
|
| 26 |
+
video_info = get_video_info(input_video)
|
| 27 |
+
if not video_info:
|
| 28 |
+
return None, "Gagal membaca informasi video"
|
| 29 |
+
|
| 30 |
+
original_size = os.path.getsize(input_video) / (1024 * 1024) # MB
|
| 31 |
+
|
| 32 |
+
# Tentukan CRF berdasarkan preset
|
| 33 |
+
crf_values = {
|
| 34 |
+
"Kualitas Maksimal (ukuran lebih besar)": 18,
|
| 35 |
+
"Seimbang (recommended)": 23,
|
| 36 |
+
"Ukuran Kecil (kualitas masih bagus)": 28
|
| 37 |
+
}
|
| 38 |
+
crf = crf_values.get(quality_preset, 23)
|
| 39 |
+
|
| 40 |
+
# Build ffmpeg command dengan parameter optimal
|
| 41 |
+
stream = ffmpeg.input(input_video)
|
| 42 |
+
|
| 43 |
+
# Video stream dengan H.265 untuk kompresi lebih baik
|
| 44 |
+
video = stream.video.filter('scale', 'iw:ih').filter('format', 'yuv420p')
|
| 45 |
+
audio = stream.audio
|
| 46 |
+
|
| 47 |
+
# Output dengan parameter optimal
|
| 48 |
+
output = ffmpeg.output(
|
| 49 |
+
video, audio, output_path,
|
| 50 |
+
vcodec='libx265',
|
| 51 |
+
crf=crf,
|
| 52 |
+
preset='medium',
|
| 53 |
+
acodec='aac',
|
| 54 |
+
audio_bitrate='128k',
|
| 55 |
+
**{'x265-params': 'log-level=error'}
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Jalankan konversi
|
| 59 |
+
ffmpeg.run(output, overwrite_output=True, quiet=True)
|
| 60 |
+
|
| 61 |
+
# Hitung hasil kompresi
|
| 62 |
+
compressed_size = os.path.getsize(output_path) / (1024 * 1024)
|
| 63 |
+
reduction = ((original_size - compressed_size) / original_size) * 100
|
| 64 |
+
|
| 65 |
+
result_text = f"""
|
| 66 |
+
✅ Konversi berhasil!
|
| 67 |
+
|
| 68 |
+
📊 Hasil:
|
| 69 |
+
• Ukuran awal: {original_size:.2f} MB
|
| 70 |
+
• Ukuran akhir: {compressed_size:.2f} MB
|
| 71 |
+
• Pengurangan: {reduction:.1f}%
|
| 72 |
+
|
| 73 |
+
Video sudah dioptimasi dengan codec H.265 untuk kualitas maksimal dengan ukuran minimal.
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
return output_path, result_text
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return None, f"Terjadi kesalahan: {str(e)}"
|
| 80 |
+
|
| 81 |
+
# Buat interface
|
| 82 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 83 |
+
gr.Markdown("""
|
| 84 |
+
# Video Optimizer
|
| 85 |
+
|
| 86 |
+
Kompres video kamu tanpa mengurangi kualitas. Cocok untuk kirim via WhatsApp atau media sosial lainnya.
|
| 87 |
+
""")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column():
|
| 91 |
+
input_video = gr.Video(label="Upload Video")
|
| 92 |
+
|
| 93 |
+
quality_preset = gr.Radio(
|
| 94 |
+
choices=[
|
| 95 |
+
"Kualitas Maksimal (ukuran lebih besar)",
|
| 96 |
+
"Seimbang (recommended)",
|
| 97 |
+
"Ukuran Kecil (kualitas masih bagus)"
|
| 98 |
+
],
|
| 99 |
+
value="Seimbang (recommended)",
|
| 100 |
+
label="Pilih Preset"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
compress_btn = gr.Button("Proses Video", variant="primary")
|
| 104 |
+
|
| 105 |
+
with gr.Column():
|
| 106 |
+
output_video = gr.Video(label="Video Hasil")
|
| 107 |
+
result_text = gr.Textbox(label="Status", lines=8)
|
| 108 |
+
|
| 109 |
+
compress_btn.click(
|
| 110 |
+
fn=compress_video,
|
| 111 |
+
inputs=[input_video, quality_preset, gr.State(None)],
|
| 112 |
+
outputs=[output_video, result_text]
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
gr.Markdown("""
|
| 116 |
+
### Tips:
|
| 117 |
+
- Gunakan preset "Seimbang" untuk hasil optimal
|
| 118 |
+
- Video akan dikonversi ke format H.265 yang lebih efisien
|
| 119 |
+
- Kualitas warna dan detail akan tetap terjaga
|
| 120 |
+
""")
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.16.0
|
| 2 |
+
ffmpeg-python==0.2.0
|