diff --git a/README.md b/README.md index e15c254..7656a8d 100644 --- a/README.md +++ b/README.md @@ -67,8 +67,6 @@ $ python3 inference_video_parallel.py --exp=2 --video=video.mp4 The warning info, 'Warning: Your video has *** static frames, it may change the duration of the generated video.' means that your video has changed the frame rate by adding static frames, it is common if you have processed 25FPS video to 30FPS. -**To combine the video and audio, please check [this issue](https://github.com/hzwer/arXiv2020-RIFE/issues/12#issuecomment-731715507).** - **Image Interpolation** ``` diff --git a/inference_video.py b/inference_video.py index da7cfff..90e6383 100644 --- a/inference_video.py +++ b/inference_video.py @@ -9,6 +9,48 @@ import warnings import _thread import skvideo.io from queue import Queue, Empty +import moviepy.editor +import shutil + +def transferAudio(sourceVideo, targetVideo): + + tempAudioFileName = "./temp/audio.mp3" + + try: + # split audio from original video file and store in "temp" directory + if True: + + # extract audio from video + if True: + video = moviepy.editor.VideoFileClip(sourceVideo) + audio = video.audio + + # clear old "temp" directory if it exits + if os.path.isdir("temp"): + # remove temp directory + shutil.rmtree("temp") + + # create new "temp" directory + os.makedirs("temp") + + # write audio file to "temp" directory + audio.write_audiofile(tempAudioFileName) + + os.rename(targetVideo, "noAudio_"+targetVideo) + + # combine audio file and new video file + os.system("ffmpeg -y -i " + "noAudio_"+targetVideo + " -i " + tempAudioFileName + " -c copy " + targetVideo) + + # remove audio-less video + os.remove("noAudio_"+targetVideo) + except: + pass + + # remove temp directory + shutil.rmtree("temp") + + + warnings.filterwarnings("ignore") device = torch.device("cuda" if torch.cuda.is_available() else "cpu") @@ -40,7 +82,10 @@ fps = videoCapture.get(cv2.CAP_PROP_FPS) tot_frame = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT) videoCapture.release() if args.fps is None: + fpsNotAssigned = True args.fps = fps * args.exp +else: + fpsNotAssigned = False videogen = skvideo.io.vreader(args.video) lastframe = next(videogen) h, w, _ = lastframe.shape @@ -130,3 +175,8 @@ while(not buffer.empty()): pbar.close() if not vid_out is None: vid_out.release() + +# move audio to new video file if appropriate +if args.png == False and fpsNotAssigned == True: + outputVideoFileName = '{}_{}X_{}fps.{}'.format(video_path_wo_ext, args.exp, int(np.round(args.fps)), args.ext) + transferAudio(video_path_wo_ext + "." + args.ext, outputVideoFileName) \ No newline at end of file