Spaces:
Running
Running
import streamlit as st | |
import ffmpeg | |
import os | |
from PIL import Image | |
import re | |
import tempfile | |
st.set_page_config(layout="wide", page_title="Video Conversion Tool") | |
# Supported formats | |
supported_formats = sorted(['3GP', 'ASF', 'AVI', 'DIVX', 'FLV', 'M2TS', 'M4V', 'MKV', 'MOV', 'MP4', 'MPEG', 'MPG', 'MTS', 'TS', 'VOB', 'WEBM', 'WMV', 'XVID']) | |
audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA', 'AIFF', 'OPUS', 'APE', 'CAF', 'PCM', 'DTS', 'TTA', 'AMR', 'MID', 'SPX', 'WV', 'RA', 'TAK']) | |
video_path = os.path.join(temp_dir, f"{sanitized_base_name}.mp4") # Saving as mp4 by default for now | |
with open(video_path, "wb") as f: | |
f.write(video.getbuffer()) # Save the uploaded video to a local file | |
if conversion_type == 'Video to Video': | |
output_file = f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}" | |
return image_formats | |
return [] | |
def main(): | |
st.title("Video Conversion Tool") | |
st.write("Convert videos to audio, GIFs, images, or other formats easily with this powerful tool.") | |
# Create two columns | |
col1, col2 = st.columns([1, 1]) | |
with col1: | |
# Upload video file | |
video_file = st.file_uploader("Upload a Video", type=supported_formats) | |
if video_file: | |
st.video(video_file) | |
with col2: | |
if video_file: | |
# Create a temporary file and save uploaded video | |
temp_video_path = os.path.join(tempfile.mkdtemp(), video_file.name) | |
with open(temp_video_path, "wb") as f: | |
f.write(video_file.getbuffer()) | |
# Get video duration | |
video_duration = get_video_duration(temp_video_path) | |
# Select conversion type | |
conversion_type = st.selectbox( | |
"Select Conversion Type", | |
['Video to Video', 'Video to Audio', 'Video to GIF', 'Video to Image'] | |
) | |
# Update format choices based on conversion type | |
target_format_choices = update_format_choices(conversion_type) | |
target_format = st.selectbox("Select Target Format", target_format_choices) | |
# If 'Video to Image' conversion, ask for time in seconds | |
if conversion_type == 'Video to Image': | |
time_in_seconds = st.slider( | |
label="Time (in seconds) for image extraction", | |
min_value=0, | |
max_value=int(video_duration), | |
value=0, | |
step=1 | |
) | |
else: | |
time_in_seconds = None | |
if st.button("Convert"): | |
with st.spinner("Converting..."): | |
output_file = convert_video(video_file, target_format, conversion_type, time_in_seconds) | |
if "Error" in output_file: | |
st.error(output_file) | |
else: | |
st.success(f"Conversion Successful! Download the file:") | |
st.download_button( | |
label="Download Converted File", | |
data=open(output_file, 'rb').read(), | |
file_name=output_file | |
) | |
if __name__ == "__main__": | |
main() |