import gradio as gr import ffmpeg import os import requests from huggingface_hub import HfApi, HfFolder # Mengambil API Token dari Secrets Hugging Face HF_TOKEN = os.getenv("HF_TOKEN") # Mengambil token API dari Secrets HF_REPO_ID = os.getenv("HF_REPO_ID") # Mengambil ID repo dari Secrets api = HfApi() HfFolder.save_token(HF_TOKEN) # Fungsi untuk mengompresi video def compress_video(video_path, quality, mode): output_path = video_path.replace(".", "_compressed.") # Menentukan tingkat kompresi berdasarkan kualitas yang dipilih if quality == "High": crf = 28 # Ukuran lebih kecil, kualitas lebih rendah elif quality == "Medium": crf = 23 # Kualitas standar else: crf = 18 # Kualitas lebih baik, ukuran lebih besar # Mode High Resolution - Low Capacity if mode == "High Resolution - Low Capacity": preset = "slow" bitrate = "2M" else: preset = "medium" bitrate = "4M" # Kompresi video menggunakan FFmpeg try: ( ffmpeg .input(video_path) .output(output_path, vcodec="libx264", crf=crf, preset=preset, video_bitrate=bitrate) .run(overwrite_output=True) ) return output_path except Exception as e: return str(e) # Fungsi untuk mengunggah hasil ke Hugging Face Hub def upload_to_huggingface(file_path): filename = os.path.basename(file_path) api.upload_file( path_or_fileobj=file_path, path_in_repo=f"compressed_videos/{filename}", repo_id=HF_REPO_ID, repo_type="dataset", ) return f"https://huggingface.co/datasets/{HF_REPO_ID}/resolve/main/compressed_videos/{filename}" # Fungsi utama untuk menangani input video def process_video(video, quality, mode): input_video_path = video # Karena `type="filepath"`, video adalah path, bukan objek file # Periksa ukuran file (500MB - 12GB) file_size_gb = os.path.getsize(input_video_path) / (1024 ** 3) if file_size_gb < 1: return "āŒ Video terlalu kecil! Harus minimal 500 MB." elif file_size_gb > 12: return "āŒ Video terlalu besar! Maksimal 12 GB." # Kompresi video compressed_video_path = compress_video(input_video_path, quality, mode) if not os.path.exists(compressed_video_path): return "āŒ Gagal mengompresi video!" # Unggah ke Hugging Face download_url = upload_to_huggingface(compressed_video_path) return f"āœ… Video berhasil dikompresi!\n\nšŸ”— [Unduh Video Terkompresi]({download_url})" # UI dengan Gradio with gr.Blocks() as demo: gr.Markdown("# šŸŽ„ Video Compressor - Hugging Face API") gr.Markdown("Unggah video (1GB-12GB) dan pilih kualitas kompresi.") video_input = gr.File(label="Unggah Video (MP4, AVI, MOV)", type="filepath") quality = gr.Radio(["High", "Medium", "Low"], label="Tingkat Kompresi", value="Medium") mode = gr.Radio(["Normal", "High Resolution - Low Capacity"], label="Mode Kompresi", value="Normal") compress_button = gr.Button("Kompres Video") output_link = gr.Markdown("") compress_button.click(process_video, inputs=[video_input, quality, mode], outputs=output_link) # Menjalankan aplikasi if __name__ == "__main__": demo.launch()