Browse Source

Removing dependency on moviepy; adding more error handling and simplifying

Jason Antic 6 years ago
parent
commit
060b552a3d
3 changed files with 21 additions and 21 deletions
  1. 1 2
      app.py
  2. 20 18
      fasterai/visualize.py
  3. 0 1
      requirements.txt

+ 1 - 2
app.py

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

+ 20 - 18
fasterai/visualize.py

@@ -17,8 +17,6 @@ import base64
 from IPython import display as ipythondisplay
 from IPython import display as ipythondisplay
 from IPython.display import HTML
 from IPython.display import HTML
 from IPython.display import Image as ipythonimage
 from IPython.display import Image as ipythonimage
-import subprocess
-from moviepy.editor import *
 
 
 class ModelImageVisualizer():
 class ModelImageVisualizer():
     def __init__(self, filter:IFilter, results_dir:str=None):
     def __init__(self, filter:IFilter, results_dir:str=None):
@@ -145,29 +143,33 @@ class VideoColorizer():
                 color_image.save(str(colorframes_folder/img))
                 color_image.save(str(colorframes_folder/img))
     
     
     def _build_video(self, source_path:Path)->Path:
     def _build_video(self, source_path:Path)->Path:
-        result_path = self.result_folder/source_path.name
+        colorized_path = self.result_folder/(source_path.name.replace('.mp4', '_no_audio.mp4'))
         colorframes_folder = self.colorframes_root/(source_path.stem)
         colorframes_folder = self.colorframes_root/(source_path.stem)
         colorframes_path_template = str(colorframes_folder/'%5d.jpg')
         colorframes_path_template = str(colorframes_folder/'%5d.jpg')
-        result_path.parent.mkdir(parents=True, exist_ok=True)
-        if result_path.exists(): result_path.unlink()
+        colorized_path.parent.mkdir(parents=True, exist_ok=True)
+        if colorized_path.exists(): colorized_path.unlink()
         fps = self._get_fps(source_path)
         fps = self._get_fps(source_path)
 
 
         ffmpeg.input(str(colorframes_path_template), format='image2', vcodec='mjpeg', framerate=str(fps)) \
         ffmpeg.input(str(colorframes_path_template), format='image2', vcodec='mjpeg', framerate=str(fps)) \
-            .output(str(result_path), crf=17, vcodec='libx264') \
+            .output(str(colorized_path), crf=17, vcodec='libx264') \
             .run(capture_stdout=True)
             .run(capture_stdout=True)
 
 
-        # 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='libx264', audio_codec='aac', threads=64, 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
+        result_path = self.result_folder/source_path.name
+        if result_path.exists(): result_path.unlink()
+        #making copy of non-audio version in case adding back audio doesn't apply or fails.
+        shutil.copyfile(str(colorized_path), str(result_path))
+
+        # adding back sound here
+        audio_file = Path(str(source_path).replace('.mp4', '.aac'))
+        if audio_file.exists(): audio_file.unlink()
+
+        os.system('ffmpeg -y -i "' + str(source_path) + '" -vn -acodec copy "' + str(audio_file) + '"')
+
+        if audio_file.exists:
+            os.system('ffmpeg -y -i "' + str(colorized_path) + '" -i "' + str(audio_file) 
+                + '" -shortest -c:v copy -c:a aac -b:a 256k "' + str(result_path) + '"')
+        print('Video created here: ' + str(result_path))
+        return result_path
 
 
     def colorize_from_url(self, source_url, file_name:str, render_factor:int=None)->Path: 
     def colorize_from_url(self, source_url, file_name:str, render_factor:int=None)->Path: 
         source_path =  self.source_folder/file_name
         source_path =  self.source_folder/file_name

+ 0 - 1
requirements.txt

@@ -5,4 +5,3 @@ ffmpeg-python==0.1.17
 youtube-dl>=2019.4.17
 youtube-dl>=2019.4.17
 jupyterlab
 jupyterlab
 opencv-python>=3.3.0.10
 opencv-python>=3.3.0.10
-moviepy