Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import subprocess | |
import ffmpeg | |
# 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 get_new_filename(original_filename): | |
"""Generate a new filename with an incremented number.""" | |
base, ext = os.path.splitext(original_filename) | |
i = 1 | |
new_filename = f"{base}_{i}{ext}" | |
while os.path.exists(os.path.join(UPLOAD_FOLDER, new_filename)): | |
i += 1 | |
new_filename = f"{base}_{i}{ext}" | |
return new_filename | |
# Streamlit UI | |
st.title('FFmpeg Command Executor with ffmpeg-python') | |
# File upload | |
uploaded_file = st.file_uploader("Upload a file", type=None) # Accept any file type | |
if uploaded_file: | |
new_filename = get_new_filename(uploaded_file.name) | |
file_path = os.path.join(UPLOAD_FOLDER, new_filename) | |
with open(file_path, 'wb') as f: | |
f.write(uploaded_file.getbuffer()) | |
st.success(f'File uploaded successfully as {new_filename}!') | |
# FFmpeg command input | |
ffmpeg_command = st.text_area("Enter FFmpeg command", placeholder="e.g., -vf scale=640:480 output.mp4") | |
if st.button('Run Command'): | |
if not uploaded_file: | |
st.error("Please upload a file before running the command.") | |
elif not ffmpeg_command: | |
st.error("Please enter an FFmpeg command.") | |
else: | |
# Build paths | |
input_file = os.path.join(UPLOAD_FOLDER, new_filename) | |
output_file = os.path.join(PROCESSED_FOLDER, 'output.mp4') | |
# Prepare FFmpeg command | |
command = f"ffmpeg -i {input_file} {ffmpeg_command}" | |
# Replace 'output' placeholder in the command with the actual output file path | |
command = command.replace('output', output_file) | |
try: | |
# Execute FFmpeg command | |
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True) | |
st.success("Command executed successfully!") | |
# Display logs | |
st.subheader('FFmpeg Logs:') | |
st.code(result.stdout) | |
# Display video player | |
if os.path.exists(output_file): | |
st.subheader('Processed Video:') | |
st.video(output_file) | |
else: | |
st.warning("No video file found. Please check the command.") | |
except subprocess.CalledProcessError as e: | |
st.error(f"Error executing command: {e}") |