Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import gradio as gr
|
|
8 |
accel = 'auto'
|
9 |
extra = '-crf 63 -c:v libx264' # default extra settings
|
10 |
|
11 |
-
#
|
12 |
UPLOAD_FOLDER = 'uploads'
|
13 |
CONVERTED_FOLDER = 'converted'
|
14 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
@@ -33,37 +33,37 @@ 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 = [] #
|
37 |
-
|
38 |
if use_youtube:
|
39 |
if not youtube_url:
|
40 |
return "Error: YouTube URL is required.", None
|
41 |
|
|
|
42 |
if downscale:
|
43 |
-
# Download the worst-quality version to disk.
|
44 |
quality = 'worstaudio' if audio_only else 'worstvideo+worstaudio'
|
45 |
-
yt_uuid = str(uuid.uuid4())
|
46 |
-
yt_input_filename = yt_uuid + ".%(ext)s"
|
47 |
-
yt_input_path = os.path.join(UPLOAD_FOLDER, yt_input_filename)
|
48 |
-
yt_dlp_cmd = ['yt-dlp', '-o', yt_input_path, '-f', quality, youtube_url]
|
49 |
-
try:
|
50 |
-
subprocess.run(yt_dlp_cmd, check=True)
|
51 |
-
except subprocess.CalledProcessError as e:
|
52 |
-
return f"An error occurred during YouTube download: {e}", None
|
53 |
-
|
54 |
-
# Locate the downloaded file (yt-dlp replaces %(ext)s with the actual extension).
|
55 |
-
pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
|
56 |
-
downloaded_files = glob.glob(pattern)
|
57 |
-
if not downloaded_files:
|
58 |
-
return "Failed to download YouTube video.", None
|
59 |
-
yt_input_file = downloaded_files[0]
|
60 |
-
files_to_delete.append(yt_input_file)
|
61 |
-
ffmpeg_cmd = ['ffmpeg', '-hwaccel', accel, '-y', '-i', yt_input_file] + extra.split()
|
62 |
else:
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
else:
|
68 |
# File upload branch.
|
69 |
if not video_file:
|
@@ -76,43 +76,35 @@ def convert_video(use_youtube, youtube_url, video_file, downscale, faster, use_m
|
|
76 |
os.rename(video_file, input_path)
|
77 |
files_to_delete.append(input_path)
|
78 |
ffmpeg_cmd = ['ffmpeg', '-hwaccel', accel, '-y', '-i', input_path] + extra.split()
|
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
|
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 |
ffmpeg_cmd.append(output_path)
|
95 |
|
96 |
# Run FFmpeg.
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
yt_proc.stdout.close()
|
102 |
-
yt_proc.wait()
|
103 |
-
except subprocess.CalledProcessError as e:
|
104 |
-
return f"An error occurred during conversion: {e}", None
|
105 |
-
else:
|
106 |
-
try:
|
107 |
-
subprocess.run(ffmpeg_cmd, check=True)
|
108 |
-
except subprocess.CalledProcessError as e:
|
109 |
-
return f"An error occurred during conversion: {e}", None
|
110 |
|
111 |
-
# (Optional cleanup
|
112 |
-
# Return
|
113 |
return output_path, output_path
|
114 |
|
115 |
-
# Create the Gradio interface
|
116 |
with gr.Blocks() as demo:
|
117 |
gr.Markdown("## Low Quality Video Inator")
|
118 |
gr.Markdown("Upload a video or use a YouTube URL and adjust the options below.")
|
|
|
8 |
accel = 'auto'
|
9 |
extra = '-crf 63 -c:v libx264' # default extra settings
|
10 |
|
11 |
+
# Fixed upload and conversion folders.
|
12 |
UPLOAD_FOLDER = 'uploads'
|
13 |
CONVERTED_FOLDER = 'converted'
|
14 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
|
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 based on whether downscale is ticked.
|
43 |
if downscale:
|
|
|
44 |
quality = 'worstaudio' if audio_only else 'worstvideo+worstaudio'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|
50 |
+
yt_input_path = os.path.join(UPLOAD_FOLDER, yt_input_filename)
|
51 |
+
yt_dlp_cmd = ['yt-dlp', '-o', yt_input_path, '-f', quality, youtube_url]
|
52 |
+
try:
|
53 |
+
subprocess.run(yt_dlp_cmd, check=True)
|
54 |
+
except subprocess.CalledProcessError as e:
|
55 |
+
return f"An error occurred during YouTube download: {e}", None
|
56 |
+
|
57 |
+
# Locate the downloaded file (yt-dlp replaces %(ext)s with the actual extension).
|
58 |
+
pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
|
59 |
+
downloaded_files = glob.glob(pattern)
|
60 |
+
if not downloaded_files:
|
61 |
+
return "Failed to download YouTube video.", None
|
62 |
+
yt_input_file = downloaded_files[0]
|
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:
|
|
|
76 |
os.rename(video_file, input_path)
|
77 |
files_to_delete.append(input_path)
|
78 |
ffmpeg_cmd = ['ffmpeg', '-hwaccel', accel, '-y', '-i', input_path] + extra.split()
|
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 |
+
# (Optional: cleanup temporary files here if desired.)
|
104 |
+
# Return output file path for preview and download.
|
105 |
return output_path, output_path
|
106 |
|
107 |
+
# Create the Gradio interface.
|
108 |
with gr.Blocks() as demo:
|
109 |
gr.Markdown("## Low Quality Video Inator")
|
110 |
gr.Markdown("Upload a video or use a YouTube URL and adjust the options below.")
|