Ask Your Question
0

How to write first N images of video frames

asked 2020-05-19 19:55:01 -0600

hernancrespo gravatar image

updated 2020-05-19 19:56:13 -0600

Hi, I want to write a video from a frame file. Frames folder is this: image description

This is my result:

import cv2
import numpy as np
import glob
j=0
img_array = []
for filename in glob.glob('/content/drive/My Drive/M-30-HD/M-30-HD/*.jpg'): #I work on Google Colab
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width,height)

    print(j)
    j=j+1

    img_array.append(img)
    if(j==3000):
      break


out = cv2.VideoWriter('m30hd.mp4',cv2.VideoWriter_fourcc(*'MP4V'), 24, size)

for i in range(len(img_array)):
    out.write(img_array[i])
    print(i)
out.release()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-05-19 21:45:09 -0600

supra56 gravatar image

W/out namespace glob and dictionary. Try this:

import cv2
import numpy as np
import os

def frames_to_video(inputpath,outputpath,fps):
   image_array = []
   files = [f for f in os.listdir(inputpath) if isfile(join(inputpath, f))]
   files.sort(key = lambda x: int(x[5:-4]))
   for i in range(len(files)):
       img = cv2.imread(inputpath + files[i])
       size =  (img.shape[1],img.shape[0])
       img = cv2.resize(img,size)
       image_array.append(img)
   fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X')
   out = cv2.VideoWriter(outputpath,fourcc, fps, size)
   for i in range(len(image_array)):
       out.write(image_array[i])
   out.release()


inputpath = 'folder path'
outpath =  'video file path/video.mp4'
fps = 29
frames_to_video(inputpath,outpath,fps)
edit flag offensive delete link more

Comments

I see this error

Traceback (most recent call last):
  File "videoya_cevirme.py", line 27, in <module>
    frames_to_video(inputpath,outpath,fps)
  File "videoya_cevirme.py", line 7, in frames_to_video
    files = [f for f in os.listdir(inputpath) if isfile(join(inputpath, f))]
  File "videoya_cevirme.py", line 7, in <listcomp>
    files = [f for f in os.listdir(inputpath) if isfile(join(inputpath, f))]
NameError: name 'isfile' is not defined

I pass input path as

inputpath = '/content/drive/My Drive/M-30-HD/M-30-HD'

And this is the path

hernancrespo gravatar imagehernancrespo ( 2020-05-19 22:08:37 -0600 )edit

Add this import os.path and os.path.isfile(join(inputpath, f))]

supra56 gravatar imagesupra56 ( 2020-05-19 22:34:05 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-05-19 19:55:01 -0600

Seen: 339 times

Last updated: May 19 '20