RandomPersonRR commited on
Commit
172af62
·
verified ·
1 Parent(s): 00cb808

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -11
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
- import subprocess
3
  import os
 
4
 
5
  # Directories for uploads and processing
6
  UPLOAD_FOLDER = 'uploads'
@@ -9,17 +10,18 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
9
  os.makedirs(PROCESSED_FOLDER, exist_ok=True)
10
 
11
  # Streamlit UI
12
- st.title('FFmpeg Command Executor')
13
 
14
  # File upload
15
- uploaded_file = st.file_uploader("Upload a file", type=['mp4', 'mkv', 'avi', 'mov'])
16
  if uploaded_file:
17
- with open(os.path.join(UPLOAD_FOLDER, uploaded_file.name), 'wb') as f:
 
18
  f.write(uploaded_file.getbuffer())
19
  st.success('File uploaded successfully!')
20
 
21
  # FFmpeg command input
22
- ffmpeg_command = st.text_area("Enter FFmpeg command", placeholder="e.g., ffmpeg -i input.mp4 -vf scale=640:480 output.mp4")
23
 
24
  if st.button('Run Command'):
25
  if not uploaded_file:
@@ -27,9 +29,16 @@ if st.button('Run Command'):
27
  elif not ffmpeg_command:
28
  st.error("Please enter an FFmpeg command.")
29
  else:
30
- # Run FFmpeg command
31
- command = ffmpeg_command.replace("input", os.path.join(UPLOAD_FOLDER, uploaded_file.name)).replace("output", os.path.join(PROCESSED_FOLDER, 'output.mp4'))
 
 
 
 
 
 
32
  try:
 
33
  result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
34
  st.success("Command executed successfully!")
35
 
@@ -38,11 +47,39 @@ if st.button('Run Command'):
38
  st.code(result.stdout)
39
 
40
  # Display video player
41
- video_file = os.path.join(PROCESSED_FOLDER, 'output.mp4')
42
- if os.path.exists(video_file):
43
  st.subheader('Processed Video:')
44
- st.video(video_file)
45
  else:
46
  st.warning("No video file found. Please check the command.")
47
  except subprocess.CalledProcessError as e:
48
- st.error(f"Error executing command: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import ffmpeg
3
  import os
4
+ import subprocess
5
 
6
  # Directories for uploads and processing
7
  UPLOAD_FOLDER = 'uploads'
 
10
  os.makedirs(PROCESSED_FOLDER, exist_ok=True)
11
 
12
  # Streamlit UI
13
+ st.title('FFmpeg Command Executor with ffmpeg-python')
14
 
15
  # File upload
16
+ uploaded_file = st.file_uploader("Upload a file", type=None) # Accept any file type
17
  if uploaded_file:
18
+ file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
19
+ with open(file_path, 'wb') as f:
20
  f.write(uploaded_file.getbuffer())
21
  st.success('File uploaded successfully!')
22
 
23
  # FFmpeg command input
24
+ ffmpeg_command = st.text_area("Enter FFmpeg command (e.g., -vf scale=640:480 output.mp4)", placeholder="e.g., -vf scale=640:480 output.mp4")
25
 
26
  if st.button('Run Command'):
27
  if not uploaded_file:
 
29
  elif not ffmpeg_command:
30
  st.error("Please enter an FFmpeg command.")
31
  else:
32
+ # Build paths
33
+ input_file = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
34
+ output_file = os.path.join(PROCESSED_FOLDER, 'output.mp4')
35
+
36
+ # Prepare FFmpeg command
37
+ # Ensure the output filename is correctly used
38
+ command = f"ffmpeg -i {input_file} {ffmpeg_command.replace('output', output_file)}"
39
+
40
  try:
41
+ # Execute FFmpeg command
42
  result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
43
  st.success("Command executed successfully!")
44
 
 
47
  st.code(result.stdout)
48
 
49
  # Display video player
50
+ if os.path.exists(output_file):
 
51
  st.subheader('Processed Video:')
52
+ st.video(output_file)
53
  else:
54
  st.warning("No video file found. Please check the command.")
55
  except subprocess.CalledProcessError as e:
56
+ st.error(f"Error executing command: {e}")
57
+
58
+ # Using ffmpeg-python to run commands directly
59
+ def run_ffmpeg_command(input_path, output_path, command_args):
60
+ try:
61
+ # ffmpeg-python command execution
62
+ ffmpeg.input(input_path).output(output_path, **command_args).run(overwrite_output=True)
63
+ st.success("Command executed successfully with ffmpeg-python!")
64
+
65
+ # Display processed video
66
+ st.subheader('Processed Video:')
67
+ st.video(output_path)
68
+ except ffmpeg.Error as e:
69
+ st.error(f"Error executing command with ffmpeg-python: {e}")
70
+
71
+ # Example usage of ffmpeg-python
72
+ if st.button('Run ffmpeg-python Command'):
73
+ if not uploaded_file:
74
+ st.error("Please upload a file before running the command.")
75
+ elif not ffmpeg_command:
76
+ st.error("Please enter an FFmpeg command.")
77
+ else:
78
+ input_file = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
79
+ output_file = os.path.join(PROCESSED_FOLDER, 'output.mp4')
80
+
81
+ # Define the command arguments
82
+ # Convert ffmpeg_command into a dictionary format suitable for ffmpeg-python
83
+ command_args = {} # You need to parse the `ffmpeg_command` into this dictionary
84
+
85
+ run_ffmpeg_command(input_file, output_file, command_args)