Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,22 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import subprocess
|
| 4 |
import tempfile
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
from moviepy.editor import VideoFileClip
|
| 7 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def download_file(url, destination):
|
| 10 |
"""Downloads a file from a url to a destination."""
|
| 11 |
response = requests.get(url)
|
| 12 |
-
response.raise_for_status()
|
| 13 |
with open(destination, 'wb') as f:
|
| 14 |
f.write(response.content)
|
| 15 |
|
| 16 |
-
from urllib.parse import urlparse
|
| 17 |
-
|
| 18 |
def get_input_path(video_file, video_url, temp_dir):
|
| 19 |
"""Returns the path to the video file, downloading it if necessary."""
|
| 20 |
if video_file is not None:
|
|
@@ -28,10 +30,9 @@ def get_input_path(video_file, video_url, temp_dir):
|
|
| 28 |
else:
|
| 29 |
raise ValueError("No input was provided.")
|
| 30 |
|
| 31 |
-
|
| 32 |
def get_output_path(input_path, temp_dir):
|
| 33 |
"""Returns the path to the output file, creating it if necessary."""
|
| 34 |
-
output_path = temp_dir / (input_path.stem + ".m3u8")
|
| 35 |
return output_path
|
| 36 |
|
| 37 |
def get_aspect_ratio(input_path, aspect_ratio):
|
|
@@ -43,28 +44,39 @@ def get_aspect_ratio(input_path, aspect_ratio):
|
|
| 43 |
|
| 44 |
def convert_video(video_file, quality, aspect_ratio, video_url):
|
| 45 |
"""Converts a video to HLS format, adjusting the quality and aspect ratio as necessary."""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
)
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except subprocess.TimeoutExpired:
|
|
|
|
| 61 |
return "ffmpeg command timed out."
|
| 62 |
except FileNotFoundError:
|
|
|
|
| 63 |
return "ffmpeg is not installed."
|
| 64 |
except Exception as e:
|
|
|
|
| 65 |
return f"An error occurred: {str(e)}"
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def main():
|
| 70 |
video_file = gr.inputs.File(label="Video File", type="file")
|
|
@@ -80,5 +92,4 @@ def main():
|
|
| 80 |
gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='file').launch()
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
main()
|
| 84 |
-
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import shutil
|
|
|
|
| 3 |
import tempfile
|
| 4 |
+
import subprocess
|
| 5 |
from pathlib import Path
|
| 6 |
from moviepy.editor import VideoFileClip
|
| 7 |
import gradio as gr
|
| 8 |
+
import requests
|
| 9 |
+
from urllib.parse import urlparse
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
|
| 13 |
def download_file(url, destination):
|
| 14 |
"""Downloads a file from a url to a destination."""
|
| 15 |
response = requests.get(url)
|
| 16 |
+
response.raise_for_status()
|
| 17 |
with open(destination, 'wb') as f:
|
| 18 |
f.write(response.content)
|
| 19 |
|
|
|
|
|
|
|
| 20 |
def get_input_path(video_file, video_url, temp_dir):
|
| 21 |
"""Returns the path to the video file, downloading it if necessary."""
|
| 22 |
if video_file is not None:
|
|
|
|
| 30 |
else:
|
| 31 |
raise ValueError("No input was provided.")
|
| 32 |
|
|
|
|
| 33 |
def get_output_path(input_path, temp_dir):
|
| 34 |
"""Returns the path to the output file, creating it if necessary."""
|
| 35 |
+
output_path = temp_dir / (Path(input_path).stem + ".m3u8")
|
| 36 |
return output_path
|
| 37 |
|
| 38 |
def get_aspect_ratio(input_path, aspect_ratio):
|
|
|
|
| 44 |
|
| 45 |
def convert_video(video_file, quality, aspect_ratio, video_url):
|
| 46 |
"""Converts a video to HLS format, adjusting the quality and aspect ratio as necessary."""
|
| 47 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 48 |
+
temp_dir = Path(temp_dir)
|
| 49 |
+
input_path = get_input_path(video_file, video_url, temp_dir)
|
| 50 |
+
output_path = get_output_path(input_path, temp_dir)
|
| 51 |
+
aspect_ratio = get_aspect_ratio(input_path, aspect_ratio)
|
| 52 |
|
| 53 |
+
ffmpeg_command = [
|
| 54 |
+
"ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
|
| 55 |
+
"-vf", f"scale=-1:720,setsar={aspect_ratio}", "-hls_time", "6",
|
| 56 |
+
"-hls_playlist_type", "vod", "-f", "hls", str(output_path)
|
| 57 |
+
]
|
|
|
|
| 58 |
|
| 59 |
try:
|
| 60 |
+
logging.info("Running command: %s", " ".join(ffmpeg_command))
|
| 61 |
+
subprocess.run(ffmpeg_command, check=True, timeout=600)
|
| 62 |
+
except subprocess.CalledProcessError:
|
| 63 |
+
logging.exception("ffmpeg command failed.")
|
| 64 |
+
return "ffmpeg command failed."
|
| 65 |
except subprocess.TimeoutExpired:
|
| 66 |
+
logging.exception("ffmpeg command timed out.")
|
| 67 |
return "ffmpeg command timed out."
|
| 68 |
except FileNotFoundError:
|
| 69 |
+
logging.exception("ffmpeg is not installed.")
|
| 70 |
return "ffmpeg is not installed."
|
| 71 |
except Exception as e:
|
| 72 |
+
logging.exception("An error occurred.")
|
| 73 |
return f"An error occurred: {str(e)}"
|
| 74 |
|
| 75 |
+
# Copy the output to a new temporary file that won't be deleted when the
|
| 76 |
+
# TemporaryDirectory context manager exits.
|
| 77 |
+
output_copy_path = shutil.copy2(output_path, tempfile.gettempdir())
|
| 78 |
+
|
| 79 |
+
return output_copy_path
|
| 80 |
|
| 81 |
def main():
|
| 82 |
video_file = gr.inputs.File(label="Video File", type="file")
|
|
|
|
| 92 |
gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='file').launch()
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
+
main()
|
|
|