Jeffgold commited on
Commit
6f1f7b0
·
1 Parent(s): 7e18507

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -65
app.py CHANGED
@@ -20,77 +20,67 @@ temp_dir = tempfile.mkdtemp()
20
 
21
  video_file = gr.inputs.File(label="Video File", type="file")
22
  quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")
23
- aspect_ratio = gr.inputs.Dropdown(choices=[
24
- "1:1",
25
- "4:3",
26
- "3:2",
27
- "5:4",
28
- "16:9",
29
- "21:9",
30
- "1.85:1",
31
- "2.35:1",
32
- "3:1",
33
- "360",
34
- "9:16",
35
- "16:9",
36
- "2:1",
37
- "1:2",
38
- "9:1",
39
- ], label="Aspect Ratio", default="16:9")
40
-
41
- video_url = gr.inputs.Textbox(label="Video URL")
42
-
43
- def convert_video(video_file: File, quality, aspect_ratio, video_url):
44
- if video_file is None:
45
- video_file = Path(video_url)
46
 
47
- # Get the file extension
48
- file_extension = video_file.name.split(".")[-1]
 
 
 
 
 
 
 
49
 
50
- # Remove the file extension
51
- output_path = video_file.name[:-len(file_extension)]
52
 
53
- # Add the .mp4 file extension
54
- output_path += "m3u8"
55
 
56
- # Set the output path to the temp directory
57
- output_path = os.path.join(temp_dir, output_path)
 
58
 
59
- if os.path.exists(output_path):
60
- # The file already exists, so we can just display it in the output viewer
61
- gr.components.Video(output_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  else:
63
- # The file does not exist, so we need to convert it
64
- ffmpeg_command = f"ffmpeg -i {video_file} -c:v libx264 -crf {quality} -f mp4 -aspect {aspect_ratio} {output_path}"
65
-
66
- try:
67
- subprocess.run(ffmpeg_command, shell=True, timeout=10)
68
- except subprocess.TimeoutExpired:
69
- print("ffmpeg timed out")
70
- return None
71
- except FileNotFoundError:
72
- print("ffmpeg is not installed.")
73
- return None
74
-
75
- for i in range(10):
76
- if os.path.exists(output_path):
77
- break
78
- else:
79
- time.sleep(1)
80
-
81
- # The file has now been converted, so we can display it in the output viewer
82
- gr.components.Video(output_path)
83
-
84
- # Create a temporary file to store the download link
85
- # with tempfile.NamedTemporaryFile(delete=False) as f:
86
- # f.write(output_path.encode())
87
- # download_link = f.name
88
-
89
- # Add a button to download the file
90
- # gr.components.HTML("""
91
- # <a href="{}" download="downloaded_file.mp4">Download</a>
92
- # """.format(download_link))
93
 
94
  from gradio import outputs
95
 
96
- gr.Interface(convert_video, inputs=[video_file, quality, aspect_ratio, video_url], outputs=[outputs.Video()]).launch()
 
20
 
21
  video_file = gr.inputs.File(label="Video File", type="file")
22
  quality = gr.inputs.Dropdown(choices=["18", "23", "28", "32"], label="Quality", default="23")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ def get_aspect_ratio(video_file):
25
+ # Get the path to the ffprobe binary
26
+ ffprobe_path = ffmpeg.probe_path()
27
+
28
+ # Get the command to get the aspect ratio of the video
29
+ ffprobe_command = f"{ffprobe_path} -show_entries stream=width,height -of compact=p=0:nk=1 {video_file}"
30
+
31
+ # Run the command and get the output
32
+ output = subprocess.check_output(ffprobe_command, shell=True).decode("utf-8")
33
 
34
+ # Get the aspect ratio from the output
35
+ aspect_ratio = output.split()[0].split(":")[1]
36
 
37
+ return aspect_ratio
 
38
 
39
+ def convert_video(video_file: File, quality, aspect_ratio):
40
+ if video_file is None:
41
+ video_file = Path(video_url)
42
 
43
+ # Get the original aspect ratio of the video
44
+ original_aspect_ratio = get_aspect_ratio(video_file)
45
+
46
+ # Check if the aspect ratio is already set
47
+ if aspect_ratio == original_aspect_ratio:
48
+ return video_file
49
+
50
+ # Set the aspect ratio
51
+ ffmpeg_command = f"ffmpeg -i {video_file} -c:v libx264 -crf {quality} -f mp4 -aspect {aspect_ratio} {video_file}"
52
+
53
+ try:
54
+ subprocess.run(ffmpeg_command, shell=True, timeout=10)
55
+ except subprocess.TimeoutExpired:
56
+ print("ffmpeg timed out")
57
+ return None
58
+ except FileNotFoundError:
59
+ print("ffmpeg is not installed.")
60
+ return None
61
+
62
+ for i in range(10):
63
+ if os.path.exists(video_file):
64
+ break
65
+ else:
66
+ time.sleep(1)
67
+
68
+ # The file has now been converted, so we can display it in the output viewer
69
+ if os.path.exists(video_file):
70
+ gr.components.Video(video_file)
71
  else:
72
+ print("File does not exist.")
73
+
74
+ # Create a temporary file to store the download link
75
+ # with tempfile.NamedTemporaryFile(delete=False) as f:
76
+ # f.write(output_path.encode())
77
+ # download_link = f.name
78
+
79
+ # Add a button to download the file
80
+ # gr.components.HTML("""
81
+ # <a href="{}" download="downloaded_file.mp4">Download</a>
82
+ # """.format(download_link))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  from gradio import outputs
85
 
86
+ gr.Interface(convert_video, inputs=[video_file, quality], outputs=[outputs.Video()]).launch(share=True)