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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -5,12 +5,10 @@ import ffmpeg # Import the ffmpeg-python library
5
 
6
  # Directories for uploads and processing
7
  UPLOAD_FOLDER = 'uploads'
8
- PROCESSED_FOLDER = 'processed'
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):
@@ -24,9 +22,10 @@ def save_uploaded_file(uploaded_file):
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)
@@ -38,7 +37,7 @@ st.title("FFmpeg Command Runner")
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"):
@@ -47,8 +46,9 @@ if st.button("Run FFmpeg Command"):
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)
@@ -57,10 +57,11 @@ if st.button("Run FFmpeg Command"):
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.")
 
5
 
6
  # Directories for uploads and processing
7
  UPLOAD_FOLDER = 'uploads'
 
8
  os.makedirs(UPLOAD_FOLDER, exist_ok=True)
 
9
 
10
  def save_uploaded_file(uploaded_file):
11
+ """Save uploaded file to the uploads folder, keeping its original name or creating a unique name if it already exists."""
12
  file_path = os.path.join(UPLOAD_FOLDER, uploaded_file.name)
13
  # If a file with the same name already exists, generate a unique name
14
  if os.path.exists(file_path):
 
22
  return file_path
23
 
24
  def run_ffmpeg(command):
25
+ """Run the FFmpeg command in the uploads folder and capture the output."""
26
  try:
27
+ # Change working directory to the uploads folder
28
+ result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=UPLOAD_FOLDER)
29
  return result.stdout, result.stderr
30
  except Exception as e:
31
  return "", str(e)
 
37
  uploaded_file = st.file_uploader("Upload a file", type=None)
38
 
39
  # Text area for FFmpeg commands
40
+ ffmpeg_command = st.text_area("Enter your FFmpeg command (use '<input_file>' for the uploaded file name)", "")
41
 
42
  # Execute button
43
  if st.button("Run FFmpeg Command"):
 
46
  uploaded_file_path = save_uploaded_file(uploaded_file)
47
  st.write(f"File saved at: {uploaded_file_path}")
48
 
49
+ # Replace placeholder with the uploaded file name (just the file name, not the full path)
50
+ uploaded_file_name = os.path.basename(uploaded_file_path)
51
+ command_to_run = ffmpeg_command.replace("<input_file>", uploaded_file_name)
52
 
53
  # Run the command
54
  stdout, stderr = run_ffmpeg(command_to_run)
 
57
  st.text_area("FFmpeg Output", stdout)
58
  st.text_area("FFmpeg Errors", stderr)
59
 
60
+ # Check if an output file is generated and show the video player
61
+ for file in os.listdir(UPLOAD_FOLDER):
62
+ if file.endswith(".mp4") or file.endswith(".mkv") or file.endswith(".avi"): # Add other formats as needed
63
+ output_path = os.path.join(UPLOAD_FOLDER, file)
64
+ st.video(output_path)
65
 
66
  else:
67
  st.write("Please upload a file before running the command.")