RandomPersonRR commited on
Commit
7f4c045
·
verified ·
1 Parent(s): 3b8885d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -14
app.py CHANGED
@@ -9,7 +9,7 @@ import gradio as gr
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', '-movflags', '+faststart']
13
 
14
  UPLOAD_FOLDER = 'uploads'
15
  CONVERTED_FOLDER = 'converted'
@@ -34,7 +34,7 @@ async def run_subprocess(cmd, use_fdkaac=False):
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 = [
@@ -50,11 +50,16 @@ async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only)
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
@@ -80,11 +85,17 @@ async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only)
80
 
81
  # Compress video without audio
82
  video_cmd = [
83
- 'ffmpeg', '-y', '-hwaccel', accel, '-i', input_path,
84
- *video_base_opts, '-an', video_output
85
  ]
86
- if faster:
87
- video_cmd.extend(['-preset', 'ultrafast'])
 
 
 
 
 
 
 
88
  await run_subprocess(video_cmd)
89
 
90
  if audio_only:
@@ -105,7 +116,7 @@ async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only)
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:
110
  if use_youtube:
111
  if not youtube_url:
@@ -127,7 +138,7 @@ async def process_conversion(use_youtube, youtube_url, video_file, downscale, fa
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:
@@ -139,7 +150,7 @@ async def process_conversion(use_youtube, youtube_url, video_file, downscale, fa
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**
@@ -162,6 +173,16 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="rose")) as demo:
162
  with gr.Row():
163
  use_mp3 = gr.Checkbox(label="Use MP3 Audio", value=False)
164
  audio_only = gr.Checkbox(label="Audio Only", value=False)
 
 
 
 
 
 
 
 
 
 
165
 
166
  convert_button = gr.Button("Convert Now", variant="primary")
167
 
@@ -173,7 +194,11 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="rose")) as demo:
173
 
174
  convert_button.click(
175
  convert_video,
176
- inputs=[use_youtube, youtube_url, video_file, downscale, faster, use_mp3, audio_only],
 
 
 
 
177
  outputs=[video_preview, file_download]
178
  )
179
 
 
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']
13
 
14
  UPLOAD_FOLDER = 'uploads'
15
  CONVERTED_FOLDER = 'converted'
 
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, custom_bitrate, video_bitrate):
38
  if use_mp3:
39
  output_audio = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp3")
40
  ffmpeg_audio_cmd = [
 
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
  ]
55
+
56
+ if custom_bitrate:
57
+ ffmpeg_video_cmd += ['-b:v', f"{int(video_bitrate)}k"]
58
+ else:
59
+ ffmpeg_video_cmd += video_base_opts
60
+ if faster:
61
+ ffmpeg_video_cmd.extend(['-preset', 'ultrafast'])
62
+
63
  ffmpeg_video_cmd += [
64
  '-c:a', 'libmp3lame', '-b:a', '8k', '-ar', '24000', '-ac', '1',
65
  output_video
 
85
 
86
  # Compress video without audio
87
  video_cmd = [
88
+ 'ffmpeg', '-y', '-hwaccel', accel, '-i', input_path
 
89
  ]
90
+
91
+ if custom_bitrate:
92
+ video_cmd += ['-b:v', f"{int(video_bitrate)}k"]
93
+ else:
94
+ video_cmd += video_base_opts
95
+ if faster:
96
+ video_cmd.extend(['-preset', 'ultrafast'])
97
+
98
+ video_cmd += ['-an', video_output]
99
  await run_subprocess(video_cmd)
100
 
101
  if audio_only:
 
116
 
117
  return None, merged_output
118
 
119
+ async def process_conversion(use_youtube, youtube_url, video_file, downscale, faster, use_mp3, audio_only, custom_bitrate, video_bitrate):
120
  try:
121
  if use_youtube:
122
  if not youtube_url:
 
138
  shutil.copy2(video_file.name, input_path)
139
 
140
  audio_out, video_out = await convert_video_task(
141
+ input_path, downscale, faster, use_mp3, audio_only, custom_bitrate, video_bitrate
142
  )
143
 
144
  if audio_only:
 
150
  def convert_video(*args):
151
  return asyncio.run(process_conversion(*args))
152
 
153
+ # Gradio Interface (Now With Custom Bitrate)
154
  with gr.Blocks(theme=gr.themes.Default(primary_hue="rose")) as demo:
155
  gr.Markdown("""
156
  # 🎥 **Low Quality Video Inator**
 
173
  with gr.Row():
174
  use_mp3 = gr.Checkbox(label="Use MP3 Audio", value=False)
175
  audio_only = gr.Checkbox(label="Audio Only", value=False)
176
+ with gr.Row():
177
+ custom_bitrate = gr.Checkbox(label="Custom Video Bitrate", value=False)
178
+ video_bitrate = gr.Number(label="Bitrate (kbps)", visible=False)
179
+
180
+ # Show/hide bitrate field
181
+ custom_bitrate.change(
182
+ lambda checked: gr.update(visible=checked),
183
+ inputs=[custom_bitrate],
184
+ outputs=[video_bitrate]
185
+ )
186
 
187
  convert_button = gr.Button("Convert Now", variant="primary")
188
 
 
194
 
195
  convert_button.click(
196
  convert_video,
197
+ inputs=[
198
+ use_youtube, youtube_url, video_file,
199
+ downscale, faster, use_mp3, audio_only,
200
+ custom_bitrate, video_bitrate
201
+ ],
202
  outputs=[video_preview, file_download]
203
  )
204