150 lines
5.7 KiB
Python
150 lines
5.7 KiB
Python
import os
|
|
from pathlib import Path
|
|
import numpy as np
|
|
import tempfile
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# head, ssim_ref_3740
|
|
fname = "head_black"
|
|
# idx_reference = 3739
|
|
|
|
path_root = Path("/mfs/lsen/DATA/Actor4/bg_black")
|
|
path_frame = path_root.joinpath(fname, "frames")
|
|
path_video = path_root.joinpath(fname, "video")
|
|
path_video.mkdir(parents=True, exist_ok=True)
|
|
|
|
# path_peaks = path_video.joinpath("reference_peaks.txt") # SSIM-based
|
|
path_peaks = path_video.joinpath("reference_peaks_landmarks.txt") # Landmark based
|
|
vpeaks = np.loadtxt(path_peaks, dtype=int)
|
|
print(vpeaks)
|
|
|
|
idx_reference = vpeaks[0]
|
|
idx_peaks = vpeaks[1:]
|
|
print("reference = ", idx_reference)
|
|
print("idx_peaks = ", idx_peaks)
|
|
|
|
|
|
path_frame_ref = path_frame.joinpath(f"frame-{idx_reference:06d}.jpg")
|
|
|
|
path_video_ref = path_video.joinpath(f"video_split_ref_{idx_reference}")
|
|
path_video_ref.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
for i in range(len(idx_peaks)-1):
|
|
|
|
idx_beg = idx_peaks[i]
|
|
idx_end = idx_peaks[i+1]
|
|
print(f"processing the peak {idx_beg}, range=[{idx_beg},{idx_end}]")
|
|
|
|
|
|
nsize = idx_end - idx_beg + 1
|
|
duration = nsize/50
|
|
duration_pad = nsize//50 + 1
|
|
|
|
# less padding add
|
|
nframe_pad = duration_pad * 50 - nsize
|
|
|
|
if idx_beg == idx_reference:
|
|
nframe_pad_head = 0
|
|
nframe_pad_tail = nframe_pad
|
|
if nframe_pad < 16: # add 0.5s
|
|
nframe_pad_head = (nframe_pad + 25) - 31 if ((nframe_pad + 25) - 31 > 0) else 0
|
|
nframe_pad_tail = 31 if ((nframe_pad + 25) - 31 > 0) else (nframe_pad + 25)
|
|
|
|
elif idx_end == idx_reference:
|
|
nframe_pad_head = nframe_pad
|
|
nframe_pad_tail = 0
|
|
if nframe_pad < 16: # add 0.5s
|
|
nframe_pad_head = 31 if ((nframe_pad + 25) - 31 > 0) else (nframe_pad + 25)
|
|
nframe_pad_tail = (nframe_pad + 25) - 31 if ((nframe_pad + 25) - 31 > 0) else 0
|
|
|
|
else:
|
|
if nframe_pad < 32: # 0.5s + 25
|
|
nframe_pad_head = (nframe_pad + 25)// 2
|
|
nframe_pad_tail = (nframe_pad + 25) - nframe_pad_head
|
|
else:
|
|
nframe_pad_head = nframe_pad // 2
|
|
nframe_pad_tail = nframe_pad - nframe_pad_head
|
|
|
|
|
|
print(f"nframes = {nsize}, duration={duration}, duration_pad = {duration_pad}, need padding {nframe_pad / 2}")
|
|
print(f"nframe_pad_head= {nframe_pad_head}, nframe_pad_tail={nframe_pad_tail}")
|
|
|
|
# select head images
|
|
input_files = []
|
|
if nframe_pad_head > 0 and idx_beg != idx_reference:
|
|
ipath_beg = path_video_ref.joinpath(f"{idx_beg:06}", "interpolated_frames")
|
|
|
|
choice = []
|
|
for k in range(1, nframe_pad_head):
|
|
idx_choice = int(k * (31/(nframe_pad_head)) + 0.5)
|
|
choice.append(idx_choice)
|
|
choice.append(32)
|
|
|
|
# writer to file (need reversed)
|
|
for k in reversed(choice):
|
|
kpath_choice = ipath_beg.joinpath(f"frame_{k:03d}.jpg")
|
|
input_files.append(str(kpath_choice))
|
|
print(f"len={len(choice)} = {choice}")
|
|
|
|
# if padding reference frame
|
|
elif nframe_pad_head > 0 and idx_beg == idx_reference:
|
|
input_files += [str(path_frame.joinpath(f"frame-{idx_reference:06d}.jpg")) for _ in range(nframe_pad_head)]
|
|
|
|
|
|
# head-tail
|
|
input_files += [ str(path_frame.joinpath(f"frame-{k:06d}.jpg")) for k in range(idx_beg, idx_end+1)]
|
|
|
|
# tail-renference
|
|
if nframe_pad_tail > 0 and idx_end != idx_reference:
|
|
ipath_tail = path_video_ref.joinpath(f"{idx_end:06}", "interpolated_frames")
|
|
|
|
choice = []
|
|
for k in range(1, nframe_pad_tail):
|
|
idx_choice = int(k * (31/(nframe_pad_tail)) + 0.5)
|
|
choice.append(idx_choice)
|
|
choice.append(32)
|
|
|
|
# writer to file
|
|
for k in choice:
|
|
kpath_choice = ipath_tail.joinpath(f"frame_{k:03d}.jpg")
|
|
input_files.append(str(kpath_choice))
|
|
print(f"len={len(choice)} = {choice}")
|
|
|
|
# if padding reference frame
|
|
elif nframe_pad_tail > 0 and idx_end == idx_reference:
|
|
input_files += [str(path_frame.joinpath(f"frame-{idx_reference:06d}.jpg")) for _ in range(nframe_pad_tail)]
|
|
|
|
|
|
# write to files
|
|
# write to a input.txt
|
|
duration = len(input_files)/50
|
|
print(f" total have {len(input_files)} files, duration = {duration}")
|
|
print(" Top 40 have \n", '\n'.join(input_files[:40]))
|
|
print(" Last 40 have \n", '\n'.join(input_files[-40:]))
|
|
|
|
file_tmp = tempfile.NamedTemporaryFile(suffix='.txt')
|
|
with open(file_tmp.name, 'w') as fwriter:
|
|
for kpath in input_files:
|
|
fwriter.write(f"file {kpath} \n")
|
|
|
|
# render to video
|
|
# concate to a video
|
|
ipath_save = path_video_ref.joinpath(f"split_{i:02d}_interplote_{int(duration):02d}.mp4")
|
|
cmd = f"ffmpeg -y -r 50 -f concat -safe 0 -i {str(file_tmp.name)} -c:v libx264 -pix_fmt yuv420p -loglevel error {str(ipath_save)}"
|
|
print(cmd)
|
|
os.system(cmd)
|
|
|
|
# rotation
|
|
# ipath_save_rotation = path_root.joinpath(fname, "video", f"split_{i:02d}_interplote_{int(duration):02d}_rotation.mp4")
|
|
# cmd = f"ffmpeg -y -i {str(ipath_save)} -vf 'transpose=1' -loglevel error {str(ipath_save_rotation)}"
|
|
# print(cmd)
|
|
# os.system(cmd)
|
|
file_tmp.close()
|
|
# break
|
|
# make 32 padding
|
|
# resampling
|
|
# concate_video(path_frame, idx_peaks[i], idx_peaks[i+1], ipath_save)
|