Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
#This was made with ChatGPT because my dumbass can't actually code
|
2 |
import os
|
3 |
import subprocess
|
4 |
import uuid
|
@@ -35,15 +34,14 @@ def convert_video(use_youtube, youtube_url, video_file, downscale, faster, use_m
|
|
35 |
else:
|
36 |
output_filename = f"{uuid.uuid4()}.mp4"
|
37 |
output_path = os.path.join(CONVERTED_FOLDER, output_filename)
|
38 |
-
|
39 |
if use_youtube:
|
40 |
if not youtube_url:
|
41 |
return "Error: YouTube URL is required.", None
|
42 |
|
43 |
-
# Determine quality: if downscale is ticked use low quality; otherwise, use "-f b" for best quality.
|
44 |
quality = 'worstaudio' if audio_only and downscale else (
|
45 |
'worstvideo+worstaudio' if downscale else 'b')
|
46 |
-
|
47 |
yt_uuid = str(uuid.uuid4())
|
48 |
yt_input_filename = yt_uuid + ".%(ext)s"
|
49 |
yt_input_path = os.path.join(UPLOAD_FOLDER, yt_input_filename)
|
@@ -53,47 +51,51 @@ def convert_video(use_youtube, youtube_url, video_file, downscale, faster, use_m
|
|
53 |
except subprocess.CalledProcessError as e:
|
54 |
return f"An error occurred during YouTube download: {e}", None
|
55 |
|
56 |
-
# Locate the downloaded file (yt-dlp replaces %(ext)s with the actual extension).
|
57 |
pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
|
58 |
downloaded_files = glob.glob(pattern)
|
59 |
if not downloaded_files:
|
60 |
return "Failed to download YouTube video.", None
|
61 |
input_path = downloaded_files[0]
|
62 |
else:
|
63 |
-
# File upload branch.
|
64 |
if not video_file:
|
65 |
return "Error: No video file provided.", None
|
66 |
-
# Check if the provided file path exists.
|
67 |
if os.path.exists(video_file):
|
68 |
ext = os.path.splitext(video_file)[1]
|
69 |
input_filename = f"{uuid.uuid4()}{ext}"
|
70 |
input_path = os.path.join(UPLOAD_FOLDER, input_filename)
|
71 |
-
# Copy the file to our uploads folder.
|
72 |
shutil.copy2(video_file, input_path)
|
73 |
else:
|
74 |
-
# If the file doesn't exist (perhaps already in a persistent location), reuse it.
|
75 |
input_path = video_file
|
76 |
|
77 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
ffmpeg_cmd = ['./ffmpeg', '-hwaccel', accel, '-y', '-i', input_path] + extra.split()
|
79 |
-
|
80 |
if faster:
|
81 |
ffmpeg_cmd.extend(['-preset', 'ultrafast'])
|
82 |
-
|
83 |
if audio_only:
|
84 |
ffmpeg_cmd.extend(['-vn'])
|
85 |
-
|
86 |
-
|
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.
|
@@ -106,6 +108,9 @@ with gr.Blocks() as demo:
|
|
106 |
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube URL here")
|
107 |
video_file = gr.File(label="Upload Video")
|
108 |
|
|
|
|
|
|
|
109 |
with gr.Row():
|
110 |
use_mp3 = gr.Checkbox(label="Use MP3 (video audio compression)", value=False)
|
111 |
audio_only = gr.Checkbox(label="Only Audio (no video)", value=False)
|
|
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
import uuid
|
|
|
34 |
else:
|
35 |
output_filename = f"{uuid.uuid4()}.mp4"
|
36 |
output_path = os.path.join(CONVERTED_FOLDER, output_filename)
|
37 |
+
|
38 |
if use_youtube:
|
39 |
if not youtube_url:
|
40 |
return "Error: YouTube URL is required.", None
|
41 |
|
|
|
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"
|
47 |
yt_input_path = os.path.join(UPLOAD_FOLDER, yt_input_filename)
|
|
|
51 |
except subprocess.CalledProcessError as e:
|
52 |
return f"An error occurred during YouTube download: {e}", None
|
53 |
|
|
|
54 |
pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
|
55 |
downloaded_files = glob.glob(pattern)
|
56 |
if not downloaded_files:
|
57 |
return "Failed to download YouTube video.", None
|
58 |
input_path = downloaded_files[0]
|
59 |
else:
|
|
|
60 |
if not video_file:
|
61 |
return "Error: No video file provided.", None
|
|
|
62 |
if os.path.exists(video_file):
|
63 |
ext = os.path.splitext(video_file)[1]
|
64 |
input_filename = f"{uuid.uuid4()}{ext}"
|
65 |
input_path = os.path.join(UPLOAD_FOLDER, input_filename)
|
|
|
66 |
shutil.copy2(video_file, input_path)
|
67 |
else:
|
|
|
68 |
input_path = video_file
|
69 |
|
70 |
+
# Handle optional downscale first
|
71 |
+
if downscale and not audio_only:
|
72 |
+
downscaled_path = os.path.join(UPLOAD_FOLDER, f"downscaled_{uuid.uuid4()}.mp4")
|
73 |
+
downscale_cmd = ['./ffmpeg', '-hwaccel', accel, '-y', '-i', input_path,
|
74 |
+
'-vf', 'scale=144:-2', '-c:v', 'libx264', '-preset', 'ultrafast', '-crf', '40',
|
75 |
+
'-an', downscaled_path]
|
76 |
+
try:
|
77 |
+
subprocess.run(downscale_cmd, check=True)
|
78 |
+
except subprocess.CalledProcessError as e:
|
79 |
+
return f"An error occurred during downscaling: {e}", None
|
80 |
+
input_path = downscaled_path # Use this for next pass
|
81 |
+
|
82 |
+
# Now do the final encode
|
83 |
ffmpeg_cmd = ['./ffmpeg', '-hwaccel', accel, '-y', '-i', input_path] + extra.split()
|
84 |
+
|
85 |
if faster:
|
86 |
ffmpeg_cmd.extend(['-preset', 'ultrafast'])
|
87 |
+
|
88 |
if audio_only:
|
89 |
ffmpeg_cmd.extend(['-vn'])
|
90 |
+
configure_audio(ffmpeg_cmd, use_mp3)
|
91 |
+
|
|
|
|
|
92 |
ffmpeg_cmd.append(output_path)
|
93 |
+
|
94 |
try:
|
95 |
subprocess.run(ffmpeg_cmd, check=True)
|
96 |
except subprocess.CalledProcessError as e:
|
97 |
return f"An error occurred during conversion: {e}", None
|
98 |
|
|
|
99 |
return output_path, output_path
|
100 |
|
101 |
# Create the Gradio interface.
|
|
|
108 |
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube URL here")
|
109 |
video_file = gr.File(label="Upload Video")
|
110 |
|
111 |
+
with gr.Row():
|
112 |
+
downscale = gr.Checkbox(label="Downscale Video (144p)", value=False)
|
113 |
+
faster = gr.Checkbox(label="Faster Conversion (more pixelated)", value=False)
|
114 |
with gr.Row():
|
115 |
use_mp3 = gr.Checkbox(label="Use MP3 (video audio compression)", value=False)
|
116 |
audio_only = gr.Checkbox(label="Only Audio (no video)", value=False)
|