Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import subprocess | |
import ffmpeg # Import the ffmpeg-python library | |
# Directories for uploads and processing | |
UPLOAD_FOLDER = 'uploads' | |
os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
def save_uploaded_file(uploaded_file): | |
"""Save uploaded file to the uploads folder, keeping its original name or creating a unique name if it already 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 in the uploads folder and capture the output.""" | |
try: | |
# Change working directory to the uploads folder | |
result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=UPLOAD_FOLDER) | |
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 (use '<input_file>' for the uploaded file name)", "") | |
# 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 name (just the file name, not the full path) | |
uploaded_file_name = os.path.basename(uploaded_file_path) | |
command_to_run = ffmpeg_command.replace("<input_file>", uploaded_file_name) | |
# 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 if an output file is generated and show the video player | |
for file in os.listdir(UPLOAD_FOLDER): | |
if file.endswith(".mp4") or file.endswith(".mkv") or file.endswith(".avi"): # Add other formats as needed | |
output_path = os.path.join(UPLOAD_FOLDER, file) | |
st.video(output_path) | |
else: | |
st.write("Please upload a file before running the command.") |