Skip to content

Commit 2fe886f

Browse files
committed
init: upload data
0 parents  commit 2fe886f

File tree

7 files changed

+135
-0
lines changed

7 files changed

+135
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
frames/*
2+
src/__pycache__

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frame-extractor
2+
A simple video frame extractor using Python and OpenCV
3+
4+
## Usage
5+
Install the requirements first
6+
```bash
7+
pip install -r requirements.txt
8+
```
9+
<br>
10+
11+
If you want to extract all frames, you can use this command :
12+
```bash
13+
python main.py --path "/path/to/file_video"
14+
```
15+
<br>
16+
17+
If you want to extract single frame, you can use this command :
18+
```bash
19+
python main.py --path "/path/to/file_video" --frame n
20+
```
21+
n is the number of frame. <br>
22+
23+
If you want to extract frame in range, you can use this command :
24+
```bash
25+
python main.py --path "/path/to/file_video" --min n --max m
26+
```
27+
n is the number start frame and m is the number of the end frame<br>
28+
29+
All files are extracted in `frames` folder

main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from src.Launcher import init_parser, check_dir
2+
3+
if __name__ == '__main__' :
4+
check_dir()
5+
init_parser()

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python
2+
tqdm

src/Extractor.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import cv2
2+
from tqdm import tqdm
3+
4+
class FrameExtractor :
5+
def __init__(self, video_path) :
6+
self.video_path = video_path
7+
self.min_frame = None
8+
self.max_frame = None
9+
self.frame_number = None
10+
11+
def extract_frames(self, min_frame=None, max_frame=None):
12+
self.min_frame = min_frame
13+
self.max_frame = max_frame
14+
video = cv2.VideoCapture(self.video_path)
15+
16+
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
17+
18+
if self.min_frame is None and self.max_frame is None:
19+
self.min_frame = 0
20+
self.max_frame = total_frames - 1
21+
22+
if self.min_frame < 0 or self.max_frame > total_frames or self.min_frame >= self.max_frame:
23+
print(f"Invalid frame range. Video has {total_frames} frames.")
24+
return
25+
26+
pbar = tqdm(total = self.max_frame - self.min_frame + 1, desc="Extracting frames ...", unit="frame")
27+
28+
frame_count = 0
29+
extracted_frames = 0
30+
while True:
31+
success, frame = video.read()
32+
33+
if not success:
34+
break
35+
36+
if self.min_frame <= frame_count <= self.max_frame:
37+
cv2.imwrite(f"frames/frame_{frame_count}.jpg", frame)
38+
extracted_frames += 1
39+
pbar.update(1)
40+
41+
frame_count += 1
42+
43+
if frame_count > self.max_frame:
44+
break
45+
46+
pbar.close()
47+
video.release()
48+
print(f"Extracted {extracted_frames} frames from frame {self.min_frame} to {self.max_frame}.")
49+
50+
def extract_single_frame(self, frame_number):
51+
self.frame_number = frame_number
52+
video = cv2.VideoCapture(self.video_path)
53+
54+
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
55+
56+
if self.frame_number < 0 or self.frame_number >= total_frames:
57+
print(f"Invalid frame number. Video has {total_frames} frames.")
58+
return
59+
60+
video.set(cv2.CAP_PROP_POS_FRAMES, self.frame_number)
61+
62+
success, frame = video.read()
63+
64+
if success:
65+
cv2.imwrite(f"frames/frame_{self.frame_number}.jpg", frame)
66+
print(f"Extracted and saved frame {self.frame_number} as frame_{self.frame_number}.jpg")
67+
else:
68+
print(f"Could not extract frame {self.frame_number}.")
69+
70+
video.release()

src/Launcher.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import argparse
2+
import os
3+
from src.Extractor import FrameExtractor
4+
5+
def check_dir():
6+
if not os.path.exists('frames'):
7+
os.makedirs('frames')
8+
return True
9+
10+
def init_parser() :
11+
parser = argparse.ArgumentParser(description="Extract frames from a video.")
12+
parser.add_argument('--path', type=str, required=True, help="Path to the video file.")
13+
parser.add_argument('--frame', type=int, help="Frame number to extract.")
14+
parser.add_argument('--min', type=int, help="Minimum frame number to extract.")
15+
parser.add_argument('--max', type=int, help="Maximum frame number to extract.")
16+
17+
args = parser.parse_args()
18+
19+
frame = FrameExtractor(args.path)
20+
21+
if args.frame is not None:
22+
frame.extract_single_frame(args.frame)
23+
elif args.min is not None and args.max is not None:
24+
frame.extract_frames(args.min, args.max)
25+
else:
26+
print("No frame or range specified. Extracting all frames from the video.")
27+
frame.extract_frames()

src/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)