RandomPersonRR commited on
Commit
322ee9c
·
verified ·
1 Parent(s): e416331

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -53
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 get_new_filename(original_filename):
13
- """Generate a new filename with an incremented number."""
14
- base, ext = os.path.splitext(original_filename)
15
- i = 1
16
- new_filename = f"{base}_{i}{ext}"
17
- while os.path.exists(os.path.join(UPLOAD_FOLDER, new_filename)):
18
- i += 1
19
- new_filename = f"{base}_{i}{ext}"
20
- return new_filename
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Streamlit UI
23
- st.title('FFmpeg Command Executor with ffmpeg-python')
24
 
25
- # File upload
26
- uploaded_file = st.file_uploader("Upload a file", type=None) # Accept any file type
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 command input
35
- ffmpeg_command = st.text_area("Enter FFmpeg command", placeholder="e.g., -vf scale=640:480 output.mp4")
36
 
37
- if st.button('Run Command'):
38
- if not uploaded_file:
39
- st.error("Please upload a file before running the command.")
40
- elif not ffmpeg_command:
41
- st.error("Please enter an FFmpeg command.")
42
- else:
43
- # Build paths
44
- input_file = os.path.join(UPLOAD_FOLDER, new_filename)
45
- output_file = os.path.join(PROCESSED_FOLDER, 'output.mp4')
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
- try:
54
- # Execute FFmpeg command
55
- result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
56
- st.success("Command executed successfully!")
57
-
58
- # Display logs
59
- st.subheader('FFmpeg Logs:')
60
- st.code(result.stdout)
61
-
62
- # Display video player
63
- if os.path.exists(output_file):
64
- st.subheader('Processed Video:')
65
- st.video(output_file)
66
- else:
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.")