OpenCV convert image to video(mp4)

0

I'm trying to convert an image to a video. I success to convert an image to an avi video, but I try to convert in mp4, but I got this error :

[libx264 @ 0x15e2140] broken ffmpeg default settings detected
[libx264 @ 0x15e2140] use an encoding preset (e.g. -vpre medium)
[libx264 @ 0x15e2140] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 0x15e2140] speed presets are listed in x264 --help
[libx264 @ 0x15e2140] profile is optional; x264 defaults to high
Could not open codec 'libx264': Unspecified error

My code for mp4 (Which give the error):

# -*- coding: utf-8 -*-

import os
import cv2
import numpy as np

print(cv2.__version__)


FILENAME = "test.jpg"

if FILENAME.endswith('.mp4'):
     print('.mp4')


elif FILENAME.endswith('.jpg'):
print('.jpg')

if not os.path.exists(FILENAME):
    print('File not found')

img = cv2.imread(FILENAME, cv2.IMREAD_COLOR)

height, width, layers = img.shape

fourcc = cv2.cv.CV_FOURCC(*'X264')
video = cv2.VideoWriter('video.mp4', fourcc, 1, (width, height))

video.write(img)

#cv2.imshow('image', img)
#cv2.waitKey(0)
cv2.destroyAllWindows()
video.release()

My code for avi (Which work):

# -*- coding: utf-8 -*-

import os
import cv2
import numpy as np

print(cv2.__version__)


FILENAME = "test.jpg"

if FILENAME.endswith('.mp4'):
     print('.mp4')


elif FILENAME.endswith('.jpg'):
print('.jpg')

if not os.path.exists(FILENAME):
    print('File not found')

img = cv2.imread(FILENAME, cv2.IMREAD_COLOR)

height, width, layers = img.shape

fourcc = cv2.cv.CV_FOURCC(*'XVID')
video = cv2.VideoWriter('video.avi', fourcc, 1, (width, height))

video.write(img)

#cv2.imshow('image', img)
#cv2.waitKey(0)
cv2.destroyAllWindows()
video.release()

I try to replace X264 by H264, I got the same error. I search some answer, and try to adapt their code to mine, change nothing, so I come back to my working avi code.

I try to install this: $ sudo apt-get install ffmpeg x264 libx264-dev

doesn't work. I try to replace H264 by 0X00000021, doesn't work too.

Thanks for your answers

python
opencv
codec
asked on Stack Overflow Jul 9, 2018 by J.Doe • edited Jul 9, 2018 by J.Doe

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0