Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,10 @@
|
|
1 |
-
import
|
2 |
import ffmpeg
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
import re
|
6 |
import tempfile
|
7 |
|
8 |
-
st.set_page_config(layout="wide", page_title="Video Conversion Tool")
|
9 |
-
|
10 |
# Supported formats
|
11 |
supported_formats = sorted(['3GP', 'ASF', 'AVI', 'DIVX', 'FLV', 'M2TS', 'M4V', 'MKV', 'MOV', 'MP4', 'MPEG', 'MPG', 'MTS', 'TS', 'VOB', 'WEBM', 'WMV', 'XVID'])
|
12 |
audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA', 'AIFF', 'OPUS', 'APE', 'CAF', 'PCM', 'DTS', 'TTA', 'AMR', 'MID', 'SPX', 'WV', 'RA', 'TAK'])
|
@@ -39,7 +37,7 @@ def convert_video(video, target_format, conversion_type, time_in_seconds=None):
|
|
39 |
video_path = os.path.join(temp_dir, f"{sanitized_base_name}.mp4") # Saving as mp4 by default for now
|
40 |
|
41 |
with open(video_path, "wb") as f:
|
42 |
-
f.write(video
|
43 |
|
44 |
if conversion_type == 'Video to Video':
|
45 |
output_file = f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}"
|
@@ -86,64 +84,27 @@ def update_format_choices(conversion_type):
|
|
86 |
return image_formats
|
87 |
return []
|
88 |
|
89 |
-
def
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
with
|
97 |
-
#
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
video_duration = get_video_duration(temp_video_path)
|
111 |
-
|
112 |
-
# Select conversion type
|
113 |
-
conversion_type = st.selectbox(
|
114 |
-
"Select Conversion Type",
|
115 |
-
['Video to Video', 'Video to Audio', 'Video to GIF', 'Video to Image']
|
116 |
-
)
|
117 |
-
|
118 |
-
# Update format choices based on conversion type
|
119 |
-
target_format_choices = update_format_choices(conversion_type)
|
120 |
-
target_format = st.selectbox("Select Target Format", target_format_choices)
|
121 |
-
|
122 |
-
# If 'Video to Image' conversion, ask for time in seconds
|
123 |
-
if conversion_type == 'Video to Image':
|
124 |
-
time_in_seconds = st.slider(
|
125 |
-
label="Time (in seconds) for image extraction",
|
126 |
-
min_value=0,
|
127 |
-
max_value=int(video_duration),
|
128 |
-
value=0,
|
129 |
-
step=1
|
130 |
-
)
|
131 |
-
else:
|
132 |
-
time_in_seconds = None
|
133 |
-
|
134 |
-
if st.button("Convert"):
|
135 |
-
with st.spinner("Converting..."):
|
136 |
-
output_file = convert_video(video_file, target_format, conversion_type, time_in_seconds)
|
137 |
-
|
138 |
-
if "Error" in output_file:
|
139 |
-
st.error(output_file)
|
140 |
-
else:
|
141 |
-
st.success(f"Conversion Successful! Download the file:")
|
142 |
-
st.download_button(
|
143 |
-
label="Download Converted File",
|
144 |
-
data=open(output_file, 'rb').read(),
|
145 |
-
file_name=output_file
|
146 |
-
)
|
147 |
|
148 |
if __name__ == "__main__":
|
149 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import ffmpeg
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
import re
|
6 |
import tempfile
|
7 |
|
|
|
|
|
8 |
# Supported formats
|
9 |
supported_formats = sorted(['3GP', 'ASF', 'AVI', 'DIVX', 'FLV', 'M2TS', 'M4V', 'MKV', 'MOV', 'MP4', 'MPEG', 'MPG', 'MTS', 'TS', 'VOB', 'WEBM', 'WMV', 'XVID'])
|
10 |
audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA', 'AIFF', 'OPUS', 'APE', 'CAF', 'PCM', 'DTS', 'TTA', 'AMR', 'MID', 'SPX', 'WV', 'RA', 'TAK'])
|
|
|
37 |
video_path = os.path.join(temp_dir, f"{sanitized_base_name}.mp4") # Saving as mp4 by default for now
|
38 |
|
39 |
with open(video_path, "wb") as f:
|
40 |
+
f.write(video) # Save the uploaded video to a local file
|
41 |
|
42 |
if conversion_type == 'Video to Video':
|
43 |
output_file = f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}"
|
|
|
84 |
return image_formats
|
85 |
return []
|
86 |
|
87 |
+
def convert_function(video_file, conversion_type, target_format, time_in_seconds=None):
|
88 |
+
# Convert the video based on user input
|
89 |
+
video_file_content = video_file.read()
|
90 |
+
return convert_video(video_file_content, target_format, conversion_type, time_in_seconds)
|
91 |
+
|
92 |
+
def gradio_interface():
|
93 |
+
# Set up Gradio interface
|
94 |
+
with gr.Blocks() as demo:
|
95 |
+
gr.Markdown("# Video Conversion Tool")
|
96 |
+
gr.Markdown("Convert videos to audio, GIFs, images, or other formats easily with this powerful tool.")
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
video_file = gr.File(label="Upload Video File", type="file", file_types=supported_formats)
|
100 |
+
conversion_type = gr.Dropdown(label="Select Conversion Type", choices=['Video to Video', 'Video to Audio', 'Video to GIF', 'Video to Image'])
|
101 |
+
target_format = gr.Dropdown(label="Select Target Format", choices=supported_formats)
|
102 |
+
time_in_seconds = gr.Slider(label="Time (in seconds) for image extraction", minimum=0, maximum=300, step=1, value=0)
|
103 |
+
output_file = gr.File(label="Converted File", interactive=False)
|
104 |
+
|
105 |
+
video_file.change(fn=convert_function, inputs=[video_file, conversion_type, target_format, time_in_seconds], outputs=output_file)
|
106 |
+
|
107 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
+
gradio_interface()
|