Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
import subprocess
|
3 |
import uuid
|
4 |
import glob
|
|
|
5 |
import gradio as gr
|
6 |
|
7 |
# Set default acceleration and extra encoding options.
|
@@ -33,17 +34,13 @@ def convert_video(use_youtube, youtube_url, video_file, downscale, faster, use_m
|
|
33 |
output_filename = f"{uuid.uuid4()}.mp4"
|
34 |
output_path = os.path.join(CONVERTED_FOLDER, output_filename)
|
35 |
|
36 |
-
files_to_delete = [] # For temporary file cleanup.
|
37 |
-
|
38 |
if use_youtube:
|
39 |
if not youtube_url:
|
40 |
return "Error: YouTube URL is required.", None
|
41 |
|
42 |
-
# Determine quality
|
43 |
-
if downscale
|
44 |
-
|
45 |
-
else:
|
46 |
-
quality = 'b' # Use "-f b" for best quality if downscale is not ticked.
|
47 |
|
48 |
yt_uuid = str(uuid.uuid4())
|
49 |
yt_input_filename = yt_uuid + ".%(ext)s"
|
@@ -59,49 +56,44 @@ def convert_video(use_youtube, youtube_url, video_file, downscale, faster, use_m
|
|
59 |
downloaded_files = glob.glob(pattern)
|
60 |
if not downloaded_files:
|
61 |
return "Failed to download YouTube video.", None
|
62 |
-
|
63 |
-
files_to_delete.append(yt_input_file)
|
64 |
-
|
65 |
-
# Build FFmpeg command using the downloaded file.
|
66 |
-
ffmpeg_cmd = ['ffmpeg', '-hwaccel', accel, '-y', '-i', yt_input_file] + extra.split()
|
67 |
else:
|
68 |
# File upload branch.
|
69 |
if not video_file:
|
70 |
return "Error: No video file provided.", None
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
# Apply downscale if requested.
|
81 |
if downscale:
|
82 |
ffmpeg_cmd.extend(['-vf', 'scale=144:-2'])
|
83 |
-
# Apply faster conversion if selected.
|
84 |
if faster:
|
85 |
ffmpeg_cmd.extend(['-preset', 'ultrafast'])
|
86 |
|
87 |
-
# Configure audio encoding.
|
88 |
if audio_only:
|
89 |
ffmpeg_cmd.extend(['-vn'])
|
90 |
configure_audio(ffmpeg_cmd, use_mp3)
|
91 |
else:
|
92 |
configure_audio(ffmpeg_cmd, use_mp3)
|
93 |
|
94 |
-
# Append output file path.
|
95 |
ffmpeg_cmd.append(output_path)
|
96 |
|
97 |
-
# Run FFmpeg.
|
98 |
try:
|
99 |
subprocess.run(ffmpeg_cmd, check=True)
|
100 |
except subprocess.CalledProcessError as e:
|
101 |
return f"An error occurred during conversion: {e}", None
|
102 |
|
103 |
-
#
|
104 |
-
# Return output file path for preview and download.
|
105 |
return output_path, output_path
|
106 |
|
107 |
# Create the Gradio interface.
|
@@ -110,7 +102,7 @@ with gr.Blocks() as demo:
|
|
110 |
gr.Markdown("Upload a video or use a YouTube URL and adjust the options below.")
|
111 |
|
112 |
with gr.Row():
|
113 |
-
use_youtube = gr.Checkbox(label="Use YouTube URL
|
114 |
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube URL here")
|
115 |
video_file = gr.File(label="Upload Video")
|
116 |
|
|
|
2 |
import subprocess
|
3 |
import uuid
|
4 |
import glob
|
5 |
+
import shutil
|
6 |
import gradio as gr
|
7 |
|
8 |
# Set default acceleration and extra encoding options.
|
|
|
34 |
output_filename = f"{uuid.uuid4()}.mp4"
|
35 |
output_path = os.path.join(CONVERTED_FOLDER, output_filename)
|
36 |
|
|
|
|
|
37 |
if use_youtube:
|
38 |
if not youtube_url:
|
39 |
return "Error: YouTube URL is required.", None
|
40 |
|
41 |
+
# Determine quality: if downscale is ticked use low quality; otherwise, use "-f b" for best quality.
|
42 |
+
quality = 'worstaudio' if audio_only and downscale else (
|
43 |
+
'worstvideo+worstaudio' if downscale else 'b')
|
|
|
|
|
44 |
|
45 |
yt_uuid = str(uuid.uuid4())
|
46 |
yt_input_filename = yt_uuid + ".%(ext)s"
|
|
|
56 |
downloaded_files = glob.glob(pattern)
|
57 |
if not downloaded_files:
|
58 |
return "Failed to download YouTube video.", None
|
59 |
+
input_path = downloaded_files[0]
|
|
|
|
|
|
|
|
|
60 |
else:
|
61 |
# File upload branch.
|
62 |
if not video_file:
|
63 |
return "Error: No video file provided.", None
|
64 |
+
# Check if the provided file path exists.
|
65 |
+
if os.path.exists(video_file):
|
66 |
+
ext = os.path.splitext(video_file)[1]
|
67 |
+
input_filename = f"{uuid.uuid4()}{ext}"
|
68 |
+
input_path = os.path.join(UPLOAD_FOLDER, input_filename)
|
69 |
+
# Copy the file to our uploads folder.
|
70 |
+
shutil.copy2(video_file, input_path)
|
71 |
+
else:
|
72 |
+
# If the file doesn't exist (perhaps already in a persistent location), reuse it.
|
73 |
+
input_path = video_file
|
74 |
+
|
75 |
+
# Build the FFmpeg command.
|
76 |
+
ffmpeg_cmd = ['ffmpeg', '-hwaccel', accel, '-y', '-i', input_path] + extra.split()
|
77 |
|
|
|
78 |
if downscale:
|
79 |
ffmpeg_cmd.extend(['-vf', 'scale=144:-2'])
|
|
|
80 |
if faster:
|
81 |
ffmpeg_cmd.extend(['-preset', 'ultrafast'])
|
82 |
|
|
|
83 |
if audio_only:
|
84 |
ffmpeg_cmd.extend(['-vn'])
|
85 |
configure_audio(ffmpeg_cmd, use_mp3)
|
86 |
else:
|
87 |
configure_audio(ffmpeg_cmd, use_mp3)
|
88 |
|
|
|
89 |
ffmpeg_cmd.append(output_path)
|
90 |
|
|
|
91 |
try:
|
92 |
subprocess.run(ffmpeg_cmd, check=True)
|
93 |
except subprocess.CalledProcessError as e:
|
94 |
return f"An error occurred during conversion: {e}", None
|
95 |
|
96 |
+
# Return the same output for preview and download.
|
|
|
97 |
return output_path, output_path
|
98 |
|
99 |
# Create the Gradio interface.
|
|
|
102 |
gr.Markdown("Upload a video or use a YouTube URL and adjust the options below.")
|
103 |
|
104 |
with gr.Row():
|
105 |
+
use_youtube = gr.Checkbox(label="Use YouTube URL", value=False)
|
106 |
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube URL here")
|
107 |
video_file = gr.File(label="Upload Video")
|
108 |
|