Open-Sora: Democratizing Efficient Video Production for All
Python
[feat] update cut for csv without timestamp
zhengzangw834d7ad630eb
diff --git a/tools/datasets/datautil.py b/tools/datasets/datautil.pyindex d12df80..5e76725 100644--- a/tools/datasets/datautil.py+++ b/tools/datasets/datautil.py@@ -640,6 +640,10 @@ def main(args):data = data[data["flow"] >= args.flowmin]if args.remove_text_duplication:data = data.drop_duplicates(subset=["text"], keep="first")+ if args.img_only:+ data = data[data["path"].str.lower().str.endswith(IMG_EXTENSIONS)]+ if args.vid_only:+ data = data[~data["path"].str.lower().str.endswith(IMG_EXTENSIONS)]# process dataif args.shuffle:@@ -730,6 +734,8 @@ def parse_args():parser.add_argument("--matchmin", type=float, default=None, help="filter the dataset by minimum match score")parser.add_argument("--flowmin", type=float, default=None, help="filter the dataset by minimum flow score")parser.add_argument("--fpsmax", type=float, default=None, help="filter the dataset by maximum fps")+ parser.add_argument("--img-only", action="store_true", help="only keep the image data")+ parser.add_argument("--vid-only", action="store_true", help="only keep the video data")# data processingparser.add_argument("--shuffle", default=False, action="store_true", help="shuffle the dataset")@@ -816,6 +822,10 @@ def get_output_path(args, input_name):name += f"_matchmin{args.matchmin}"if args.flowmin is not None:name += f"_flowmin{args.flowmin}"+ if args.img_only:+ name += "_img"+ if args.vid_only:+ name += "_vid"# processingif args.shuffle:diff --git a/tools/scene_cut/cut.py b/tools/scene_cut/cut.pyindex ed958c9..9b5212b 100644--- a/tools/scene_cut/cut.py+++ b/tools/scene_cut/cut.py@@ -1,3 +1,5 @@+import cv2 # isort:skip+import argparseimport osimport subprocess@@ -29,19 +31,22 @@ def process_single_row(row, args):# if not is_intact_video(video_path, logger=logger):# return False- timestamp = row["timestamp"]- if not (timestamp.startswith("[") and timestamp.endswith("]")):- return False- scene_list = eval(timestamp)- scene_list = [(FrameTimecode(s, fps=1), FrameTimecode(t, fps=1)) for s, t in scene_list]+ if "timestamp" in row:+ timestamp = row["timestamp"]+ if not (timestamp.startswith("[") and timestamp.endswith("]")):+ return False+ scene_list = eval(timestamp)+ scene_list = [(FrameTimecode(s, fps=1), FrameTimecode(t, fps=1)) for s, t in scene_list]+ else:+ scene_list = [None]- if 'relpath' in row:- save_dir = os.path.dirname(os.path.join(args.save_dir, row['relpath']))+ if "relpath" in row:+ save_dir = os.path.dirname(os.path.join(args.save_dir, row["relpath"]))os.makedirs(save_dir, exist_ok=True)shorter_size = args.shorter_size- if (shorter_size is not None) and ('height' in row) and ('width' in row):- min_size = min(row['height'], row['width'])+ if (shorter_size is not None) and ("height" in row) and ("width" in row):+ min_size = min(row["height"], row["width"])if min_size <= shorter_size:shorter_size = None@@ -84,17 +89,18 @@ def split_video(save_path_list = []for idx, scene in enumerate(scene_list):- s, t = scene # FrameTimecode- if min_seconds is not None:- if (t - s).get_seconds() < min_seconds:- continue-- duration = t - s- if max_seconds is not None:- fps = s.framerate- max_duration = FrameTimecode(timecode="00:00:00", fps=fps)- max_duration.frame_num = round(fps * max_seconds)- duration = min(max_duration, duration)+ if scene is not None:+ s, t = scene # FrameTimecode+ if min_seconds is not None:+ if (t - s).get_seconds() < min_seconds:+ continue++ duration = t - s+ if max_seconds is not None:+ fps = s.framerate+ max_duration = FrameTimecode(timecode="00:00:00", fps=fps)+ max_duration.frame_num = round(fps * max_seconds)+ duration = min(max_duration, duration)# save pathfname = os.path.basename(video_path)@@ -112,7 +118,9 @@ def split_video(# clip to cut# -ss after -i is very slow; put -ss before -i- cmd += ["-nostdin", "-y", "-ss", str(s.get_seconds()), "-i", video_path, "-t", str(duration.get_seconds())]+ cmd += ["-nostdin", "-y", "-i", video_path]+ if scene is not None:+ cmd += ["-ss", str(s.get_seconds()), "-t", str(duration.get_seconds())]# target fpsif target_fps is not None:@@ -123,7 +131,7 @@ def split_video(cmd += ["-vf", f"scale='if(gt(iw,ih),-2,{shorter_size})':'if(gt(iw,ih),{shorter_size},-2)'"]# cmd += ['-vf', f"scale='if(gt(iw,ih),{shorter_size},trunc(ow/a/2)*2)':-2"]- cmd += ["-map", "0", save_path]+ cmd += ["-map", "0:v", save_path]proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)stdout, stderr = proc.communicate()@@ -141,13 +149,18 @@ def parse_args():parser = argparse.ArgumentParser()parser.add_argument("meta_path", type=str)parser.add_argument("--save_dir", type=str)- parser.add_argument("--min_seconds", type=float, default=None,- help='if not None, clip shorter than min_seconds is ignored')- parser.add_argument("--max_seconds", type=float, default=None,- help='if not None, clip longer than max_seconds is truncated')- parser.add_argument("--target_fps", type=int, default=None, help='target fps of clips')- parser.add_argument("--shorter_size", type=int, default=1080, help='resize the shorter size by keeping ratio; will not do upscale')- parser.add_argument("--num_workers", type=int, default=None, help='#workers for pandarallel')+ parser.add_argument(+ "--min_seconds", type=float, default=None, help="if not None, clip shorter than min_seconds is ignored"+ )+ parser.add_argument(+ "--max_seconds", type=float, default=None, help="if not None, clip longer than max_seconds is truncated"+ )+ parser.add_argument("--target_fps", type=int, default=None, help="target fps of clips")+ parser.add_argument(+ "--shorter_size", type=int, default=1080, help="resize the shorter size by keeping ratio; will not do upscale"+ )+ parser.add_argument("--num_workers", type=int, default=None, help="#workers for pandarallel")+ parser.add_argument("--disable_parallel", action="store_true", help="disable parallel processing")args = parser.parse_args()return args@@ -161,15 +174,19 @@ def main():logger = None# initialize pandarallel- if args.num_workers is not None:- pandarallel.initialize(progress_bar=True, nb_workers=args.num_workers)- else:- pandarallel.initialize(progress_bar=True)+ if not args.disable_parallel:+ if args.num_workers is not None:+ pandarallel.initialize(progress_bar=True, nb_workers=args.num_workers)+ else:+ pandarallel.initialize(progress_bar=True)process_single_row_partial = partial(process_single_row, args=args)# processmeta = pd.read_csv(args.meta_path)- meta.parallel_apply(process_single_row_partial, axis=1)+ if not args.disable_parallel:+ meta.parallel_apply(process_single_row_partial, axis=1)+ else:+ meta.apply(process_single_row_partial, axis=1)if __name__ == "__main__":