This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_chunks.py
81 lines (65 loc) · 3 KB
/
video_chunks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
""" Given a video and time, Divide the video into chucks based on time and save it to a folder. Leave the end chuck if it is less than time (sec)
Author @Prakash
Resources: https://unix.stackexchange.com/questions/1670/how-can-i-use-ffmpeg-to-split-mpeg-video-into-10-minute-chunks
"""
import cv2
from tqdm import tqdm
import os
# vid_loc = "../../CPG_Video_analytics/All_Vacuuming/all_annotated/Boston_Elizabeth_G_LivingRoom_20150418162435/37_Boston_Elizabeth_G_LivingRoom_20150418162435.mp4"
# save_loc = "video_sample"
# save_format = ".avi"
# required_time = 10
def video_chuncks(vid_loc, save_loc, save_format=".avi", required_time=10, verbose=False):
if not os.path.exists(save_loc):
os.makedirs(save_loc)
cap = cv2.VideoCapture(vid_loc)
frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if verbose:
print("frames: {} height: {} width: {}".format(frames, width, height))
fps = cap.get(cv2.CAP_PROP_FPS)
total_time_in_sec = int(frames/fps)
if verbose:
print("frames_per_sec: ", fps)
print("total time in sec: ", total_time_in_sec)
#capturing all the frames
vid = []
while True:
ret, img = cap.read()
if not ret:
break
vid.append(img)
print("video_length", len(vid))
#sec
total_frames_to_capture_required_time = int(fps * required_time)
if verbose:
print("total_frames_to_capture_required_time:", total_frames_to_capture_required_time)
## Now dividing the video into 10 sec chunks ..
for i in range(int(frames/total_frames_to_capture_required_time)+1):
start_frame = i * total_frames_to_capture_required_time
end_frame = (i+1) * total_frames_to_capture_required_time
if end_frame-start_frame < total_frames_to_capture_required_time:
capture = vid[start_frame:]
else:
capture = vid[start_frame:end_frame]
## Capture the video
video_save_loc = save_loc+vid_loc.rsplit("/")[-1].rsplit(".")[0]+"_"+str(i)+save_format
out = cv2.VideoWriter(video_save_loc, cv2.VideoWriter_fourcc('M','J','P','G') , fps, (width, height))
for frame in capture:
out.write(frame)
out.release()
if verbose:
print("video saved at", video_save_loc)
def video_chunks_ffpmeg(vid_loc, save_loc, seconds=10):
if not os.path.exists(save_loc):
os.makedirs(save_loc)
video_save_loc = save_loc+vid_loc.rsplit("/")[-1].rsplit(".")[0]+"_"
k = "ffmpeg -i {} -c copy -map 0 -segment_time {} -f segment -reset_timestamps 1 {}%03d.mp4".format(vid_loc, seconds, video_save_loc)
os.system(k)
if __name__ == '__main__':
for num, i in enumerate(tqdm(open("txt_files/others.lst", "r"))):
classs = i.split()[1]
loc = i.split()[0]
#video_chuncks(loc, "VIDEO_CHUNKS/", save_format=".avi", required_time=10, verbose=False)
video_chunks_ffpmeg(loc, "VIDEO_OTHERS_CHUNKS/")