MP4可以封装MJPEG格式吗,帮忙给出参考代码
时间: 2024-01-16 18:02:48 浏览: 215
是的,MP4可以封装MJPEG格式。下面是一个使用FFmpeg库进行封装的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/time.h>
#define STREAM_DURATION 10.0
#define STREAM_FRAME_RATE 25 /* 25 images/s */
#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
static AVFormatContext *format_ctx;
static AVCodecContext *codec_ctx;
static AVStream *stream;
static char *output_file = "output.mp4";
static int video_frame_count;
static int open_video(void)
{
AVCodec *codec;
int ret;
codec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
codec_ctx->bit_rate = 400000;
codec_ctx->width = 640;
codec_ctx->height = 480;
codec_ctx->time_base = (AVRational){1, STREAM_FRAME_RATE};
codec_ctx->pix_fmt = STREAM_PIX_FMT;
codec_ctx->codec_id = AV_CODEC_ID_MJPEG;
codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
if (format_ctx->oformat->flags & AVFMT_GLOBALHEADER)
codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
ret = avcodec_open2(codec_ctx, codec, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
exit(1);
}
stream = avformat_new_stream(format_ctx, NULL);
if (!stream) {
fprintf(stderr, "Could not allocate video stream\n");
exit(1);
}
ret = avcodec_parameters_from_context(stream->codecpar, codec_ctx);
if (ret < 0) {
fprintf(stderr, "Could not copy codec parameters: %s\n", av_err2str(ret));
exit(1);
}
av_dump_format(format_ctx, 0, output_file, 1);
return 0;
}
static void write_video_frame(AVFrame *frame)
{
int ret;
AVPacket pkt = { 0 };
av_init_packet(&pkt);
/* encode the image */
ret = avcodec_send_frame(codec_ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending a frame for encoding: %s\n", av_err2str(ret));
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_packet(codec_ctx, &pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error during encoding: %s\n", av_err2str(ret));
exit(1);
}
pkt.stream_index = stream->index;
/* Write the compressed frame to the media file. */
av_interleaved_write_frame(format_ctx, &pkt);
av_packet_unref(&pkt);
}
}
int main(int argc, char **argv)
{
int ret;
AVFrame *frame = NULL;
int got_frame = 0;
int64_t start_time;
av_register_all();
avformat_alloc_output_context2(&format_ctx, NULL, NULL, output_file);
if (!format_ctx) {
fprintf(stderr, "Could not create output context\n");
exit(1);
}
open_video();
ret = avio_open(&format_ctx->pb, output_file, AVIO_FLAG_WRITE);
if (ret < 0) {
fprintf(stderr, "Could not open output file '%s': %s\n", output_file, av_err2str(ret));
exit(1);
}
start_time = av_gettime();
avformat_write_header(format_ctx, NULL);
for (;;) {
AVPacket pkt = { 0 };
av_init_packet(&pkt);
/* Prepare a dummy image. */
if (!frame) {
if (!(frame = av_frame_alloc())) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
frame->format = codec_ctx->pix_fmt;
frame->width = codec_ctx->width;
frame->height = codec_ctx->height;
/* Allocate the buffer for the frame data. */
ret = av_image_alloc(frame->data, frame->linesize, codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, 32);
if (ret < 0) {
fprintf(stderr, "Could not allocate raw picture buffer\n");
exit(1);
}
}
/* Prepare a dummy image. */
/* Y */
for (int y = 0; y < codec_ctx->height; y++) {
for (int x = 0; x < codec_ctx->width; x++) {
frame->data[0][y * frame->linesize[0] + x] = x + y + video_frame_count * 3;
}
}
/* Cb and Cr */
for (int y = 0; y < codec_ctx->height / 2; y++) {
for (int x = 0; x < codec_ctx->width / 2; x++) {
frame->data[1][y * frame->linesize[1] + x] = 128 + y + video_frame_count * 2;
frame->data[2][y * frame->linesize[2] + x] = 64 + x + video_frame_count * 5;
}
}
frame->pts = av_rescale_q(av_gettime() - start_time, (AVRational){1, 1000000}, codec_ctx->time_base);
video_frame_count++;
write_video_frame(frame);
if (video_frame_count >= STREAM_DURATION * STREAM_FRAME_RATE)
break;
}
av_write_trailer(format_ctx);
avcodec_free_context(&codec_ctx);
av_frame_free(&frame);
avio_closep(&format_ctx->pb);
avformat_free_context(format_ctx);
return 0;
}
```
这个示例代码将生成一个10秒长的MJPEG格式视频,并将其封装为MP4格式。你可以根据自己的需求修改代码中的参数。
阅读全文