Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
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 |
|
@@ -11,63 +13,67 @@ st.set_page_config(layout="wide", page_title="Video Conversion Tool")
|
|
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'])
|
13 |
gif_formats = ['GIF']
|
14 |
-
image_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS',
|
15 |
-
'
|
16 |
-
'
|
17 |
-
|
18 |
-
|
19 |
|
20 |
def sanitize_filename(filename):
|
21 |
"""Sanitize filename by removing special characters and spaces."""
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def get_video_duration(video_path):
|
26 |
"""Get video duration in seconds using ffmpeg."""
|
27 |
probe = ffmpeg.probe(video_path, v='error', select_streams='v:0', show_entries='stream=duration')
|
28 |
-
|
29 |
-
return duration_in_seconds
|
30 |
|
31 |
def convert_video(video, target_format, conversion_type, time_in_seconds=None):
|
32 |
try:
|
33 |
-
#
|
34 |
-
temp_dir = tempfile.mkdtemp()
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
-
|
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())
|
43 |
|
44 |
if conversion_type == 'Video to Video':
|
45 |
-
output_file = f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}"
|
46 |
-
ffmpeg.input(video_path).output(output_file).run()
|
47 |
return output_file
|
48 |
|
49 |
elif conversion_type == 'Video to Audio':
|
50 |
-
|
51 |
-
ffmpeg.input(video_path).output(
|
52 |
-
return
|
53 |
|
54 |
elif conversion_type == 'Video to GIF':
|
55 |
-
|
56 |
-
ffmpeg.input(video_path).output(
|
57 |
-
return
|
58 |
|
59 |
elif conversion_type == 'Video to Image':
|
60 |
if time_in_seconds is None:
|
61 |
return "Please specify a valid time in seconds for image extraction."
|
62 |
|
63 |
-
image_output_file = f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.png"
|
64 |
-
ffmpeg.input(video_path, ss=time_in_seconds).output(image_output_file, vframes=1).run()
|
65 |
|
66 |
# Convert the image to the desired format using Pillow
|
67 |
img = Image.open(image_output_file)
|
68 |
if target_format.lower() in image_formats:
|
69 |
-
image_output_file = f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.{target_format.lower()}"
|
70 |
-
img.save(image_output_file, target_format.upper())
|
71 |
|
72 |
return image_output_file
|
73 |
|
@@ -101,8 +107,8 @@ def main():
|
|
101 |
|
102 |
with col2:
|
103 |
if video_file:
|
104 |
-
#
|
105 |
-
temp_video_path = os.path.join(
|
106 |
with open(temp_video_path, "wb") as f:
|
107 |
f.write(video_file.getbuffer())
|
108 |
|
@@ -134,16 +140,17 @@ def main():
|
|
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(
|
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 streamlit as st
|
2 |
import ffmpeg
|
3 |
import os
|
4 |
+
import time
|
5 |
from PIL import Image
|
6 |
import re
|
7 |
import tempfile
|
8 |
+
import shutil
|
9 |
|
10 |
st.set_page_config(layout="wide", page_title="Video Conversion Tool")
|
11 |
|
|
|
13 |
supported_formats = sorted(['3GP', 'ASF', 'AVI', 'DIVX', 'FLV', 'M2TS', 'M4V', 'MKV', 'MOV', 'MP4', 'MPEG', 'MPG', 'MTS', 'TS', 'VOB', 'WEBM', 'WMV', 'XVID'])
|
14 |
audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA', 'AIFF', 'OPUS', 'APE', 'CAF', 'PCM', 'DTS', 'TTA', 'AMR', 'MID', 'SPX', 'WV', 'RA', 'TAK'])
|
15 |
gif_formats = ['GIF']
|
16 |
+
image_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS', 'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM',
|
17 |
+
'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX', 'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER',
|
18 |
+
'TGA', 'TIFF', 'WEBP', 'WMX', 'XBM']
|
19 |
+
|
20 |
+
CACHE_DIR = tempfile.mkdtemp()
|
21 |
|
22 |
def sanitize_filename(filename):
|
23 |
"""Sanitize filename by removing special characters and spaces."""
|
24 |
+
return re.sub(r'[^a-zA-Z0-9_.-]', '_', filename)
|
25 |
+
|
26 |
+
def clean_cache():
|
27 |
+
"""Remove files in the cache directory older than 15 minutes."""
|
28 |
+
now = time.time()
|
29 |
+
for file in os.listdir(CACHE_DIR):
|
30 |
+
file_path = os.path.join(CACHE_DIR, file)
|
31 |
+
if os.path.isfile(file_path) and now - os.path.getmtime(file_path) > 900: # 15 minutes
|
32 |
+
os.remove(file_path)
|
33 |
|
34 |
def get_video_duration(video_path):
|
35 |
"""Get video duration in seconds using ffmpeg."""
|
36 |
probe = ffmpeg.probe(video_path, v='error', select_streams='v:0', show_entries='stream=duration')
|
37 |
+
return float(probe['streams'][0]['duration'])
|
|
|
38 |
|
39 |
def convert_video(video, target_format, conversion_type, time_in_seconds=None):
|
40 |
try:
|
41 |
+
clean_cache() # Clean up cache before processing
|
|
|
42 |
|
43 |
+
# Save the uploaded video to the temp directory
|
44 |
+
sanitized_base_name = sanitize_filename(os.path.splitext(video.name)[0])
|
45 |
+
video_path = os.path.join(CACHE_DIR, f"{sanitized_base_name}.mp4")
|
|
|
46 |
|
47 |
with open(video_path, "wb") as f:
|
48 |
+
f.write(video.getbuffer())
|
49 |
|
50 |
if conversion_type == 'Video to Video':
|
51 |
+
output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}")
|
52 |
+
ffmpeg.input(video_path).output(output_file, overwrite_output=True).run(overwrite_output=True)
|
53 |
return output_file
|
54 |
|
55 |
elif conversion_type == 'Video to Audio':
|
56 |
+
output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_to_audio_{sanitized_base_name}.{target_format.lower()}")
|
57 |
+
ffmpeg.input(video_path).output(output_file, overwrite_output=True).run(overwrite_output=True)
|
58 |
+
return output_file
|
59 |
|
60 |
elif conversion_type == 'Video to GIF':
|
61 |
+
output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_to_gif_{sanitized_base_name}.gif")
|
62 |
+
ffmpeg.input(video_path).output(output_file, vf="fps=10,scale=320:-1:flags=lanczos", overwrite_output=True).run(overwrite_output=True)
|
63 |
+
return output_file
|
64 |
|
65 |
elif conversion_type == 'Video to Image':
|
66 |
if time_in_seconds is None:
|
67 |
return "Please specify a valid time in seconds for image extraction."
|
68 |
|
69 |
+
image_output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.png")
|
70 |
+
ffmpeg.input(video_path, ss=time_in_seconds).output(image_output_file, vframes=1, overwrite_output=True).run(overwrite_output=True)
|
71 |
|
72 |
# Convert the image to the desired format using Pillow
|
73 |
img = Image.open(image_output_file)
|
74 |
if target_format.lower() in image_formats:
|
75 |
+
image_output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.{target_format.lower()}")
|
76 |
+
img.save(image_output_file, target_format.upper())
|
77 |
|
78 |
return image_output_file
|
79 |
|
|
|
107 |
|
108 |
with col2:
|
109 |
if video_file:
|
110 |
+
# Save uploaded video to cache
|
111 |
+
temp_video_path = os.path.join(CACHE_DIR, video_file.name)
|
112 |
with open(temp_video_path, "wb") as f:
|
113 |
f.write(video_file.getbuffer())
|
114 |
|
|
|
140 |
if st.button("Convert"):
|
141 |
with st.spinner("Converting..."):
|
142 |
output_file = convert_video(video_file, target_format, conversion_type, time_in_seconds)
|
143 |
+
|
144 |
if "Error" in output_file:
|
145 |
st.error(output_file)
|
146 |
else:
|
147 |
+
st.success("Conversion Successful! Download the file:")
|
148 |
st.download_button(
|
149 |
label="Download Converted File",
|
150 |
data=open(output_file, 'rb').read(),
|
151 |
+
file_name=os.path.basename(output_file)
|
152 |
)
|
153 |
|
154 |
if __name__ == "__main__":
|
155 |
main()
|
156 |
+
|