Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

creating mp4 file in linux

I can read and play the file but when I write a mp4 or even avi video its not playable . I have confirmed that ffmpeg can create the video fine so why from openCV its not working? iam using openCV 3.1.0 and python 3.3

ffmpeg -i /tmp/kyak.mp4 -vcodec libx264 -f mp4 /tmp/a.mp4

the code below produces the file but it contains zero frame as shown by ffmpeg command .

import numpy as np
import cv2

cap = cv2.VideoCapture('/tmp/kyak.mp4')

fourcc = cv2.VideoWriter_fourcc('X','2','6','4')

out = cv2.VideoWriter('/tmp/output.mp4',fourcc, 20.0, (640,480))

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

                gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

                #write the gray frame
                out.write(gray)

                cv2.imshow('frame',gray)

                if cv2.waitKey(30) & 0xFF == ord('q'):
                        break
        else:
                break
cap.release()
out.release()
cv2.destroyAllWindows()

The video plays but I get this error from the code above :

OpenCV: FFMPEG: tag 0x34363258/'X264' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000021/'!???'

if I replace the "fourcc" with "0x00000021" then the error goes away but still the produced file has zero frame .

[root@hadoop1 scripts]# ffmpeg -i /tmp/output.mp4
ffmpeg version N-82533-gae514b1 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-17)
  configuration: --enable-libx264 --enable-gpl --enable-shared
  libavutil      55. 40.100 / 55. 40.100
  libavcodec     57. 66.105 / 57. 66.105
  libavformat    57. 57.100 / 57. 57.100
  libavdevice    57.  2.100 / 57.  2.100
  libavfilter     6. 67.100 /  6. 67.100
  libswscale      4.  3.101 /  4.  3.101
  libswresample   2.  4.100 /  2.  4.100
  libpostproc    54.  2.100 / 54.  2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/tmp/output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.57.100
  Duration: 00:00:00.00, bitrate: N/A