Spaces:
Running
Running
import streamlit as st | |
import os | |
import subprocess | |
import ffmpeg # Import the ffmpeg-python library | |
# Directories for uploads and processing | |
UPLOAD_FOLDER = 'uploads' | |
PROCESSED_FOLDER = 'processed' | |
os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
os.makedirs(PROCESSED_FOLDER, exist_ok=True) | |
def save_uploaded_file(uploaded_file): | |
"""Save uploaded file to a designated folder without renaming it unless a file with the same name exists.""" | |
file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name) | |
# If a file with the same name already exists, generate a unique name | |
if os.path.exists(file_path): | |
base, ext = os.path.splitext(uploaded_file.name) | |
i = 1 | |
while os.path.exists(os.path.join(UPLOAD_FOLDER, f"{base}_{i}{ext}")): | |
i += 1 | |
file_path = os.path.join(UPLOAD_FOLDER, f"{base}_{i}{ext}") | |
with open(file_path, "wb") as f: | |
f.write(uploaded_file.getbuffer()) | |
return file_path | |
def run_ffmpeg(command): | |
"""Run the FFmpeg command and capture the output.""" | |
try: | |
result = subprocess.run(command, shell=True, capture_output=True, text=True) | |
return result.stdout, result.stderr | |
except Exception as e: | |
return "", str(e) | |
# Streamlit UI | |
st.title("FFmpeg Command Runner") | |
# File uploader | |
uploaded_file = st.file_uploader("Upload a file", type=None) | |
# Text area for FFmpeg commands | |
ffmpeg_command = st.text_area("Enter your FFmpeg command", "") | |
# Execute button | |
if st.button("Run FFmpeg Command"): | |
if uploaded_file: | |
# Save the uploaded file | |
uploaded_file_path = save_uploaded_file(uploaded_file) | |
st.write(f"File saved at: {uploaded_file_path}") | |
# Replace placeholder with the uploaded file path in the FFmpeg command | |
command_to_run = ffmpeg_command.replace("<input_file>", uploaded_file_path) | |
# Run the command | |
stdout, stderr = run_ffmpeg(command_to_run) | |
# Display the logs | |
st.text_area("FFmpeg Output", stdout) | |
st.text_area("FFmpeg Errors", stderr) | |
# Check for a successful FFmpeg process and show the video player if the output file exists | |
output_path = os.path.join(PROCESSED_FOLDER, "output.mp4") # Change as needed | |
if os.path.exists(output_path): | |
st.video(output_path) | |
else: | |
st.write("Please upload a file before running the command.") |