MCPcopy Index your code
hub / github.com/hzwer/ECCV2022-RIFE / transferAudio

Function transferAudio

inference_video.py:16–54  ·  view source on GitHub ↗
(sourceVideo, targetVideo)

Source from the content-addressed store, hash-verified

14warnings.filterwarnings("ignore")
15
16def 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
56parser = argparse.ArgumentParser(description='Interpolation for a pair of images')
57parser.add_argument('--video', dest='video', type=str, default=None)

Callers 1

inference_video.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected