Reputation: 1
I was trying to run a repo located HERE. Basically, just targeting SimpleVideoSummarizer.cc
which uses OpenCV for some basic video processing. I'm using Ubuntu 14.04. Following is the save part of the code:
void SimpleVideoSummarizer::playAndSaveSummaryVideo(char* videoFileSave) {
cv::VideoCapture capture(videoFile);
cv::Mat frame;
capture.set(CV_CAP_PROP_POS_FRAMES, 0);
cv::VideoWriter videoWriter;
if (videoFileSave != "") {
videoWriter = cv::VideoWriter(videoFileSave, CV_FOURCC('M', 'J', 'P', 'G'), static_cast<int>(capture.get(CV_CAP_PROP_FPS)), cv::Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)));
}
for (std::set<int>::iterator it = summarySet.begin(); it != summarySet.end(); it++) {
capture.set(CV_CAP_PROP_POS_FRAMES, segmentStartTimes[*it] * frameRate);
for (int i = segmentStartTimes[*it]; i < segmentStartTimes[*it + 1]; i++) {
for (int j = 0; j < frameRate; j++) {
capture >> frame;
cv::putText(frame, "Time: " + IntToString(i) + " seconds", cvPoint(30, 30),
cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(200, 200, 250), 1, CV_AA);
if (frame.data) {
cv::imshow("Summary Video", frame);
}
if (videoFileSave != "") {
videoWriter.write(frame);
}
// Press ESC on keyboard to exit
char c = static_cast<char>(cv::waitKey(25));
if (c == 27) {
break;
}
}
}
}
capture.release();
}
I pass an input.mp4
file and specify a out.mp4
as well. Unfortunately, when the example is trying to save the output video file, it throws errors on the FOURCC:
OpenCV: FFMPEG: tag 0x44495658/'XVID' is not supported with codec id 12 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'
or another one:
OpenCV: FFMPEG: tag 0x3234504d/'MP42' is not supported with codec id 15 and format 'mp4 / MP4 (MPEG-4 Part 14)'
[mp4 @ 0x16bc700] Could not find tag for codec msmpeg4v2 in stream #0, codec not currently supported in container
I tried to change the FOURCC in this part of the code which writes the video, and applied XVID, MJPG, X264, MP42, MP4V. None worked and threw similar errors.
What is the problem? How to fix it?
Upvotes: 18
Views: 58970
Reputation: 15365
You must use 'a', 'v', 'c', '1'
(avc1
) to request H.264.
ffmpeg is of the opinion that "H264" is an invalid fourcc in an MP4 container.
mp4v
is not H.264. mp4v
is the previous compression standard, implemented by DivX/XviD.
Upvotes: 0
Reputation: 91
For anyone stranded here due to the same error, to write frames to a .mp4
output file, use these chars as arguments:
cv.VideoWriter_fourcc('m', 'p', '4', 'v')
or
cv.VideoWriter_fourcc('H','2','6','4')
In your code try replacing
CV_FOURCC('M', 'J', 'P', 'G')
with
CV_FOURCC('m', 'p', '4', 'v')
I solved my problem with the help of this GitHub comment.
If it does not help, consider this post from the NVIDIA forum and plug in the ASCII number directly in cv2.VideoWriter()
, without using cv2.VideoWriter_fourcc()
like this for example:
cv2.VideoWriter('output.mp4', 0x00000021, 15.0, (1280,360))
Upvotes: 9
Reputation: 115
I fixed similar problem on macOS by install ffmpeg:
macOS:
brew install ffmpeg
Ubuntu:
sudo apt install ffmpeg
Upvotes: 2
Reputation: 591
I basically replaced 'MP4V' with 'mp4v' and got rid of the issue. It seems like that the API is case sensitive.
Upvotes: 48