|
"""Cut video clips from downloaded videos.""" |
|
import os |
|
from os.path import join, exists |
|
from subprocess import call |
|
import time |
|
|
|
import numpy as np |
|
import pandas as pd |
|
from tqdm import tqdm |
|
|
|
|
|
|
|
|
|
|
|
def time_float_to_str(time_in_seconds): |
|
import datetime |
|
|
|
|
|
hours, remainder = divmod(time_in_seconds, 3600) |
|
minutes, seconds_with_ms = divmod(remainder, 60) |
|
seconds, milliseconds = divmod(int(seconds_with_ms * 1000), 1000) |
|
|
|
|
|
time_delta = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds, milliseconds=milliseconds) |
|
|
|
|
|
formatted_time = str(time_delta) |
|
|
|
return formatted_time |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
import argparse |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument( |
|
"--csv", type=str, required=True, |
|
help="Path to CSV file containing video IDs and timestamps", |
|
) |
|
parser.add_argument( |
|
"--video_id_key", type=str, default="video_id", |
|
) |
|
parser.add_argument( |
|
"--start_time_key", type=str, default="start_time", |
|
) |
|
parser.add_argument( |
|
"--end_time_key", type=str, default="end_time", |
|
) |
|
parser.add_argument( |
|
"--video_dir", type=str, required=True, |
|
help="Path to directory containing downloaded videos", |
|
) |
|
parser.add_argument( |
|
"--cut_dir", type=str, required=True, |
|
help="Path to directory where cut videos will be saved", |
|
) |
|
parser.add_argument( |
|
"--overwrite", action="store_true", |
|
help="Whether to overwrite existing cut videos", |
|
) |
|
parser.add_argument( |
|
"--verbose", action="store_true", |
|
) |
|
parser.add_argument( |
|
"--no_round_times", action="store_true", |
|
help="Whether to round start and end times to nearest second in filenames", |
|
) |
|
args = parser.parse_args() |
|
|
|
|
|
os.makedirs(args.cut_dir, exist_ok=True) |
|
|
|
|
|
assert os.path.exists(args.csv), f"CSV file {args.csv} does not exist." |
|
df = pd.read_csv(args.csv) |
|
print(">>> Loaded CSV file with shape", df.shape) |
|
assert {args.video_id_key, args.start_time_key, args.end_time_key}.issubset(df.columns), \ |
|
f"CSV file must contain columns {args.video_id_key}, {args.start_time_key}, and {args.end_time_key}." |
|
|
|
|
|
df["video_path"] = df[args.video_id_key].apply( |
|
lambda video_id: join(args.video_dir, f"{video_id}.mp4"), |
|
) |
|
df["check_video"] = df["video_path"].apply(exists) |
|
df = df[df["check_video"]] |
|
del df["check_video"] |
|
print(">>> Found videos for", df.shape[0], "rows.") |
|
|
|
|
|
|
|
ext = "mp4" |
|
iterator = tqdm(range(len(df)), desc="Cutting clips") |
|
for i in iterator: |
|
|
|
row = df.iloc[i].to_dict() |
|
f = row["video_path"] |
|
v, s, e = row[args.video_id_key], row[args.start_time_key], row[args.end_time_key] |
|
s = float(s) |
|
e = float(e) |
|
|
|
if args.no_round_times: |
|
clip_filename = f"{v}_{s}_{e}.{ext}" |
|
else: |
|
clip_filename = f"{v}_{np.round(s, 1)}_{np.round(e, 1)}.{ext}" |
|
clip_filepath = join(args.cut_dir, clip_filename) |
|
os.makedirs(os.path.dirname(clip_filepath), exist_ok=True) |
|
|
|
if os.path.exists(clip_filepath) and not args.overwrite: |
|
continue |
|
|
|
|
|
s = time_float_to_str(s) |
|
e = time_float_to_str(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
command = f"ffmpeg -i {f} -ss {s} -to {e} -strict -2 -c:v libx264 "\ |
|
f"-pix_fmt yuv420p -c:a copy {clip_filepath} "\ |
|
f"-y -format {ext}" |
|
if not args.verbose: |
|
command += " -loglevel quiet" |
|
else: |
|
print(">>> Cutting clip", clip_filepath) |
|
call(command, shell=True) |
|
|
|
print(">>> Number of cut files:", len(os.listdir(args.cut_dir))) |