Browse Source

add audio to video

jqueguiner 6 years ago
parent
commit
ed3e4ae5a6
2 changed files with 17 additions and 4 deletions
  1. 2 1
      app.py
  2. 15 3
      fasterai/visualize.py

+ 2 - 1
app.py

@@ -62,9 +62,10 @@ def process_video():
     random_filename = str(uuid4()) + '.mp4'
 
     video_path = video_colorizer.colorize_from_url(source_url, random_filename, render_factor)
-    callback = send_file(os.path.join("video/result/", random_filename), mimetype='application/octet-stream')
+    callback = send_file(os.path.join("video/result/", random_filename.replace('.mp4', '-w-audio.mp4')), mimetype='application/octet-stream')
 
     os.remove(os.path.join("video/result/", random_filename))
+    os.remove(os.path.join("video/result/", random_filename.replace('.mp4', '-w-audio.mp4')))
 
     return callback
 

+ 15 - 3
fasterai/visualize.py

@@ -17,6 +17,8 @@ import base64
 from IPython import display as ipythondisplay
 from IPython.display import HTML
 from IPython.display import Image as ipythonimage
+import subprocess
+from moviepy.editor import *
 
 class ModelImageVisualizer():
     def __init__(self, filter:IFilter, results_dir:str=None):
@@ -153,9 +155,19 @@ class VideoColorizer():
         ffmpeg.input(str(colorframes_path_template), format='image2', vcodec='mjpeg', framerate=str(fps)) \
             .output(str(result_path), crf=17, vcodec='libx264') \
             .run(capture_stdout=True)
-        
-        print('Video created here: ' + str(result_path))
-        return result_path
+
+        # replace output video sound (blank) by original video sound
+        # kudos to https://github.com/btahir 
+        videoclip = VideoFileClip(str(source_path))
+        audioclip = videoclip.audio
+        video = VideoFileClip(str(result_path))
+        final = video.set_audio(audioclip)
+        out = str(result_path).replace('.mp4', '-w-audio.mp4')
+        final.write_videofile(out, codec='mpeg4', audio_codec='aac', temp_audiofile='temp-audio.m4a', remove_temp=True)
+
+        print('Video without audio created here: ' + str(result_path))
+        print('Video with audio created here: ' + str(out))
+        return out
 
     def colorize_from_url(self, source_url, file_name:str, render_factor:int=None)->Path: 
         source_path =  self.source_folder/file_name