RandomPersonRR commited on
Commit
7be90a2
·
verified ·
1 Parent(s): f384c57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -24
app.py CHANGED
@@ -6,18 +6,7 @@ import glob
6
  import shutil
7
  import gradio as gr
8
 
9
- os.system("chmod +x ./fdkaac")
10
- # Set executable fdkaac path
11
- fdkaac_path = os.path.abspath("./fdkaac")
12
- ffmpeg_path = shutil.which("ffmpeg")
13
- yt_dlp_path = shutil.which("yt-dlp")
14
-
15
- if not os.path.isfile(fdkaac_path):
16
- raise FileNotFoundError("fdkaac binary not found in current directory.")
17
- if not ffmpeg_path:
18
- raise FileNotFoundError("FFmpeg not found in PATH.")
19
- if not yt_dlp_path:
20
- raise FileNotFoundError("yt-dlp not found in PATH.")
21
 
22
  accel = 'auto'
23
  video_base_opts = ['-crf', '63', '-c:v', 'libx264', '-tune', 'zerolatency']
@@ -30,7 +19,8 @@ os.makedirs(CONVERTED_FOLDER, exist_ok=True)
30
  async def run_subprocess(cmd, use_fdkaac=False):
31
  env = os.environ.copy()
32
  if use_fdkaac:
33
- env["LD_LIBRARY_PATH"] = env.get("LD_LIBRARY_PATH", "") + ":/usr/local/lib"
 
34
  print(f"[DEBUG] Running command:\n{' '.join(cmd)}\n")
35
  process = await asyncio.create_subprocess_exec(
36
  *cmd,
@@ -47,43 +37,63 @@ async def run_subprocess(cmd, use_fdkaac=False):
47
 
48
  async def convert_video_task(input_path, downscale, faster, use_mp3):
49
  if use_mp3:
 
50
  output_path = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp3")
51
  ffmpeg_cmd = [
52
- ffmpeg_path, '-y', '-i', input_path, '-vn',
53
  '-c:a', 'libmp3lame', '-b:a', '8k', '-ar', '24000', '-ac', '1', output_path
54
  ]
55
  await run_subprocess(ffmpeg_cmd)
56
  return output_path, None
57
 
 
58
  audio_wav = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.wav")
59
  audio_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.m4a")
60
  video_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
61
 
 
62
  await run_subprocess([
63
- ffmpeg_path, '-y', '-i', input_path, '-ac', '1', '-ar', '8000', audio_wav
64
  ])
65
 
 
66
  await run_subprocess([
67
- fdkaac_path, '-b', '1k', '-C', '-f', '2', '-G', '1', '-w', '8000',
68
  '-o', audio_output, audio_wav
69
  ], use_fdkaac=True)
70
 
 
71
  video_cmd = [
72
- ffmpeg_path, '-y', '-hwaccel', accel, '-i', input_path,
73
  *video_base_opts, '-an', video_output
74
  ]
75
  if faster:
76
  video_cmd.extend(['-preset', 'ultrafast'])
77
  await run_subprocess(video_cmd)
78
 
79
- return audio_output, video_output
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  async def process_conversion(use_youtube, youtube_url, video_file, downscale, faster, use_mp3, audio_only):
82
  try:
83
  if use_youtube:
 
 
84
  yt_uuid = str(uuid.uuid4())
85
  yt_out = os.path.join(UPLOAD_FOLDER, yt_uuid + ".%(ext)s")
86
- yt_cmd = [yt_dlp_path, '-o', yt_out, '-f', 'b', youtube_url]
87
  await run_subprocess(yt_cmd)
88
  pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
89
  files = glob.glob(pattern)
@@ -93,22 +103,22 @@ async def process_conversion(use_youtube, youtube_url, video_file, downscale, fa
93
  else:
94
  if not video_file:
95
  return "No video provided.", None
96
- ext = os.path.splitext(video_file)[1]
97
  input_path = os.path.join(UPLOAD_FOLDER, f"{uuid.uuid4()}{ext}")
98
- shutil.copy2(video_file, input_path)
99
 
100
  audio_out, video_out = await convert_video_task(input_path, downscale, faster, use_mp3)
101
 
102
  if audio_only:
103
  return audio_out, audio_out
104
- return video_out or audio_out, video_out or audio_out
 
105
  except Exception as e:
106
- return str(e), None
107
 
108
  def convert_video(*args):
109
  return asyncio.run(process_conversion(*args))
110
 
111
- # Gradio Interface
112
  with gr.Blocks(theme=gr.themes.Default(primary_hue="rose")) as demo:
113
  gr.Markdown("""
114
  # 🎥 **Low Quality Video Inator**
 
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
+ # 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(
26
  *cmd,
 
37
 
38
  async def convert_video_task(input_path, downscale, faster, use_mp3):
39
  if use_mp3:
40
+ # MP3 audio only, no splitting
41
  output_path = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp3")
42
  ffmpeg_cmd = [
43
+ 'ffmpeg', '-y', '-i', input_path, '-vn',
44
  '-c:a', 'libmp3lame', '-b:a', '8k', '-ar', '24000', '-ac', '1', output_path
45
  ]
46
  await run_subprocess(ffmpeg_cmd)
47
  return output_path, None
48
 
49
+ # Paths for audio and video
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 8kHz WAV audio
55
  await run_subprocess([
56
+ 'ffmpeg', '-y', '-i', input_path, '-ac', '1', '-ar', '8000', audio_wav
57
  ])
58
 
59
+ # Compress WAV to AAC with fdkaac
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 with extreme low quality, no audio
66
  video_cmd = [
67
+ 'ffmpeg', '-y', '-hwaccel', accel, '-i', input_path,
68
  *video_base_opts, '-an', video_output
69
  ]
70
  if faster:
71
  video_cmd.extend(['-preset', 'ultrafast'])
72
  await run_subprocess(video_cmd)
73
 
74
+ # Merge video + audio into final mp4 container
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 intermediate files
81
+ for f in [audio_wav, audio_output, video_output]:
82
+ try:
83
+ os.remove(f)
84
+ except FileNotFoundError:
85
+ pass
86
+
87
+ return audio_output, merged_output
88
 
89
  async def process_conversion(use_youtube, youtube_url, video_file, downscale, faster, use_mp3, audio_only):
90
  try:
91
  if use_youtube:
92
+ if not youtube_url:
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', 'b', youtube_url]
97
  await run_subprocess(yt_cmd)
98
  pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
99
  files = glob.glob(pattern)
 
103
  else:
104
  if not video_file:
105
  return "No video provided.", None
106
+ ext = os.path.splitext(video_file.name)[1]
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(input_path, downscale, faster, use_mp3)
111
 
112
  if audio_only:
113
  return audio_out, audio_out
114
+ else:
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**