Spaces:
Paused
Paused
File size: 2,850 Bytes
485608a faa2bdb c8942d9 faa2bdb 7a80d3a faa2bdb 5296733 faa2bdb 40446f2 d47c1c5 40446f2 ec46e62 701ed9a b7d9c54 131761b 06412df 6f1f7b0 1819951 6f1f7b0 1819951 6f1f7b0 c137773 6f1f7b0 785f173 6f1f7b0 4c21960 6f1f7b0 3cb887f 6f1f7b0 3cb887f 7a80d3a 2024db3 3cb887f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
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")
def get_aspect_ratio(video_file):
# Get the path to the ffprobe binary
ffprobe_path = ffmpeg.probe_path()
# Get the command to get the aspect ratio of the video
ffprobe_command = f"{ffprobe_path} -show_entries stream=width,height -of compact=p=0:nk=1 {video_file}"
# Run the command and get the output
output = subprocess.check_output(ffprobe_command, shell=True).decode("utf-8")
# Get the aspect ratio from the output
aspect_ratio = output.split()[0].split(":")[1]
return aspect_ratio
def convert_video(video_file: File, quality, aspect_ratio):
if video_file is None:
video_file = Path(video_url)
# Get the original aspect ratio of the video
original_aspect_ratio = get_aspect_ratio(video_file)
# Check if the aspect ratio is already set
if aspect_ratio == original_aspect_ratio:
return video_file
# Set the aspect ratio
ffmpeg_command = f"ffmpeg -i {video_file} -c:v libx264 -crf {quality} -f mp4 -aspect {aspect_ratio} {video_file}"
try:
subprocess.run(ffmpeg_command, shell=True, timeout=10)
except subprocess.TimeoutExpired:
print("ffmpeg timed out")
return None
except FileNotFoundError:
print("ffmpeg is not installed.")
return None
for i in range(10):
if os.path.exists(video_file):
break
else:
time.sleep(1)
# The file has now been converted, so we can display it in the output viewer
if os.path.exists(video_file):
gr.components.Video(video_file)
else:
print("File does not exist.")
# Create a temporary file to store the download link
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(output_path.encode())
download_link = f.name
# Add a button to download the file
gr.components.HTML("""
<a href="{}" download="downloaded_file.mp4">Download</a>
""".format(download_link))
# Convert the video file to M3U8 format
m3u8_file = Path(video_file.stem + ".m3u8")
ffmpeg_command = f"ffmpeg -i {video_file} -c:v libx264 -crf {quality} -f hls -segment_time 10 {m3u8_file}"
try:
subprocess.run(ffmpeg_command, shell=True, timeout=10)
except subprocess.TimeoutExpired:
print("ffmpeg timed out")
return None
except FileNotFoundError:
print("ffmpeg is not installed.")
return None
return m3u8_file
from gradio import outputs
gr.Interface(convert_video, inputs=[video_file, quality], outputs=[outputs.Video(), outputs.File()]).launch(share=False)
|