jantic / DeOldify

A Deep Learning based project for colorizing and restoring old images (and video!)
MIT License
18.01k stars 2.57k forks source link

Add back audio from source when available to video renders. #88

Closed jantic closed 5 years ago

jantic commented 5 years ago

Title says it all. Having audio dropping from videos sucks.

lf-openthos commented 5 years ago

It's easy for ffmpeg to remux the audio track back from the original one. Such as

ffmpeg -i original.video -vn -acodec copy temp.aac ffmpeg -i results.video -i temp.aac -codec copy final.video

jantic commented 5 years ago

Thanks! I'll try this soon.

btahir commented 5 years ago

This is what you need:

from moviepy.editor import *

# get the original video
videoclip = VideoFileClip('original.mp4')

# extract the audio from video
audioclip = videoclip.audio

# DeOldify video as usual and get the colorized output
video = VideoFileClip('colorized_output.mp4')

# set audio
final = video.set_audio(audioclip)

# save with required settings (mostly to make Quicktime happy and recognize the audio): https://github.com/Zulko/moviepy/issues/820
final.write_videofile("final_output.mp4", codec='mpeg4', audio_codec='aac', temp_audiofile='temp-audio.m4a', remove_temp=True)

You can look at the options for saving here: https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html

Quicktime is really annoying and won't recognize the audio so you have to set those codecs for it.

Love the project. Great job! :)

btahir commented 5 years ago

Working pretty well :)

https://www.youtube.com/watch?v=JACcWJRUkYc&feature=youtu.be

jqueguiner commented 5 years ago

https://github.com/jantic/DeOldify/pull/108

btahir commented 5 years ago

Sweet! One more thing I should mention. I realized that writing the Video to mpeg4 leads to bad quality video (for some reason). If you change the codec to libx264, the quality is much better!

final.write_videofile(out, codec='libx264', audio_codec='aac', temp_audiofile='temp-audio.m4a', remove_temp=True)

btahir commented 5 years ago

Bonus: You can add a thread parameter to leverage multithreading which leads to a much faster operation. Example:

final.write_videofile(out, codec='libx264', threads=64, audio_codec='aac', temp_audiofile='temp-audio.m4a', remove_temp=True)

jqueguiner commented 5 years ago

Yep also saw the bad quality, fixing it

jqueguiner commented 5 years ago

https://github.com/jantic/DeOldify/pull/108/commits/3df2647500a4f82571beac0fc5101395390c9bc0 Fixed and tested

jantic commented 5 years ago

Ok guys, this functionality is merged in now. Thanks!