Spaces:
Running
Running
RandomPersonRR
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
import subprocess
|
4 |
-
import ffmpeg
|
5 |
|
6 |
# Directories for uploads and processing
|
7 |
UPLOAD_FOLDER = 'uploads'
|
@@ -9,61 +9,58 @@ PROCESSED_FOLDER = 'processed'
|
|
9 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
10 |
os.makedirs(PROCESSED_FOLDER, exist_ok=True)
|
11 |
|
12 |
-
def
|
13 |
-
"""
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
i
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Streamlit UI
|
23 |
-
st.title(
|
24 |
|
25 |
-
# File
|
26 |
-
uploaded_file = st.file_uploader("Upload a file", type=None)
|
27 |
-
if uploaded_file:
|
28 |
-
new_filename = get_new_filename(uploaded_file.name)
|
29 |
-
file_path = os.path.join(UPLOAD_FOLDER, new_filename)
|
30 |
-
with open(file_path, 'wb') as f:
|
31 |
-
f.write(uploaded_file.getbuffer())
|
32 |
-
st.success(f'File uploaded successfully as {new_filename}!')
|
33 |
|
34 |
-
# FFmpeg
|
35 |
-
ffmpeg_command = st.text_area("Enter FFmpeg command",
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
# Prepare FFmpeg command
|
48 |
-
command = f"ffmpeg -i {input_file} {ffmpeg_command}"
|
49 |
-
|
50 |
-
# Replace 'output' placeholder in the command with the actual output file path
|
51 |
-
command = command.replace('output', output_file)
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
st.warning("No video file found. Please check the command.")
|
68 |
-
except subprocess.CalledProcessError as e:
|
69 |
-
st.error(f"Error executing command: {e}")
|
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
import subprocess
|
4 |
+
import ffmpeg # Import the ffmpeg-python library
|
5 |
|
6 |
# Directories for uploads and processing
|
7 |
UPLOAD_FOLDER = 'uploads'
|
|
|
9 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
10 |
os.makedirs(PROCESSED_FOLDER, exist_ok=True)
|
11 |
|
12 |
+
def save_uploaded_file(uploaded_file):
|
13 |
+
"""Save uploaded file to a designated folder without renaming it unless a file with the same name exists."""
|
14 |
+
file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
|
15 |
+
# If a file with the same name already exists, generate a unique name
|
16 |
+
if os.path.exists(file_path):
|
17 |
+
base, ext = os.path.splitext(uploaded_file.name)
|
18 |
+
i = 1
|
19 |
+
while os.path.exists(os.path.join(UPLOAD_FOLDER, f"{base}_{i}{ext}")):
|
20 |
+
i += 1
|
21 |
+
file_path = os.path.join(UPLOAD_FOLDER, f"{base}_{i}{ext}")
|
22 |
+
with open(file_path, "wb") as f:
|
23 |
+
f.write(uploaded_file.getbuffer())
|
24 |
+
return file_path
|
25 |
+
|
26 |
+
def run_ffmpeg(command):
|
27 |
+
"""Run the FFmpeg command and capture the output."""
|
28 |
+
try:
|
29 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
30 |
+
return result.stdout, result.stderr
|
31 |
+
except Exception as e:
|
32 |
+
return "", str(e)
|
33 |
|
34 |
# Streamlit UI
|
35 |
+
st.title("FFmpeg Command Runner")
|
36 |
|
37 |
+
# File uploader
|
38 |
+
uploaded_file = st.file_uploader("Upload a file", type=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
# Text area for FFmpeg commands
|
41 |
+
ffmpeg_command = st.text_area("Enter your FFmpeg command", "")
|
42 |
|
43 |
+
# Execute button
|
44 |
+
if st.button("Run FFmpeg Command"):
|
45 |
+
if uploaded_file:
|
46 |
+
# Save the uploaded file
|
47 |
+
uploaded_file_path = save_uploaded_file(uploaded_file)
|
48 |
+
st.write(f"File saved at: {uploaded_file_path}")
|
49 |
+
|
50 |
+
# Replace placeholder with the uploaded file path in the FFmpeg command
|
51 |
+
command_to_run = ffmpeg_command.replace("<input_file>", uploaded_file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# Run the command
|
54 |
+
stdout, stderr = run_ffmpeg(command_to_run)
|
55 |
+
|
56 |
+
# Display the logs
|
57 |
+
st.text_area("FFmpeg Output", stdout)
|
58 |
+
st.text_area("FFmpeg Errors", stderr)
|
59 |
+
|
60 |
+
# Check for a successful FFmpeg process and show the video player if the output file exists
|
61 |
+
output_path = os.path.join(PROCESSED_FOLDER, "output.mp4") # Change as needed
|
62 |
+
if os.path.exists(output_path):
|
63 |
+
st.video(output_path)
|
64 |
+
|
65 |
+
else:
|
66 |
+
st.write("Please upload a file before running the command.")
|
|
|
|
|
|