Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ import glob
|
|
6 |
import shutil
|
7 |
import gradio as gr
|
8 |
|
9 |
-
os.system("chmod +x
|
10 |
|
11 |
accel = 'auto'
|
12 |
video_base_opts = ['-crf', '63', '-c:v', 'libx264', '-tune', 'zerolatency']
|
@@ -19,7 +19,6 @@ os.makedirs(CONVERTED_FOLDER, exist_ok=True)
|
|
19 |
async def run_subprocess(cmd, use_fdkaac=False):
|
20 |
env = os.environ.copy()
|
21 |
if use_fdkaac:
|
22 |
-
# Add your fdkaac lib path here if needed:
|
23 |
env["LD_LIBRARY_PATH"] = os.path.abspath("./") + ":" + env.get("LD_LIBRARY_PATH", "")
|
24 |
print(f"[DEBUG] Running command:\n{' '.join(cmd)}\n")
|
25 |
process = await asyncio.create_subprocess_exec(
|
@@ -35,34 +34,51 @@ async def run_subprocess(cmd, use_fdkaac=False):
|
|
35 |
print(f"[DEBUG] Command succeeded:\n{stdout.decode()}\n")
|
36 |
return stdout.decode(), stderr.decode()
|
37 |
|
38 |
-
async def convert_video_task(input_path, downscale, faster, use_mp3):
|
39 |
if use_mp3:
|
40 |
-
|
41 |
-
|
42 |
-
ffmpeg_cmd = [
|
43 |
'ffmpeg', '-y', '-i', input_path, '-vn',
|
44 |
-
'-c:a', 'libmp3lame', '-b:a', '8k', '-ar', '24000', '-ac', '1',
|
|
|
45 |
]
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
audio_wav = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.wav")
|
51 |
audio_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.m4a")
|
52 |
video_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
|
53 |
|
54 |
-
# Extract mono
|
55 |
await run_subprocess([
|
56 |
'ffmpeg', '-y', '-i', input_path, '-ac', '1', '-ar', '8000', audio_wav
|
57 |
])
|
58 |
|
59 |
-
# Compress WAV
|
60 |
await run_subprocess([
|
61 |
'./fdkaac', '-b', '1k', '-C', '-f', '2', '-G', '1', '-w', '8000',
|
62 |
'-o', audio_output, audio_wav
|
63 |
], use_fdkaac=True)
|
64 |
|
65 |
-
# Compress video
|
66 |
video_cmd = [
|
67 |
'ffmpeg', '-y', '-hwaccel', accel, '-i', input_path,
|
68 |
*video_base_opts, '-an', video_output
|
@@ -71,20 +87,23 @@ async def convert_video_task(input_path, downscale, faster, use_mp3):
|
|
71 |
video_cmd.extend(['-preset', 'ultrafast'])
|
72 |
await run_subprocess(video_cmd)
|
73 |
|
74 |
-
|
|
|
|
|
|
|
75 |
merged_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
|
76 |
await run_subprocess([
|
77 |
'ffmpeg', '-y', '-i', video_output, '-i', audio_output, '-c', 'copy', merged_output
|
78 |
])
|
79 |
|
80 |
-
# Cleanup
|
81 |
for f in [audio_wav, audio_output, video_output]:
|
82 |
try:
|
83 |
os.remove(f)
|
84 |
except FileNotFoundError:
|
85 |
pass
|
86 |
|
87 |
-
return
|
88 |
|
89 |
async def process_conversion(use_youtube, youtube_url, video_file, downscale, faster, use_mp3, audio_only):
|
90 |
try:
|
@@ -93,7 +112,7 @@ async def process_conversion(use_youtube, youtube_url, video_file, downscale, fa
|
|
93 |
return "Error: YouTube URL required.", None
|
94 |
yt_uuid = str(uuid.uuid4())
|
95 |
yt_out = os.path.join(UPLOAD_FOLDER, yt_uuid + ".%(ext)s")
|
96 |
-
yt_cmd = ['yt-dlp', '-o', yt_out, '-f',
|
97 |
await run_subprocess(yt_cmd)
|
98 |
pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
|
99 |
files = glob.glob(pattern)
|
@@ -107,28 +126,31 @@ async def process_conversion(use_youtube, youtube_url, video_file, downscale, fa
|
|
107 |
input_path = os.path.join(UPLOAD_FOLDER, f"{uuid.uuid4()}{ext}")
|
108 |
shutil.copy2(video_file.name, input_path)
|
109 |
|
110 |
-
audio_out, video_out = await convert_video_task(
|
|
|
|
|
111 |
|
112 |
if audio_only:
|
113 |
return audio_out, audio_out
|
114 |
-
|
115 |
-
return video_out, video_out
|
116 |
except Exception as e:
|
117 |
return f"Error: {str(e)}", None
|
118 |
|
119 |
def convert_video(*args):
|
120 |
return asyncio.run(process_conversion(*args))
|
121 |
|
|
|
122 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="rose")) as demo:
|
123 |
gr.Markdown("""
|
124 |
# π₯ **Low Quality Video Inator**
|
125 |
-
|
126 |
-
|
|
|
127 |
""")
|
128 |
|
129 |
with gr.Group():
|
130 |
with gr.Row():
|
131 |
-
use_youtube = gr.Checkbox(label="π Use YouTube URL (
|
132 |
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Paste YouTube URL here")
|
133 |
video_file = gr.File(label="π Upload Video File")
|
134 |
|
|
|
6 |
import shutil
|
7 |
import gradio as gr
|
8 |
|
9 |
+
os.system("chmod +x fdkaac") # Ensure fdkaac is executable
|
10 |
|
11 |
accel = 'auto'
|
12 |
video_base_opts = ['-crf', '63', '-c:v', 'libx264', '-tune', 'zerolatency']
|
|
|
19 |
async def run_subprocess(cmd, use_fdkaac=False):
|
20 |
env = os.environ.copy()
|
21 |
if use_fdkaac:
|
|
|
22 |
env["LD_LIBRARY_PATH"] = os.path.abspath("./") + ":" + env.get("LD_LIBRARY_PATH", "")
|
23 |
print(f"[DEBUG] Running command:\n{' '.join(cmd)}\n")
|
24 |
process = await asyncio.create_subprocess_exec(
|
|
|
34 |
print(f"[DEBUG] Command succeeded:\n{stdout.decode()}\n")
|
35 |
return stdout.decode(), stderr.decode()
|
36 |
|
37 |
+
async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only):
|
38 |
if use_mp3:
|
39 |
+
output_audio = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp3")
|
40 |
+
ffmpeg_audio_cmd = [
|
|
|
41 |
'ffmpeg', '-y', '-i', input_path, '-vn',
|
42 |
+
'-c:a', 'libmp3lame', '-b:a', '8k', '-ar', '24000', '-ac', '1',
|
43 |
+
output_audio
|
44 |
]
|
45 |
+
if audio_only:
|
46 |
+
# Audio-only MP3
|
47 |
+
await run_subprocess(ffmpeg_audio_cmd)
|
48 |
+
return output_audio, None
|
49 |
+
else:
|
50 |
+
# Video+MP3, no splitting; encode video with MP3 audio embedded
|
51 |
+
output_video = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
|
52 |
+
ffmpeg_video_cmd = [
|
53 |
+
'ffmpeg', '-y', '-hwaccel', accel, '-i', input_path,
|
54 |
+
*video_base_opts
|
55 |
+
]
|
56 |
+
if faster:
|
57 |
+
ffmpeg_video_cmd.extend(['-preset', 'ultrafast'])
|
58 |
+
ffmpeg_video_cmd += [
|
59 |
+
'-c:a', 'libmp3lame', '-b:a', '8k', '-ar', '24000', '-ac', '1',
|
60 |
+
output_video
|
61 |
+
]
|
62 |
+
await run_subprocess(ffmpeg_video_cmd)
|
63 |
+
return None, output_video
|
64 |
+
|
65 |
+
# fdkaac path: Split, compress audio, merge
|
66 |
audio_wav = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.wav")
|
67 |
audio_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.m4a")
|
68 |
video_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
|
69 |
|
70 |
+
# Extract mono WAV audio
|
71 |
await run_subprocess([
|
72 |
'ffmpeg', '-y', '-i', input_path, '-ac', '1', '-ar', '8000', audio_wav
|
73 |
])
|
74 |
|
75 |
+
# Compress WAV with fdkaac
|
76 |
await run_subprocess([
|
77 |
'./fdkaac', '-b', '1k', '-C', '-f', '2', '-G', '1', '-w', '8000',
|
78 |
'-o', audio_output, audio_wav
|
79 |
], use_fdkaac=True)
|
80 |
|
81 |
+
# Compress video without audio
|
82 |
video_cmd = [
|
83 |
'ffmpeg', '-y', '-hwaccel', accel, '-i', input_path,
|
84 |
*video_base_opts, '-an', video_output
|
|
|
87 |
video_cmd.extend(['-preset', 'ultrafast'])
|
88 |
await run_subprocess(video_cmd)
|
89 |
|
90 |
+
if audio_only:
|
91 |
+
return audio_output, None
|
92 |
+
|
93 |
+
# Merge video + audio
|
94 |
merged_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
|
95 |
await run_subprocess([
|
96 |
'ffmpeg', '-y', '-i', video_output, '-i', audio_output, '-c', 'copy', merged_output
|
97 |
])
|
98 |
|
99 |
+
# Cleanup intermediates
|
100 |
for f in [audio_wav, audio_output, video_output]:
|
101 |
try:
|
102 |
os.remove(f)
|
103 |
except FileNotFoundError:
|
104 |
pass
|
105 |
|
106 |
+
return None, merged_output
|
107 |
|
108 |
async def process_conversion(use_youtube, youtube_url, video_file, downscale, faster, use_mp3, audio_only):
|
109 |
try:
|
|
|
112 |
return "Error: YouTube URL required.", None
|
113 |
yt_uuid = str(uuid.uuid4())
|
114 |
yt_out = os.path.join(UPLOAD_FOLDER, yt_uuid + ".%(ext)s")
|
115 |
+
yt_cmd = ['yt-dlp', '-o', yt_out, '-f', 'b', youtube_url]
|
116 |
await run_subprocess(yt_cmd)
|
117 |
pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
|
118 |
files = glob.glob(pattern)
|
|
|
126 |
input_path = os.path.join(UPLOAD_FOLDER, f"{uuid.uuid4()}{ext}")
|
127 |
shutil.copy2(video_file.name, input_path)
|
128 |
|
129 |
+
audio_out, video_out = await convert_video_task(
|
130 |
+
input_path, downscale, faster, use_mp3, audio_only
|
131 |
+
)
|
132 |
|
133 |
if audio_only:
|
134 |
return audio_out, audio_out
|
135 |
+
return video_out, video_out
|
|
|
136 |
except Exception as e:
|
137 |
return f"Error: {str(e)}", None
|
138 |
|
139 |
def convert_video(*args):
|
140 |
return asyncio.run(process_conversion(*args))
|
141 |
|
142 |
+
# Gradio Interface (unchanged, still pretty)
|
143 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="rose")) as demo:
|
144 |
gr.Markdown("""
|
145 |
# π₯ **Low Quality Video Inator**
|
146 |
+
Welcome to your personal bad-video-making machine.
|
147 |
+
|
148 |
+
Upload a video or paste a YouTube URL below, then tweak the settings to ruin your video (on purpose).
|
149 |
""")
|
150 |
|
151 |
with gr.Group():
|
152 |
with gr.Row():
|
153 |
+
use_youtube = gr.Checkbox(label="π Use YouTube URL (experimental)", value=False)
|
154 |
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Paste YouTube URL here")
|
155 |
video_file = gr.File(label="π Upload Video File")
|
156 |
|