使用FFMPEG类库分离出多媒体文件中的H.264码流

来源:互联网 发布:欧洲难民感恩 知乎 编辑:程序博客网 时间:2024/06/08 02:49

在使用FFMPEG的类库进行编程的过程中,可以直接输出解复用之后的的视频数据码流。只需要在每次调用av_read_frame()之后将得到的视频的AVPacket存为本地文件即可。

经试验,在分离MPEG2码流的时候,直接存储AVPacket即可。

在分离H.264码流的时候,直接存储AVPacket后的文件可能是不能播放的。

如果视音频复用格式是TS(MPEG2 Transport Stream),直接存储后的文件是可以播放的。

复用格式是FLV,MP4则不行。

经过长时间资料搜索发现,FLV,MP4这些属于“特殊容器”,需要经过以下处理才能得到可播放的H.264码流:

1.第一次存储AVPacket之前需要在前面加上H.264的SPS和PPS。这些信息存储在AVCodecContext的extradata里面。

并且需要使用FFMPEG中的名为"h264_mp4toannexb"的bitstream filter 进行处理。

然后将处理后的extradata存入文件

具体代码如下:

FILE *fp_h264 = fopen("film.h264", "wb+"); unsigned char *dummy=NULL;   //输入的指针int dummy_len;AVBitStreamFilterContext* bsfc = av_bitstream_filter_init("h264_mp4toannexb");    av_bitstream_filter_filter(bsfc, pCodecCtx, NULL, &dummy, &dummy_len, NULL, 0, 0);  fwrite(pCodecCtx->extradata,pCodecCtx->extradata_size,1,fp_h264);  av_bitstream_filter_close(bsfc);    free(dummy);  


2.通过查看FFMPEG源代码我们发现,AVPacket中的数据起始处没有分隔符(0x00000001), 也不是0x65、0x67、0x68、0x41等字节,所以可以AVPacket肯定这不是标准的nalu。其实,AVPacket前4个字表示的是nalu的长度,从第5个字节开始才是nalu的数据。所以直接将AVPacket前4个字节替换为0x00000001即可得到标准的nalu数据。

具体代码如下:

char nal_start[]={0,0,0,1};fwrite(nal_start, 4, 1, fp_h264);fwrite(packet.data + 4, packet.size - 4, 1, fp_h264);fclose(fp_h264);

经过以上两步处理之后,我们就得到了可以正常播放的H.264码流

/***  使用FFmpeg解析出H264、YUV数据*/#include <stdio.h>extern "C"{#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"#include "libswscale/swscale.h"#include "libavutil/imgutils.h"};#pragma comment(lib, "avcodec.lib")#pragma comment(lib, "avformat.lib")#pragma comment(lib, "swscale.lib")#pragma comment(lib, "avutil.lib")int main(int argc, char* argv[]){AVFormatContext*pFormatCtx = NULL;AVCodecContext*pCodecCtx = NULL;AVCodec*pCodec = NULL;AVFrame*pFrame = NULL, *pFrameYUV = NULL;unsigned char*out_buffer = NULL;AVPacketpacket;struct SwsContext*img_convert_ctx = NULL;intgot_picture;intvideoIndex;intframe_cnt = 1;//char filepath[] = "Titanic.ts";char filepath[] = "Forrest_Gump_IMAX.mp4";FILE *fp_yuv = fopen("film.yuv", "wb+");FILE *fp_h264 = fopen("film.h264", "wb+");if (fp_yuv == NULL || fp_h264 == NULL){printf("FILE open error");return -1;}av_register_all();if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0){printf("Couldn't open an input stream.\n");return -1;}if (avformat_find_stream_info(pFormatCtx, NULL) < 0){printf("Couldn't find stream information.\n");return -1;}videoIndex = -1;for (int i = 0; i < pFormatCtx->nb_streams; i++)if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){videoIndex = i;break;}if (videoIndex == -1){printf("Couldn't find a video stream.\n");return -1;}pCodecCtx = pFormatCtx->streams[videoIndex]->codec;pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL){printf("Codec not found.\n");return -1;}if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){printf("Could not open codec.\n");return -1;}pFrame = av_frame_alloc();pFrameYUV = av_frame_alloc();if (pFrame == NULL || pFrameYUV == NULL){printf("memory allocation error\n");return -1;}out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1));av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer,AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);/*//针对H.264码流unsigned char *dummy = NULL;   //输入的指针  int dummy_len;const char nal_start[] = { 0, 0, 0, 1 };AVBitStreamFilterContext* bsfc = av_bitstream_filter_init("h264_mp4toannexb");av_bitstream_filter_filter(bsfc, pCodecCtx, NULL, &dummy, &dummy_len, NULL, 0, 0);fwrite(pCodecCtx->extradata, pCodecCtx->extradata_size, 1, fp_h264);av_bitstream_filter_close(bsfc);free(dummy);*/while (av_read_frame(pFormatCtx, &packet) >= 0){if (packet.stream_index == videoIndex){//输出出h.264数据fwrite(packet.data, 1, packet.size, fp_h264);//针对H.264码流//fwrite(nal_start, 4, 1, fp_h264);//fwrite(packet.data + 4, packet.size - 4, 1, fp_h264);if (avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet) < 0){printf("Decode Error.\n");return -1;}if (got_picture){sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameYUV->data, pFrameYUV->linesize);//输出出YUV数据int y_size = pCodecCtx->width * pCodecCtx->height;fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);//Y fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);//Ufwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);//Vprintf("Succeed to decode %d frame!\n", frame_cnt);frame_cnt++;}}av_free_packet(&packet);}//flush decoder//FIX: Flush Frames remained in Codecwhile (true){if (avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, &packet) < 0){break;}if (!got_picture){break; }sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameYUV->data, pFrameYUV->linesize);int y_size = pCodecCtx->width * pCodecCtx->height;fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);//Y fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);//Ufwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);//Vprintf("Flush Decoder: Succeed to decode %d frame!\n", frame_cnt);frame_cnt++;}fclose(fp_yuv);fclose(fp_h264);sws_freeContext(img_convert_ctx);av_free(out_buffer);av_frame_free(&pFrameYUV);av_frame_free(&pFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0;}


0 0
原创粉丝点击