134 lines
4.9 KiB
Python
Executable File
134 lines
4.9 KiB
Python
Executable File
from pathlib import Path
|
|
import json
|
|
import moviepy
|
|
from moviepy.editor import VideoFileClip, AudioFileClip
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# path_root = Path("/yrfs/lsen/DATA/Actor1")
|
|
|
|
path_raw = Path("/ssdata1/cf/data/raw/20220319")
|
|
path_root = Path("/yrfs/lsen/DATA/Actor2")
|
|
|
|
vpath_videos = sorted(path_raw.glob("*.MP4"), reverse=False)
|
|
print(f"Have {len(vpath_videos)} dir files")
|
|
|
|
# IDs = ["clip_full"]
|
|
durations = 0
|
|
# for each video
|
|
for i, ipath_item in enumerate(vpath_videos):
|
|
iname = ipath_item.stem
|
|
print(f"processing {i}/{len(vpath_videos)} -> {ipath_item} ... ")
|
|
|
|
# if i < 40: continue
|
|
# if iname not in IDs: continue
|
|
|
|
# data path
|
|
ipath_data = path_root.joinpath("processing", ipath_item.stem)
|
|
|
|
ipath_frame = ipath_data.joinpath("frames")
|
|
ipath_video = ipath_data.joinpath("video")
|
|
ipath_audio = ipath_data.joinpath("audio")
|
|
ipath_infor = ipath_data.joinpath("video/info.json")
|
|
|
|
ipath_frame.mkdir(parents=True, exist_ok=True)
|
|
ipath_video.mkdir(parents=True, exist_ok=True)
|
|
ipath_audio.mkdir(parents=True, exist_ok=True)
|
|
|
|
# # load video info & check
|
|
if ipath_infor.exists():
|
|
# continue
|
|
with open(str(ipath_infor), "r") as f:
|
|
info = json.load(f)
|
|
nframe_info = info['nframes']
|
|
|
|
# check frames
|
|
nframe_real = len(list(ipath_frame.glob("*.jpg")))
|
|
if nframe_real == nframe_info:
|
|
print(f"\n Skip: have already done!")
|
|
continue
|
|
|
|
# load video
|
|
video = VideoFileClip(str(ipath_item))
|
|
print(f"video duration = {video.duration}, fps = {video.fps}, (start, end)=({video.start},{video.end}), nframes={int(video.duration * video.fps)}")
|
|
|
|
if video.audio is None:
|
|
print("\nERROR: audio info is None")
|
|
continue
|
|
|
|
durations += video.duration
|
|
print(f"\n\t -> Total durations = ({durations / 60 } min) ({durations / 3600 } h))")
|
|
# continue
|
|
|
|
# Trim
|
|
# video = video.subclip(t_start=5.0, t_end=int(video.duration)-5.0) before 03.14
|
|
video = video.subclip(t_start=15.0, t_end=int(video.duration)-15.0)
|
|
|
|
# video = video.subclip(t_start=5.0, t_end=60.0)
|
|
print(f"after cut video duration = {video.duration}, fps = {video.fps}, (start, end)=({video.start},{video.end}), nframes={int(video.duration * video.fps)}")
|
|
nframes = int(video.duration * video.fps)
|
|
# nframes = len(list(video.iter_frames()))
|
|
# print(f"after cut video duration = {video.duration}, fps = {video.fps}, (start, end)=({video.start},{video.end}), nframes={int(video.duration * video.fps)}")
|
|
|
|
# if fps != 50
|
|
if video.fps != 50:
|
|
print("FPS NOT = 50!! ")
|
|
|
|
# save sub video fps=50
|
|
ipath_video_sub = ipath_video.joinpath("video_trim.mp4")
|
|
if ipath_video_sub.exists():
|
|
print("\nERROR: ipath_video_sub.exists()")
|
|
continue
|
|
else:
|
|
video.write_videofile(str(ipath_video_sub), fps=50, audio=True)
|
|
print(f"save video > {ipath_video_sub}")
|
|
|
|
# reload video
|
|
video.close()
|
|
video = VideoFileClip(str(ipath_video_sub))
|
|
print(f"video reload duration = {video.duration}, fps = {video.fps}, (start, end)=({video.start},{video.end}), nframes={int(video.duration * video.fps)}")
|
|
nframes = len(list(video.iter_frames()))
|
|
|
|
|
|
|
|
# get audio
|
|
audio_sub = video.audio
|
|
if audio_sub is None:
|
|
print("\nERROR: audio info is None")
|
|
|
|
print(f"audio_sub duration = {audio_sub.duration}, fps = {audio_sub.fps}, (start, end)=({audio_sub.start},{audio_sub.end}), nframes={int(audio_sub.duration * audio_sub.fps)}")
|
|
|
|
# save audio_sub
|
|
ipath_audio_sub = ipath_audio.joinpath("audio_trim.wav")
|
|
audio_sub.write_audiofile(str(ipath_audio_sub), fps=audio_sub.fps)
|
|
print(f"save audio > {ipath_audio_sub}")
|
|
|
|
# # check fps
|
|
# if int(video.fps) != 50:
|
|
# print(f"\n Skip: fps != 50 realy fps={video.fps}, convert to 50fps")
|
|
|
|
# ipath_video_save = ipath_data.joinpath("video", f"video_50fps.mp4")
|
|
# video.write_videofile(str(ipath_video_save), fps=50.0)
|
|
# print(f"save video to > {ipath_video_save}...")
|
|
|
|
# save video feature
|
|
info = {"nframes": nframes,
|
|
"fps": video.fps,
|
|
"width": video.w,
|
|
"height": video.h}
|
|
|
|
with open(str(ipath_infor), 'w+') as f:
|
|
json.dump(info, f)
|
|
print(info)
|
|
print(f"save video info in -> {ipath_infor}\n")
|
|
|
|
# save trim video
|
|
print(f"write_images_sequence...")
|
|
ipath_format = str(ipath_frame) + "/frame-%06d.jpg"
|
|
video.write_images_sequence(ipath_format, fps=50)
|
|
|
|
|
|
|
|
|