Adityadn commited on
Commit
9de7e25
·
verified ·
1 Parent(s): 1364555

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import ffmpeg
3
+ import os
4
+ from PIL import Image
5
+
6
+ # Supported formats
7
+ supported_formats = ['avi', 'mp4', 'mov', 'mkv', 'flv', 'wmv', 'webm', 'mpeg', 'mpg', '3gp']
8
+ audio_formats = ['mp3', 'wav', 'aac', 'flac']
9
+ gif_formats = ['gif']
10
+ image_formats = list(Image.SAVE.keys()) # Supported image formats
11
+
12
+ def get_video_duration(video_path):
13
+ """Get video duration in seconds using ffmpeg."""
14
+ probe = ffmpeg.probe(video_path, v='error', select_streams='v:0', show_entries='stream=duration')
15
+ return float(probe['streams'][0]['duration'])
16
+
17
+ def convert_video(video, target_format, conversion_type, time_in_seconds=None):
18
+ try:
19
+ base_name = os.path.splitext(os.path.basename(video.name))[0]
20
+
21
+ if conversion_type == 'Video to Video':
22
+ output_file = f"flowly_ai_video_converter_{base_name}.{target_format.lower()}"
23
+ ffmpeg.input(video.name).output(output_file).run()
24
+ return output_file
25
+
26
+ elif conversion_type == 'Video to Audio':
27
+ audio_output_file = f"flowly_ai_video_to_audio_{base_name}.{target_format.lower()}"
28
+ ffmpeg.input(video.name).output(audio_output_file).run()
29
+ return audio_output_file
30
+
31
+ elif conversion_type == 'Video to GIF':
32
+ gif_output_file = f"flowly_ai_video_to_gif_{base_name}.gif"
33
+ ffmpeg.input(video.name).output(gif_output_file, vf="fps=10,scale=320:-1:flags=lanczos").run()
34
+ return gif_output_file
35
+
36
+ elif conversion_type == 'Video to Image':
37
+ if time_in_seconds is None:
38
+ return "Please specify a valid time in seconds for image extraction."
39
+
40
+ image_output_file = f"flowly_ai_video_to_image_{base_name}_{time_in_seconds}.png"
41
+ ffmpeg.input(video.name, ss=time_in_seconds).output(image_output_file, vframes=1).run()
42
+
43
+ img = Image.open(image_output_file)
44
+ if target_format.lower() in image_formats:
45
+ image_output_file = f"flowly_ai_video_to_image_{base_name}_{time_in_seconds}.{target_format.lower()}"
46
+ img.save(image_output_file, target_format.upper()) # Save with the desired format
47
+
48
+ return image_output_file
49
+
50
+ except Exception as e:
51
+ return f"Error: {e}"
52
+
53
+ def update_format_choices(conversion_type):
54
+ """Update available format choices based on the conversion type."""
55
+ if conversion_type == 'Video to Video':
56
+ return supported_formats
57
+ elif conversion_type == 'Video to Audio':
58
+ return audio_formats
59
+ elif conversion_type == 'Video to GIF':
60
+ return gif_formats
61
+ elif conversion_type == 'Video to Image':
62
+ return image_formats
63
+ return []
64
+
65
+ def main():
66
+ st.title("Video Conversion Tool")
67
+
68
+ # Upload video file
69
+ video_file = st.file_uploader("Upload a Video", type=supported_formats)
70
+
71
+ if video_file:
72
+ st.video(video_file)
73
+
74
+ # Select conversion type
75
+ conversion_type = st.selectbox(
76
+ "Select Conversion Type",
77
+ ['Video to Video', 'Video to Audio', 'Video to GIF', 'Video to Image']
78
+ )
79
+
80
+ # Update format choices based on conversion type
81
+ target_format_choices = update_format_choices(conversion_type)
82
+ target_format = st.selectbox("Select Target Format", target_format_choices)
83
+
84
+ # If 'Video to Image' conversion, ask for time in seconds
85
+ if conversion_type == 'Video to Image':
86
+ time_in_seconds = st.number_input("Time (in seconds) for image extraction", min_value=0)
87
+ else:
88
+ time_in_seconds = None
89
+
90
+ if st.button("Convert"):
91
+ with st.spinner("Converting..."):
92
+ output_file = convert_video(video_file, target_format, conversion_type, time_in_seconds)
93
+
94
+ if "Error" in output_file:
95
+ st.error(output_file)
96
+ else:
97
+ st.success(f"Conversion Successful! Download the file:")
98
+ st.download_button(label="Download Converted File", data=open(output_file, 'rb').read(), file_name=output_file)
99
+
100
+ if __name__ == "__main__":
101
+ main()