Jeffgold commited on
Commit
e4ed31e
·
1 Parent(s): 35a1468

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -7,6 +7,7 @@ from gradio import components
7
  import tempfile
8
  from pathlib import Path
9
  from gradio import inputs, outputs
 
10
 
11
  # Define File object
12
  File = Path
@@ -16,7 +17,10 @@ temp_dir = tempfile.mkdtemp()
16
 
17
  video_file = gr.inputs.File(label="Video File", type="file")
18
  quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")
 
 
19
  aspect_ratio = gr.inputs.Dropdown(choices=[
 
20
  "1:1",
21
  "4:3",
22
  "3:2",
@@ -32,7 +36,7 @@ aspect_ratio = gr.inputs.Dropdown(choices=[
32
  "2:1",
33
  "1:2",
34
  "9:1",
35
- ], label="Aspect Ratio", default="16:9")
36
 
37
  video_url = gr.inputs.Textbox(label="Video URL")
38
 
@@ -48,7 +52,7 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
48
  input_path = video_file.name
49
 
50
  # If a URL was provided
51
- elif video_url is not None:
52
  # Get the file name from URL
53
  file_name = video_url.split("/")[-1]
54
 
@@ -62,7 +66,7 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
62
  response = requests.get(video_url)
63
 
64
  if response.status_code == 200:
65
- input_path = os.path.join(temp_dir, output_file_name)
66
  with open(input_path, 'wb') as f:
67
  f.write(response.content)
68
  else:
@@ -81,13 +85,17 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
81
  return output_path
82
  else:
83
  # The file does not exist, so we need to convert it
84
- ffmpeg_command = f"ffmpeg -i {input_path} -c:v libx264 -crf {quality} -vf scale=-1:720,setsar=1:1 -hls_time 6 -hls_playlist_type vod -f hls {output_path}"
 
 
 
 
85
 
86
  try:
87
- subprocess.run(ffmpeg_command, shell=True, timeout=10)
88
  except subprocess.TimeoutExpired:
89
  print("ffmpeg timed out")
90
- return "ffmpeg timed out"
91
  except FileNotFoundError:
92
  print("ffmpeg is not installed.")
93
  return "ffmpeg is not installed."
@@ -101,6 +109,4 @@ def convert_video(video_file, quality, aspect_ratio, video_url):
101
  # The file has now been converted, so we can display it in the output viewer
102
  return output_path
103
 
104
-
105
-
106
  gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='file').launch()
 
7
  import tempfile
8
  from pathlib import Path
9
  from gradio import inputs, outputs
10
+ from moviepy.editor import VideoFileClip
11
 
12
  # Define File object
13
  File = Path
 
17
 
18
  video_file = gr.inputs.File(label="Video File", type="file")
19
  quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")
20
+
21
+ # default will now be None, which will retain original aspect ratio
22
  aspect_ratio = gr.inputs.Dropdown(choices=[
23
+ None, # Add None option to retain original aspect ratio
24
  "1:1",
25
  "4:3",
26
  "3:2",
 
36
  "2:1",
37
  "1:2",
38
  "9:1",
39
+ ], label="Aspect Ratio", default=None)
40
 
41
  video_url = gr.inputs.Textbox(label="Video URL")
42
 
 
52
  input_path = video_file.name
53
 
54
  # If a URL was provided
55
+ elif video_url:
56
  # Get the file name from URL
57
  file_name = video_url.split("/")[-1]
58
 
 
66
  response = requests.get(video_url)
67
 
68
  if response.status_code == 200:
69
+ input_path = os.path.join(temp_dir, file_name)
70
  with open(input_path, 'wb') as f:
71
  f.write(response.content)
72
  else:
 
85
  return output_path
86
  else:
87
  # The file does not exist, so we need to convert it
88
+ if aspect_ratio is None:
89
+ video = VideoFileClip(input_path)
90
+ aspect_ratio = f"{video.size[0]}:{video.size[1]}"
91
+
92
+ ffmpeg_command = f"ffmpeg -i {input_path} -c:v libx264 -crf {quality} -vf scale=-1:720,setsar={aspect_ratio} -hls_time 6 -hls_playlist_type vod -f hls {output_path}"
93
 
94
  try:
95
+ subprocess.run(ffmpeg_command, shell=True, timeout=600)
96
  except subprocess.TimeoutExpired:
97
  print("ffmpeg timed out")
98
+ return "ffmpeg command timed out."
99
  except FileNotFoundError:
100
  print("ffmpeg is not installed.")
101
  return "ffmpeg is not installed."
 
109
  # The file has now been converted, so we can display it in the output viewer
110
  return output_path
111
 
 
 
112
  gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs='file').launch()