Ask Your Question
0

Saving video from frames in with fourcc codec h264 and h265 with opencv

asked 2020-10-27 09:23:59 -0500

updated 2020-10-27 17:24:41 -0500

0

I am saving frames from live stream to a video with h264 codec. I tried this with openCV (versions 3.4 and 4.4) in python but I am not able to save it. I can save video in XVID and many other codecs but I am not successful in h264 and h265.

I am using windows opencv 4.4 in Python.

My sample code is as follow

    cap = cv2.VideoCapture(0)
    while(cap.isOpened()):    
     ret,frame = cap.read()
     if ret == True:
     width  = int(cap.get(3)) # float
     height = int(cap.get(4)) # float
     # fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))        
     fourcc = cv2.VideoWriter_fourcc(*'H264')
     out = cv2.VideoWriter(filename, fourcc, 30, (width,height)) 
     out.write(frame)
    out.release()

Can anyone help me how can I save video in h264 and h265.

I am saving frames from live stream to a video with h264 codec. I tried this with openCV (versions 3.4 and 4.4) in python but I am not able to save it. I can save video in XVID and many other codecs but I am not successful in h264 and h265.

I am using windows opencv 4.4 in Python.

My sample code is as follow

cap = cv2.VideoCapture(0)
while(cap.isOpened()):    
    ret,frame = cap.read()
    if ret == True:
    width  = int(cap.get(3)) # float
    height = int(cap.get(4)) # float
    # fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))        
    fourcc = cv2.VideoWriter_fourcc(*'H264')
    out = cv2.VideoWriter(filename, fourcc, 30, (width,height)) 
    out.write(frame)
out.release()

Can anyone help me how can I save video in h264 and h265.

edit retag flag offensive close merge delete

Comments

What is bloody filename?

supra56 gravatar imagesupra56 ( 2020-10-26 21:47:51 -0500 )edit

Here is tutorial for Getting Started with Videos

supra56 gravatar imagesupra56 ( 2020-10-26 21:56:12 -0500 )edit

windows opencv 4.4

afaik, the opencv_ffmpeg.dll does not support h265 codec (gpl)

berak gravatar imageberak ( 2020-10-27 04:32:17 -0500 )edit

@berak. Need to install H265. sudo apt-get install ffmpeg x264 libx264-dev and sudo apt-get install ffmpeg x265 libx265-dev.

supra56 gravatar imagesupra56 ( 2020-10-27 08:10:38 -0500 )edit

supra, nope, it's on windows, and all of it is of no use there

berak gravatar imageberak ( 2020-10-27 08:13:34 -0500 )edit

H264(2K) for mp4. And H265(4K) for mp5

supra56 gravatar imagesupra56 ( 2020-10-27 08:40:55 -0500 )edit
sturkmen gravatar imagesturkmen ( 2020-10-27 09:29:19 -0500 )edit

@Imran Hassan, please do NOT duplicate questions here, thank you.

berak gravatar imageberak ( 2020-10-27 13:36:30 -0500 )edit

@berak Sorry for duplication of the question. Actually, it was a little misunderstanding. I also tried to install "sudo apt-get install ffmpeg x264 libx264-dev and sudo apt-get install ffmpeg x265 libx265-dev" in Linux but it is also not able to save it.

Can you explain or guide how I can I build openCV and ffmpeg having these two libraries and to install it in python so that I can save video in h264.

imran.hassan gravatar imageimran.hassan ( 2020-10-28 02:13:38 -0500 )edit
1

@supra56 I tried filemanes with different extension like saveVideo.mkv, saveVideo.avi, saveVideo.mp4

But still it is not working.

imran.hassan gravatar imageimran.hassan ( 2020-10-28 02:14:49 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-10-26 21:48:51 -0500

supra56 gravatar image

updated 2020-10-26 21:53:47 -0500

Try this:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'X264')
out = cv2.VideoWriter('output.mp4',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

I tried this but it is not working

imran.hassan gravatar imageimran.hassan ( 2020-10-28 02:09:54 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2020-10-27 09:23:59 -0500

Seen: 187 times

Last updated: Oct 27