Jeffgold commited on
Commit
5a36eaa
·
1 Parent(s): 95bc252

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -36
app.py CHANGED
@@ -59,45 +59,44 @@ def upload_to_web3_storage(api_key, path):
59
  logging.exception("An error occurred while uploading to web3.storage.")
60
  return f"An error occurred while uploading to web3.storage: {response.json()}"
61
 
62
- def convert_video(video_file, quality, aspect_ratio, resolution, video_url, api_key, upload):
63
- """Converts a video to HLS format, adjusting the quality and aspect ratio as necessary."""
64
  with tempfile.TemporaryDirectory() as temp_dir:
65
  temp_dir = Path(temp_dir)
66
  input_path = get_input_path(video_file, video_url, temp_dir)
67
- output_path = get_output_path(input_path, temp_dir)
68
  aspect_ratio = get_aspect_ratio(input_path, aspect_ratio)
69
 
70
- # Set default values if quality or aspect_ratio are None
71
- quality = "27" if quality is None else quality
72
- aspect_ratio = "1:1" if aspect_ratio is None else aspect_ratio
73
-
74
- ffmpeg_command = [
75
- "ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
76
- "-vf", f"scale=-1:720,setsar={aspect_ratio}", "-hls_time", "6",
77
- "-hls_playlist_type", "vod", "-f", "hls", str(output_path)
78
- ]
79
-
80
- try:
81
- logging.info("Running command: %s", " ".join(ffmpeg_command))
82
- subprocess.run(ffmpeg_command, check=True, timeout=600)
83
- except subprocess.CalledProcessError:
84
- logging.exception("ffmpeg command failed.")
85
- error_file_path = tempfile.gettempdir() + "/error.txt"
86
- with open(error_file_path, 'w') as error_file:
87
- error_file.write("ffmpeg command failed.")
88
- return error_file_path
89
- except subprocess.TimeoutExpired:
90
- logging.exception("ffmpeg command timed out.")
91
- return "ffmpeg command timed out."
92
- except FileNotFoundError:
93
- logging.exception("ffmpeg is not installed.")
94
- return "ffmpeg is not installed."
95
- except Exception as e:
96
- logging.exception("An error occurred.")
97
- return f"An error occurred: {str(e)}"
98
-
99
- # Copy the output to a new temporary file that won't be deleted when the
100
- # TemporaryDirectory context manager exits.
101
  output_copy_path = shutil.copy2(output_path, tempfile.gettempdir())
102
 
103
  if upload:
@@ -114,8 +113,8 @@ def main():
114
  "1.85:1", "2.35:1", "3:1", "360", "9:16", "16:9",
115
  "2:1", "1:2", "9:1"],
116
  label="Aspect Ratio", default=None)
117
- resolution = gr.inputs.Dropdown(
118
- choices=["Max", "Min"], label="Resolution", default="Max")
119
  video_url = gr.inputs.Textbox(label="Video URL")
120
  api_key = gr.inputs.Textbox(label="web3.storage API Key")
121
  upload = gr.inputs.Checkbox(label="Upload to web3.storage", default=False)
 
59
  logging.exception("An error occurred while uploading to web3.storage.")
60
  return f"An error occurred while uploading to web3.storage: {response.json()}"
61
 
62
+ def convert_video(video_file, quality, aspect_ratio, video_url, api_key, upload, resolution):
 
63
  with tempfile.TemporaryDirectory() as temp_dir:
64
  temp_dir = Path(temp_dir)
65
  input_path = get_input_path(video_file, video_url, temp_dir)
66
+
67
  aspect_ratio = get_aspect_ratio(input_path, aspect_ratio)
68
 
69
+ for res in resolution:
70
+ scale = "-1:" + str(res.replace("p", "")) # we convert '1080p' to '-1:1080'
71
+ output_path = get_output_path(input_path, temp_dir, res) # pass the resolution to create a unique output file
72
+
73
+ ffmpeg_command = [
74
+ "ffmpeg", "-i", str(input_path), "-c:v", "libx264", "-crf", str(quality),
75
+ "-vf", f"scale={scale},setsar={aspect_ratio}", "-hls_time", "6",
76
+ "-hls_playlist_type", "vod", "-f", "hls", str(output_path)
77
+ ]
78
+
79
+ try:
80
+ logging.info("Running command: %s", " ".join(ffmpeg_command))
81
+ subprocess.run(ffmpeg_command, check=True, timeout=600)
82
+ except subprocess.CalledProcessError:
83
+ logging.exception("ffmpeg command failed.")
84
+ error_file_path = tempfile.gettempdir() + "/error.txt"
85
+ with open(error_file_path, 'w') as error_file:
86
+ error_file.write("ffmpeg command failed.")
87
+ return error_file_path
88
+ except subprocess.TimeoutExpired:
89
+ logging.exception("ffmpeg command timed out.")
90
+ return "ffmpeg command timed out."
91
+ except FileNotFoundError:
92
+ logging.exception("ffmpeg is not installed.")
93
+ return "ffmpeg is not installed."
94
+ except Exception as e:
95
+ logging.exception("An error occurred.")
96
+ return f"An error occurred: {str(e)}"
97
+
98
+ # We need to modify this part if you want to return all converted videos
99
+ # Here we only return the last converted video
100
  output_copy_path = shutil.copy2(output_path, tempfile.gettempdir())
101
 
102
  if upload:
 
113
  "1.85:1", "2.35:1", "3:1", "360", "9:16", "16:9",
114
  "2:1", "1:2", "9:1"],
115
  label="Aspect Ratio", default=None)
116
+ resolution = gr.components.Dropdown(
117
+ choices=["480p", "720p", "1080p", "1440p", "2160p", "4320p"], label="Resolution", multiple=True, default=["1080p"])
118
  video_url = gr.inputs.Textbox(label="Video URL")
119
  api_key = gr.inputs.Textbox(label="web3.storage API Key")
120
  upload = gr.inputs.Checkbox(label="Upload to web3.storage", default=False)