Spaces:
Sleeping
Sleeping
File size: 2,629 Bytes
eefaa37 00cb808 172af62 322ee9c eefaa37 00cb808 4cf9976 322ee9c 347f6f1 322ee9c 347f6f1 322ee9c 347f6f1 322ee9c e6b42eb 00cb808 322ee9c eefaa37 322ee9c eefaa37 322ee9c 347f6f1 8df4907 322ee9c 347f6f1 172af62 322ee9c 347f6f1 322ee9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
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.") |