fffiloni commited on
Commit
537ba12
1 Parent(s): fed1557

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -1,26 +1,28 @@
1
  import gradio as gr
2
  import subprocess
3
  import cv2
 
4
 
5
- def convert_video(input_file, output_file, codec='mp4v'):
6
  try:
7
  # Define input and output files
8
  input_path = input_file
9
  output_path = output_file
10
 
11
- # Open input video file
12
  cap = cv2.VideoCapture(input_path)
13
 
14
  # Get video codec and frame dimensions
15
  fourcc = cv2.VideoWriter_fourcc(*codec)
16
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
17
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
 
18
 
19
  # Create output video writer
20
- out = cv2.VideoWriter(output_path, fourcc, 20.0, (width, height))
21
 
22
- # Read and write video frames
23
- while(cap.isOpened()):
24
  ret, frame = cap.read()
25
  if ret:
26
  out.write(frame)
@@ -31,12 +33,16 @@ def convert_video(input_file, output_file, codec='mp4v'):
31
  cap.release()
32
  out.release()
33
 
 
 
 
 
 
34
  print(f"Video converted successfully: {output_path}")
35
 
36
  except Exception as e:
37
  print(f"Error converting video: {e}")
38
 
39
-
40
  def execute_command(command: str) -> None:
41
  subprocess.run(command, check=True)
42
 
 
1
  import gradio as gr
2
  import subprocess
3
  import cv2
4
+ import ffmpeg
5
 
6
+ def convert_video(input_file, output_file, codec='libx264'):
7
  try:
8
  # Define input and output files
9
  input_path = input_file
10
  output_path = output_file
11
 
12
+ # Read input video file
13
  cap = cv2.VideoCapture(input_path)
14
 
15
  # Get video codec and frame dimensions
16
  fourcc = cv2.VideoWriter_fourcc(*codec)
17
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
18
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
19
+ fps = cap.get(cv2.CAP_PROP_FPS)
20
 
21
  # Create output video writer
22
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
23
 
24
+ # Process and write video frames
25
+ while cap.isOpened():
26
  ret, frame = cap.read()
27
  if ret:
28
  out.write(frame)
 
33
  cap.release()
34
  out.release()
35
 
36
+ # Process and write audio stream using ffmpeg
37
+ stream = ffmpeg.input(input_path)
38
+ stream = ffmpeg.output(stream, output_path, acodec='aac', vcodec=codec, strict='experimental', loglevel='error')
39
+ ffmpeg.run(stream)
40
+
41
  print(f"Video converted successfully: {output_path}")
42
 
43
  except Exception as e:
44
  print(f"Error converting video: {e}")
45
 
 
46
  def execute_command(command: str) -> None:
47
  subprocess.run(command, check=True)
48