Save video frame by frame with Python OpenCV

import cv2
import os
 
def extractFrames(pathIn, pathOut):
    if not os.path.exists(pathOut):
        os.mkdir(pathOut)
 
    cap = cv2.VideoCapture(pathIn)
    count = 0
 
    while (cap.isOpened()):
 
        ret, frame = cap.read()
 
        if ret == True:
            cv2.imwrite(os.path.join(pathOut, "frame_{:06d}.jpg ".format(count)), frame)
            count += 1
        else:
            break
 
    cap.release()
    cv2.destroyAllWindows()
 
def main():
    extractFrames('video.mp4', 'outputdir')
 
if __name__=="__main__":
    main()

Recommended Posts

Save video frame by frame with Python OpenCV
Cut out frames from video by 1 second with Python + OpenCV
Binarization with OpenCV / Python
Video frame interpolation by deep learning Part1 [Python]
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Camera capture with Python + OpenCV
YouTube video management with Python 3
[Python] Using OpenCV with Python (Basic)
Loop video loading with opencv
Face detection with Python + OpenCV
JPEG image generation by specifying quality with Python + OpenCV
[Python] Webcam frame size and FPS settings with OpenCV
Display USB camera video with Python OpenCV with Raspberry Pi
Using OpenCV with Python @Mac
[Python] Save the video data imported by OpenCV as a serial number jpg file
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
Add fake tilt shift effect to video with OpenCV + Python
Create a simple video analysis tool with python wxpython + openCV
Convert video to black and white with ffmpeg + python + opencv
Shining life with Python and OpenCV
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Easy Python + OpenCV programming with Canopy
Try face recognition with python + OpenCV
Accelerate read by specifying OpenCV frame
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Load gif images with Python + OpenCV
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
Send image with python, save with php
Object recognition with openCV by traincascade
Draw arrows (vectors) with opencv / python
Basic study of OpenCV with Python
Object extraction in images by pattern matching using OpenCV with Python
Play video with sound with python !! (tkinter / imageio)
Get property information by scraping with python
Face detection with Python + OpenCV (rotation invariant)
[Python] Face detection by OpenCV (Haar Cascade)
Make a video player with PySimpleGUI + OpenCV
Capturing images with Pupil, python and OpenCV
I tried non-photorealistic rendering with Python + opencv
Save / load in-memory DB with python sqlite3
Image processing with Python & OpenCV [Tone Curve]
Function to save images by date [python3]
Image acquisition from camera with Python + OpenCV
[python, openCV] base64 Face recognition with images
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
[Python] Read images with OpenCV (for beginners)
Record with Python → Save file (sounddevice + wave)
Organize data divided by folder with Python
Video processing using Python + OpenCV on Mac
Until you can use opencv with python
Light image processing with Python x OpenCV
Smoothing edge-saved with python + OpenCV (BilateralFilter, NLMeansFilter)