fffiloni commited on
Commit
2768572
1 Parent(s): 7cf7ad0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -49
app.py CHANGED
@@ -1,53 +1,17 @@
1
  import gradio as gr
2
  import subprocess
3
- import cv2
4
- import ffmpeg
5
 
6
- def convert_video(input_file, output_file, codec='mp4v'):
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)
29
- else:
30
- break
31
-
32
- # Release video objects
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
- # Run FFmpeg command and capture stderr output
40
- try:
41
- ffmpeg.run(stream, capture_stderr=True)
42
- except subprocess.CalledProcessError as e:
43
- # Print stderr output if an error occurs
44
- print(f"FFmpeg error: {e.stderr.decode()}")
45
- raise e
46
 
47
- print(f"Video converted successfully: {output_path}")
48
 
49
- except Exception as e:
50
- print(f"Error converting video: {e}")
51
 
52
  def execute_command(command: str) -> None:
53
  subprocess.run(command, check=True)
@@ -71,11 +35,13 @@ def infer():
71
  execute_command(command)
72
 
73
  # Convert video to compatible codecs
74
- input_file_path = f"output_video/{output_name}.mp4"
75
- output_file_path = f"{output_name}.mp4"
76
- #convert_video(input_file_path, output_file_path)
 
 
77
 
78
- return f"output_video/{output_name}.mp4"
79
 
80
  with gr.Blocks() as demo:
81
  with gr.Column():
 
1
  import gradio as gr
2
  import subprocess
3
+ from moviepy.editor import VideoFileClip
 
4
 
5
+ def convert_to_mp4_with_aac(input_path, output_path):
6
+ # Load the video
7
+ video = VideoFileClip(input_path)
8
+
9
+ # Set the output format to mp4 with AAC codec
10
+ video.write_videofile(output_path, codec="libx264", audio_codec="aac")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ return output_path
13
 
14
+
 
15
 
16
  def execute_command(command: str) -> None:
17
  subprocess.run(command, check=True)
 
35
  execute_command(command)
36
 
37
  # Convert video to compatible codecs
38
+ input_file = f"output_video/{output_name}.mp4"
39
+ output_file = f"{output_name}.mp4"
40
+
41
+ result = convert_to_mp4_with_aac(input_file, output_file)
42
+
43
 
44
+ return result
45
 
46
  with gr.Blocks() as demo:
47
  with gr.Column():