Jeffgold commited on
Commit
4c21960
·
1 Parent(s): 5ad04a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -56,28 +56,33 @@ def convert_video(video_file: File, quality, aspect_ratio, video_url):
56
  # Set the output path to the temp directory
57
  output_path = os.path.join(temp_dir, output_path)
58
 
59
- # Create the temporary file
60
- os.mknod(output_path)
61
-
62
- ffmpeg_command = f"ffmpeg -i {video_file} -c:v libx264 -crf {quality} -f mp4 -aspect {aspect_ratio} {output_path}"
63
-
64
- try:
65
- subprocess.run(ffmpeg_command, shell=True, timeout=10)
66
- except subprocess.TimeoutExpired:
67
- print("ffmpeg timed out")
68
- return None
69
- except FileNotFoundError:
70
- print("ffmpeg is not installed.")
71
- return None
72
-
73
- for i in range(10):
74
- if os.path.exists(output_path):
75
- break
76
- else:
77
- time.sleep(1)
78
-
79
- # Return the output file directly
80
- return output_path
 
 
 
 
 
81
 
82
  from gradio import outputs
83
 
 
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.outputs.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.outputs.Video(output_path)
83
+
84
+ # Add a button to download the file
85
+ gr.outputs.Button(label="Download", click=lambda: gr.download(output_path))
86
 
87
  from gradio import outputs
88