Adityadn commited on
Commit
826ac5c
·
verified ·
1 Parent(s): 20c3f81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -63
app.py CHANGED
@@ -1,12 +1,10 @@
1
- import streamlit as st
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.getbuffer()) # Save the uploaded video to a local file
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 main():
90
- st.title("Video Conversion Tool")
91
- st.write("Convert videos to audio, GIFs, images, or other formats easily with this powerful tool.")
92
-
93
- # Create two columns
94
- col1, col2 = st.columns([1, 1])
95
-
96
- with col1:
97
- # Upload video file
98
- video_file = st.file_uploader("Upload a Video", type=supported_formats)
99
- if video_file:
100
- st.video(video_file)
101
-
102
- with col2:
103
- if video_file:
104
- # Create a temporary file and save uploaded video
105
- temp_video_path = os.path.join(tempfile.mkdtemp(), video_file.name)
106
- with open(temp_video_path, "wb") as f:
107
- f.write(video_file.getbuffer())
108
-
109
- # Get video duration
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
- main()
 
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()