| 14 | warnings.filterwarnings("ignore") |
| 15 | |
| 16 | def transferAudio(sourceVideo, targetVideo): |
| 17 | import shutil |
| 18 | import moviepy.editor |
| 19 | tempAudioFileName = "./temp/audio.mkv" |
| 20 | |
| 21 | # split audio from original video file and store in "temp" directory |
| 22 | if True: |
| 23 | |
| 24 | # clear old "temp" directory if it exits |
| 25 | if os.path.isdir("temp"): |
| 26 | # remove temp directory |
| 27 | shutil.rmtree("temp") |
| 28 | # create new "temp" directory |
| 29 | os.makedirs("temp") |
| 30 | # extract audio from video |
| 31 | os.system('ffmpeg -y -i "{}" -c:a copy -vn {}'.format(sourceVideo, tempAudioFileName)) |
| 32 | |
| 33 | targetNoAudio = os.path.splitext(targetVideo)[0] + "_noaudio" + os.path.splitext(targetVideo)[1] |
| 34 | os.rename(targetVideo, targetNoAudio) |
| 35 | # combine audio file and new video file |
| 36 | os.system('ffmpeg -y -i "{}" -i {} -c copy "{}"'.format(targetNoAudio, tempAudioFileName, targetVideo)) |
| 37 | |
| 38 | if os.path.getsize(targetVideo) == 0: # if ffmpeg failed to merge the video and audio together try converting the audio to aac |
| 39 | tempAudioFileName = "./temp/audio.m4a" |
| 40 | os.system('ffmpeg -y -i "{}" -c:a aac -b:a 160k -vn {}'.format(sourceVideo, tempAudioFileName)) |
| 41 | os.system('ffmpeg -y -i "{}" -i {} -c copy "{}"'.format(targetNoAudio, tempAudioFileName, targetVideo)) |
| 42 | if (os.path.getsize(targetVideo) == 0): # if aac is not supported by selected format |
| 43 | os.rename(targetNoAudio, targetVideo) |
| 44 | print("Audio transfer failed. Interpolated video will have no audio") |
| 45 | else: |
| 46 | print("Lossless audio transfer failed. Audio was transcoded to AAC (M4A) instead.") |
| 47 | |
| 48 | # remove audio-less video |
| 49 | os.remove(targetNoAudio) |
| 50 | else: |
| 51 | os.remove(targetNoAudio) |
| 52 | |
| 53 | # remove temp directory |
| 54 | shutil.rmtree("temp") |
| 55 | |
| 56 | parser = argparse.ArgumentParser(description='Interpolation for a pair of images') |
| 57 | parser.add_argument('--video', dest='video', type=str, default=None) |