yuv序列转avi源代码

来源:互联网 发布:图片制作软件下载 编辑:程序博客网 时间:2024/05/21 15:03

//  http://yeyingxian.blog.163.com/blog/static/344712420096296270569/

 

yuv2avi,用ffmpeg SDK做的,用的还是output_example.c的一套代码。关键是写好codec_tag,I420是YUV格式,YV12是YVU格式。ffmpeg sdk下载地址
http://www.bairuitech.com/html/ruanjianxiazai/ffmpeg/20080414/89.html

#include "libavformat/avformat.h"const int WIDTH = 320;const int HEIGHT = 240;unsigned char buf[WIDTH *HEIGHT *3];int main(){    int i;    FILE *fin = fopen("e:\\1031.yuv", "rb");    AVOutputFormat *pOutputFormat;    AVFormatContext *pFormatCtxEnc;    AVStream *video_st;    AVCodecContext *pCodecCtxEnc;    const char *filename = "1.avi";    av_register_all();    // auto detect the output format from the name    pOutputFormat = guess_format(NULL, filename, NULL);    // allocate the output media context    pFormatCtxEnc = av_alloc_format_context();    pFormatCtxEnc->oformat = pOutputFormat;    snprintf(pFormatCtxEnc->filename, sizeof(pFormatCtxEnc->filename), "%s", filename);    video_st = av_new_stream(pFormatCtxEnc, 0);    pCodecCtxEnc = video_st->codec;    pCodecCtxEnc->codec_id = CODEC_ID_RAWVIDEO;    pCodecCtxEnc->codec_type = CODEC_TYPE_VIDEO;    pCodecCtxEnc->codec_tag = MKTAG('I', '4', '2', '0');    // put sample parameters     // resolution must be a multiple of two     pCodecCtxEnc->width = WIDTH;    pCodecCtxEnc->height = HEIGHT;    // frames per second     pCodecCtxEnc->time_base.den = 25;    pCodecCtxEnc->time_base.num = 1;    pCodecCtxEnc->pix_fmt = PIX_FMT_YUV420P;    // set the output parameters (must be done even if no parameters).    if (av_set_parameters(pFormatCtxEnc, NULL) < 0) {        return -1;    }    dump_format(pFormatCtxEnc, 0, filename, 1);        if (url_fopen(&pFormatCtxEnc->pb, filename, URL_WRONLY) < 0) {            fprintf(stderr, "Could not open '%s'\n", filename);            return -1;        }    // write the stream header, if any    av_write_header(pFormatCtxEnc);    for ( i = 0; i < 300; i++)    {        fread(buf, WIDTH, HEIGHT, fin);        fread(buf + WIDTH*HEIGHT, WIDTH/2, HEIGHT/2, fin);        fread(buf + WIDTH*HEIGHT*5/4, WIDTH/2, HEIGHT/2, fin);        AVPacket pkt;        av_init_packet(&pkt);        pkt.flags |= PKT_FLAG_KEY;        pkt.stream_index= video_st->index;        pkt.data= buf;        pkt.size= WIDTH*HEIGHT*3/2;        // write the compressed frame in the media file        av_write_frame(pFormatCtxEnc, &pkt);    }    // write the trailer, if any    av_write_trailer(pFormatCtxEnc);    // free the streams    for(i = 0; i < pFormatCtxEnc->nb_streams; i++) {        av_freep(&pFormatCtxEnc->streams[i]->codec);        av_freep(&pFormatCtxEnc->streams[i]);    }    if (!(pOutputFormat->flags & AVFMT_NOFILE)) {        /* close the output file */        url_fclose(pFormatCtxEnc->pb);    }    /* free the stream */    av_free(pFormatCtxEnc);    fclose(fin);    return 0;}


 

原创粉丝点击