RandomPersonRR commited on
Commit
1dc23c5
·
verified ·
1 Parent(s): fd07a03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -44
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
- # For simplicity, we use fixed upload and conversion folders.
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 = [] # List to keep track of temporary files.
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
- # Pipe best-quality output directly from yt-dlp.
64
- quality = 'bestaudio' if audio_only else 'best'
65
- yt_dlp_cmd = ['yt-dlp', '-o', '-', '-f', quality, youtube_url]
66
- ffmpeg_cmd = ['ffmpeg', '-hwaccel', accel, '-y', '-i', '-'] + extra.split()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (for file uploads and YouTube downloads alike).
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 options.
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
- if use_youtube and not downscale:
98
- try:
99
- yt_proc = subprocess.Popen(yt_dlp_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
100
- subprocess.run(ffmpeg_cmd, stdin=yt_proc.stdout, check=True)
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 could be done here for files in files_to_delete.)
112
- # Return the output file path for both video preview and file download.
113
  return output_path, output_path
114
 
115
- # Create the Gradio interface using Blocks.
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.")