r3gm commited on
Commit
0ca7d0d
·
verified ·
1 Parent(s): 4164ab4

Update inference_video.py

Browse files
Files changed (1) hide show
  1. inference_video.py +27 -13
inference_video.py CHANGED
@@ -10,23 +10,25 @@ import _thread
10
  import skvideo.io
11
  from queue import Queue, Empty
12
  from model.pytorch_msssim import ssim_matlab
 
 
13
 
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
 
@@ -36,7 +38,7 @@ def transferAudio(sourceVideo, targetVideo):
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
@@ -51,7 +53,8 @@ def transferAudio(sourceVideo, targetVideo):
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)
@@ -137,7 +140,17 @@ else:
137
  vid_out_name = args.output
138
  else:
139
  vid_out_name = '{}_{}X_{}fps.{}'.format(video_path_wo_ext, args.multi, int(np.round(args.fps)), args.ext)
140
- vid_out = cv2.VideoWriter(vid_out_name, fourcc, args.fps, (w, h))
 
 
 
 
 
 
 
 
 
 
141
 
142
  def clear_write_buffer(user_args, write_buffer):
143
  cnt = 0
@@ -149,7 +162,8 @@ def clear_write_buffer(user_args, write_buffer):
149
  cv2.imwrite('vid_out/{:0>7d}.png'.format(cnt), item[:, :, ::-1])
150
  cnt += 1
151
  else:
152
- vid_out.write(item[:, :, ::-1])
 
153
 
154
  def build_read_buffer(user_args, read_buffer, videogen):
155
  try:
@@ -278,7 +292,7 @@ while(not write_buffer.empty()):
278
  time.sleep(0.1)
279
  pbar.close()
280
  if not vid_out is None:
281
- vid_out.release()
282
 
283
  # move audio to new video file if appropriate
284
  if args.png == False and fpsNotAssigned == True and not args.video is None:
@@ -287,4 +301,4 @@ if args.png == False and fpsNotAssigned == True and not args.video is None:
287
  except:
288
  print("Audio transfer failed. Interpolated video will have no audio")
289
  targetNoAudio = os.path.splitext(vid_out_name)[0] + "_noaudio" + os.path.splitext(vid_out_name)[1]
290
- os.rename(targetNoAudio, vid_out_name)
 
10
  import skvideo.io
11
  from queue import Queue, Empty
12
  from model.pytorch_msssim import ssim_matlab
13
+ import shutil
14
+ import tempfile
15
 
16
  warnings.filterwarnings("ignore")
17
 
18
+
19
  def transferAudio(sourceVideo, targetVideo):
20
  import shutil
21
+ import tempfile
22
+
23
+ # generate a unique temp directory for this user
24
+ unique_temp_dir = tempfile.mkdtemp()
25
+ tempAudioFileName = os.path.join(unique_temp_dir, "audio.mkv")
26
 
27
  # split audio from original video file and store in "temp" directory
28
  if True:
29
 
30
  # clear old "temp" directory if it exits
31
+ # (This logic is replaced by mkdtemp which guarantees a new empty dir, but keeping comment structure)
 
 
 
 
32
  # extract audio from video
33
  os.system('ffmpeg -y -i "{}" -c:a copy -vn {}'.format(sourceVideo, tempAudioFileName))
34
 
 
38
  os.system('ffmpeg -y -i "{}" -i {} -c copy "{}"'.format(targetNoAudio, tempAudioFileName, targetVideo))
39
 
40
  if os.path.getsize(targetVideo) == 0: # if ffmpeg failed to merge the video and audio together try converting the audio to aac
41
+ tempAudioFileName = os.path.join(unique_temp_dir, "audio.m4a")
42
  os.system('ffmpeg -y -i "{}" -c:a aac -b:a 160k -vn {}'.format(sourceVideo, tempAudioFileName))
43
  os.system('ffmpeg -y -i "{}" -i {} -c copy "{}"'.format(targetNoAudio, tempAudioFileName, targetVideo))
44
  if (os.path.getsize(targetVideo) == 0): # if aac is not supported by selected format
 
53
  os.remove(targetNoAudio)
54
 
55
  # remove temp directory
56
+ shutil.rmtree(unique_temp_dir)
57
+
58
 
59
  parser = argparse.ArgumentParser(description='Interpolation for a pair of images')
60
  parser.add_argument('--video', dest='video', type=str, default=None)
 
140
  vid_out_name = args.output
141
  else:
142
  vid_out_name = '{}_{}X_{}fps.{}'.format(video_path_wo_ext, args.multi, int(np.round(args.fps)), args.ext)
143
+ # OUTPUT QUALITY SETTINGS
144
+ # -crf 17 is visually lossless (lower is better quality, higher is lower file size).
145
+ # -preset slow provides better compression efficiency.
146
+ outputdict = {
147
+ '-c:v': 'libx264',
148
+ '-crf': '17',
149
+ '-preset': 'slow',
150
+ '-pix_fmt': 'yuv420p'
151
+ }
152
+ vid_out = skvideo.io.FFmpegWriter(vid_out_name, inputdict={'-r': str(args.fps)}, outputdict=outputdict)
153
+
154
 
155
  def clear_write_buffer(user_args, write_buffer):
156
  cnt = 0
 
162
  cv2.imwrite('vid_out/{:0>7d}.png'.format(cnt), item[:, :, ::-1])
163
  cnt += 1
164
  else:
165
+ # skvideo expects RGB (item), whereas cv2 expected BGR (item[:, :, ::-1])
166
+ vid_out.writeFrame(item)
167
 
168
  def build_read_buffer(user_args, read_buffer, videogen):
169
  try:
 
292
  time.sleep(0.1)
293
  pbar.close()
294
  if not vid_out is None:
295
+ vid_out.close() # skvideo uses .close(), not .release()
296
 
297
  # move audio to new video file if appropriate
298
  if args.png == False and fpsNotAssigned == True and not args.video is None:
 
301
  except:
302
  print("Audio transfer failed. Interpolated video will have no audio")
303
  targetNoAudio = os.path.splitext(vid_out_name)[0] + "_noaudio" + os.path.splitext(vid_out_name)[1]
304
+ os.rename(targetNoAudio, vid_out_name)