Jeffgold's picture
Update app.py
785f173
raw
history blame
1.65 kB
import time
import os
import gradio as gr
import ffmpeg
import subprocess
from gradio import components
import tempfile
from pathlib import Path
from gradio import inputs
# Define File object
File = Path
# Define temp directory
temp_dir = tempfile.mkdtemp()
video_file = gr.inputs.File(label="Video File", type="file")
quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")
aspect_ratio = gr.inputs.Dropdown(choices=[
"1:1",
"4:3",
"3:2",
"5:4",
"16:9",
"21:9",
"1.85:1",
"2.35:1",
"3:1",
"360",
"9:16",
"16:9",
"2:1",
"1:2",
"9:1",
], label="Aspect Ratio", default="16:9")
video_url = gr.inputs.Textbox(label="Video URL")
def convert_video(video_file: File, quality, aspect_ratio, video_url):
if video_file is None:
video_file = Path(video_url)
# Get the file extension
file_extension = video_file.name.split(".")[-1]
# Remove the file extension
output_path = video_file.name[:-len(file_extension)]
# Add the .m3u8 file extension
output_path += ".m3u8"
# Set the output path to the temp directory
output_path = f"{temp_dir}/{output_path}"
ffmpeg_command = f"ffmpeg -i {video_file} -c:v libx264 -crf {quality} -f hls -aspect {aspect_ratio} {output_path}"
try:
subprocess.run(ffmpeg_command, shell=True)
except FileNotFoundError:
print("ffmpeg is not installed.")
return None
time.sleep(2)
# Return the output file directly
return components.Video(output_path)
from gradio import outputs
gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs=[outputs.Video()]).launch(share=False)