Python Forum
OpenCV - extract 1st frame out of a video file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenCV - extract 1st frame out of a video file
#1
Hi All,

I'm trying to extract the first frame of a sample video file with OpenCV however it doesnt work:

import cv2

f = cv2.VideoCapture('1.mkv')

rval, frame = f.read()
cv2.imwrite('first_frame.jpg', frame)
f.release()
Could you help me out, what do I do wrong, the jpeg file is empty after running the code

Thanks

Zoli
Reply
#2
import cv2
import numpy as np
vidcap = cv2.VideoCapture('http://Desktop/SampleVideo_1280x720_1mb.mp4')
success,image = vidcap.read()
count = 0
while success:
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file      
  success,image = vidcap.read()
  print('Read a new frame: ', success)
  count += 1
Reply
#3
Hi, I think it's gonna extract ALL the frames from the video file. I just need the first frame so I modified it this way:

def getFirstFrame(videofile):
    vidcap = cv2.VideoCapture(videofile)
    success, image = vidcap.read()
    if success:
        cv2.imwrite("first_frame.jpg", image)  # save frame as JPEG file
Thanks for the hint!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract and plot data from a txt file usercat123 2 1,208 Apr-20-2022, 06:50 PM
Last Post: usercat123
  save video frames into pandas data-frame tofi 0 2,648 Oct-18-2018, 07:02 PM
Last Post: tofi
  Upload csv file as numbers (floating?) and extract element, row, and column bentaz 7 4,412 Mar-19-2018, 05:34 PM
Last Post: bentaz
  Reading json file as pandas data frame? Alberto 1 8,307 Feb-05-2018, 12:43 AM
Last Post: snippsat
  Extract data between two dates from a .csv file using Python 2.7 sujai_banerji 1 10,307 Nov-15-2017, 09:48 PM
Last Post: snippsat
  Identifying items in a csv file that also appear in a Text extract Jaynorth 17 17,258 Sep-21-2016, 10:51 PM
Last Post: Jaynorth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020