Upload folder using huggingface_hub
Browse files- youtube_samples/cut_clips/K87g4RvO-9k_254.0_259.0.mp4 +3 -0
- youtube_samples/cut_clips/ayNzH0uygFw_9.0_21.0.mp4 +3 -0
- youtube_samples/cut_clips/biDn0Gi6V8U_7.0_15.0.mp4 +3 -0
- youtube_samples/cut_clips/goWgiQQMugA_2.5_9.0.mp4 +3 -0
- youtube_samples/metadata/annotations-v1.csv +5 -0
- youtube_samples/scripts/cut_clips.py +129 -0
- youtube_samples/scripts/download_sample.py +44 -0
- youtube_samples/scripts/youtube-dlp.conf +25 -0
- youtube_samples/videos/K87g4RvO-9k.mp4 +3 -0
- youtube_samples/videos/ayNzH0uygFw.mp4 +3 -0
- youtube_samples/videos/biDn0Gi6V8U.mp4 +3 -0
- youtube_samples/videos/goWgiQQMugA.mp4 +3 -0
youtube_samples/cut_clips/K87g4RvO-9k_254.0_259.0.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dd4f459b996bca29d594784ace8ac46fc63d06795dfe20b2af3484ecfb33a37f
|
3 |
+
size 250016
|
youtube_samples/cut_clips/ayNzH0uygFw_9.0_21.0.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7fac12cbdb413e7402af7b809317b30cc0da71ce6e1495e308bef605c10777b7
|
3 |
+
size 636872
|
youtube_samples/cut_clips/biDn0Gi6V8U_7.0_15.0.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:50b050a0f4f2322d2afc38d3ebb870956ce4c0fc9ef53866fb139caed27b3357
|
3 |
+
size 431493
|
youtube_samples/cut_clips/goWgiQQMugA_2.5_9.0.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:82a9330eec584036a8bcda09510adb4276cec08716e5c5eff0bd76e0b383d4e6
|
3 |
+
size 423949
|
youtube_samples/metadata/annotations-v1.csv
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
video_id,start_time,end_time
|
2 |
+
K87g4RvO-9k,254.0,259.0
|
3 |
+
ayNzH0uygFw,9.0,21.0
|
4 |
+
biDn0Gi6V8U,7.0,15.0
|
5 |
+
goWgiQQMugA,2.5,9.0
|
youtube_samples/scripts/cut_clips.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Cut video clips from downloaded videos."""
|
2 |
+
import os
|
3 |
+
from os.path import join, exists
|
4 |
+
from subprocess import call
|
5 |
+
import time
|
6 |
+
|
7 |
+
import numpy as np
|
8 |
+
import pandas as pd
|
9 |
+
from tqdm import tqdm
|
10 |
+
|
11 |
+
# import shared.utils.io as io
|
12 |
+
# import shared.utils.log as log
|
13 |
+
|
14 |
+
|
15 |
+
def time_float_to_str(time_in_seconds):
|
16 |
+
import datetime
|
17 |
+
|
18 |
+
# Calculate hours, minutes, seconds, and milliseconds
|
19 |
+
hours, remainder = divmod(time_in_seconds, 3600)
|
20 |
+
minutes, seconds_with_ms = divmod(remainder, 60)
|
21 |
+
seconds, milliseconds = divmod(int(seconds_with_ms * 1000), 1000)
|
22 |
+
|
23 |
+
# Create a timedelta object
|
24 |
+
time_delta = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds, milliseconds=milliseconds)
|
25 |
+
|
26 |
+
# Format the time as HH:MM:SS.mmm
|
27 |
+
formatted_time = str(time_delta)
|
28 |
+
|
29 |
+
return formatted_time
|
30 |
+
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
|
34 |
+
import argparse
|
35 |
+
parser = argparse.ArgumentParser()
|
36 |
+
parser.add_argument(
|
37 |
+
"--csv", type=str, required=True,
|
38 |
+
help="Path to CSV file containing video IDs and timestamps",
|
39 |
+
)
|
40 |
+
parser.add_argument(
|
41 |
+
"--video_id_key", type=str, default="video_id",
|
42 |
+
)
|
43 |
+
parser.add_argument(
|
44 |
+
"--start_time_key", type=str, default="start_time",
|
45 |
+
)
|
46 |
+
parser.add_argument(
|
47 |
+
"--end_time_key", type=str, default="end_time",
|
48 |
+
)
|
49 |
+
parser.add_argument(
|
50 |
+
"--video_dir", type=str, required=True,
|
51 |
+
help="Path to directory containing downloaded videos",
|
52 |
+
)
|
53 |
+
parser.add_argument(
|
54 |
+
"--cut_dir", type=str, required=True,
|
55 |
+
help="Path to directory where cut videos will be saved",
|
56 |
+
)
|
57 |
+
parser.add_argument(
|
58 |
+
"--overwrite", action="store_true",
|
59 |
+
help="Whether to overwrite existing cut videos",
|
60 |
+
)
|
61 |
+
parser.add_argument(
|
62 |
+
"--verbose", action="store_true",
|
63 |
+
)
|
64 |
+
parser.add_argument(
|
65 |
+
"--no_round_times", action="store_true",
|
66 |
+
help="Whether to round start and end times to nearest second in filenames",
|
67 |
+
)
|
68 |
+
args = parser.parse_args()
|
69 |
+
|
70 |
+
# Make cut_dir
|
71 |
+
os.makedirs(args.cut_dir, exist_ok=True)
|
72 |
+
|
73 |
+
# Load csv
|
74 |
+
assert os.path.exists(args.csv), f"CSV file {args.csv} does not exist."
|
75 |
+
df = pd.read_csv(args.csv)
|
76 |
+
print(">>> Loaded CSV file with shape", df.shape)
|
77 |
+
assert {args.video_id_key, args.start_time_key, args.end_time_key}.issubset(df.columns), \
|
78 |
+
f"CSV file must contain columns {args.video_id_key}, {args.start_time_key}, and {args.end_time_key}."
|
79 |
+
|
80 |
+
# Filter out videos that don't exist
|
81 |
+
df["video_path"] = df[args.video_id_key].apply(
|
82 |
+
lambda video_id: join(args.video_dir, f"{video_id}.mp4"),
|
83 |
+
)
|
84 |
+
df["check_video"] = df["video_path"].apply(exists)
|
85 |
+
df = df[df["check_video"]]
|
86 |
+
del df["check_video"]
|
87 |
+
print(">>> Found videos for", df.shape[0], "rows.")
|
88 |
+
|
89 |
+
|
90 |
+
# Cut videos
|
91 |
+
ext = "mp4"
|
92 |
+
iterator = tqdm(range(len(df)), desc="Cutting clips")
|
93 |
+
for i in iterator:
|
94 |
+
|
95 |
+
row = df.iloc[i].to_dict()
|
96 |
+
f = row["video_path"]
|
97 |
+
v, s, e = row[args.video_id_key], row[args.start_time_key], row[args.end_time_key]
|
98 |
+
s = float(s)
|
99 |
+
e = float(e)
|
100 |
+
|
101 |
+
if args.no_round_times:
|
102 |
+
clip_filename = f"{v}_{s}_{e}.{ext}"
|
103 |
+
else:
|
104 |
+
clip_filename = f"{v}_{np.round(s, 1)}_{np.round(e, 1)}.{ext}"
|
105 |
+
clip_filepath = join(args.cut_dir, clip_filename)
|
106 |
+
os.makedirs(os.path.dirname(clip_filepath), exist_ok=True)
|
107 |
+
|
108 |
+
if os.path.exists(clip_filepath) and not args.overwrite:
|
109 |
+
continue
|
110 |
+
|
111 |
+
# bring s in HH:MM:SS.mmm format with milliseconds
|
112 |
+
s = time_float_to_str(s)
|
113 |
+
e = time_float_to_str(e)
|
114 |
+
# # bring s in HH:MM:SS. format
|
115 |
+
# s = time.strftime("%H:%M:%S", time.gmtime(s))
|
116 |
+
# e = time.strftime("%H:%M:%S", time.gmtime(e))
|
117 |
+
|
118 |
+
# ffmpeg code
|
119 |
+
# use ffmpeg to cut the clip + change spatial resolution to have max height
|
120 |
+
command = f"ffmpeg -i {f} -ss {s} -to {e} -strict -2 -c:v libx264 "\
|
121 |
+
f"-pix_fmt yuv420p -c:a copy {clip_filepath} "\
|
122 |
+
f"-y -format {ext}"
|
123 |
+
if not args.verbose:
|
124 |
+
command += " -loglevel quiet"
|
125 |
+
else:
|
126 |
+
print(">>> Cutting clip", clip_filepath)
|
127 |
+
call(command, shell=True)
|
128 |
+
|
129 |
+
print(">>> Number of cut files:", len(os.listdir(args.cut_dir)))
|
youtube_samples/scripts/download_sample.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Downloads single YouTube video."""
|
2 |
+
import pytube
|
3 |
+
import os
|
4 |
+
|
5 |
+
|
6 |
+
def download_video_ytdlp(video_id, save_dir, conf_file, log_dir):
|
7 |
+
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
8 |
+
log_file = f"{log_dir}/{video_id}.log"
|
9 |
+
|
10 |
+
save_path = f"{save_dir}/{video_id}.mkv"
|
11 |
+
if os.path.exists(save_path):
|
12 |
+
return {"video_id": video_id, "status": "ALREADY_DOWNLOADED", "download_time": 0}
|
13 |
+
|
14 |
+
# Also check mp4
|
15 |
+
mp4_path = f"{save_dir}/{video_id}.mp4"
|
16 |
+
if os.path.exists(mp4_path):
|
17 |
+
return {"video_id": video_id, "status": "ALREADY_DOWNLOADED", "download_time": 0}
|
18 |
+
|
19 |
+
start = time.time()
|
20 |
+
try:
|
21 |
+
# command = f"youtube-dl -f bestvideo+bestaudio --merge-output-format mp4 -o {save_dir}/%(id)s.%(ext)s {video_url}"
|
22 |
+
command = f"yt-dlp --config-location {conf_file} -o {save_dir}/{video_id} {video_url} > {log_file} 2>&1"
|
23 |
+
# command = f"yt-dlp --config-location {conf_file} -o {save_dir}/{video_id} {video_url}"
|
24 |
+
call(command, shell=True)
|
25 |
+
status = "SUCCESS"
|
26 |
+
except:
|
27 |
+
status = "FAIL"
|
28 |
+
return {"video_id": video_id, "status": status, "download_time": 0}
|
29 |
+
end = time.time()
|
30 |
+
download_time = end - start
|
31 |
+
|
32 |
+
return {"video_id": video_id, "status": status, "download_time": download_time}
|
33 |
+
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
import os
|
37 |
+
import time
|
38 |
+
conf_file = "./youtube-dlp.conf"
|
39 |
+
log_dir = "../download_metadata/"
|
40 |
+
os.makedirs(log_dir, exist_ok=True)
|
41 |
+
save_dir = "../videos/"
|
42 |
+
os.makedirs(save_dir, exist_ok=True)
|
43 |
+
video_id = "K87g4RvO-9k"
|
44 |
+
download_video_ytdlp(video_id, save_dir, conf_file, log_dir)
|
youtube_samples/scripts/youtube-dlp.conf
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# RESOLUTION
|
2 |
+
-f 'bestvideo[height<=360][ext=mp4]+bestaudio/best[height<=360][ext=m4a]/mp4'
|
3 |
+
|
4 |
+
--flat-playlist
|
5 |
+
|
6 |
+
# FORMAT
|
7 |
+
--prefer-ffmpeg
|
8 |
+
--merge-output-format mkv
|
9 |
+
--video-multistreams
|
10 |
+
--audio-multistreams
|
11 |
+
|
12 |
+
# SUBTITLES
|
13 |
+
# --write-auto-sub
|
14 |
+
# --convert-subs srt
|
15 |
+
|
16 |
+
# METADATA
|
17 |
+
#--add-metadata
|
18 |
+
#--write-description
|
19 |
+
|
20 |
+
# COOKIES
|
21 |
+
--cookies ./cookies/cookies-youtube-personal.txt
|
22 |
+
--referer "URL"
|
23 |
+
|
24 |
+
# Ignore missing videos
|
25 |
+
--ignore-errors
|
youtube_samples/videos/K87g4RvO-9k.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6c4e2f39ba9822a96eb2e33dd665abcf4027055f69755ff3432cb92938949984
|
3 |
+
size 22672619
|
youtube_samples/videos/ayNzH0uygFw.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b1f710802f9bb33169ad95f90752b2c24ac7d0fdde709ed0b9fc78d1b77f1246
|
3 |
+
size 1807861
|
youtube_samples/videos/biDn0Gi6V8U.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1d3d98d5e6839d719dbcd46b286c8d51d6786402d61cc427b6bc82ede04b96b0
|
3 |
+
size 879547
|
youtube_samples/videos/goWgiQQMugA.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4be8ed9e9461471be9e03051b8e9d1fdfe599368cb0f72604a87c62878aa1317
|
3 |
+
size 555516
|