remuxing.c FFMPEG读文件改为读内存实现H.264格式视频实时转码为FLV

来源:互联网 发布:js面试题及答案2017 编辑:程序博客网 时间:2024/05/04 20:23

FFMPEG在DOC/EXAMPLES/remuxing.c实现视频格式的转化,可以实现H.264格式视频文件,转码为FLV格式

但是我的需求是读取网络摄像头视频,视频格式为H.264,并实时转化为FLV格式视频流。

我的解决思路,我共用了两个线程实现,一个线程读取网络摄像头视频流,一个线程将网络摄像头视频流由H.264格式转化为FLV格式视频流,两个线程之间通过共享内存信号量方式实现。

本来remuxing.c通过读文件获取到视频的信息,然后通过获取到的视频的信息实现视频转码;我的思路是让rexuing.c读取一个视频文件,获取到视频信息,然后在remuxing.c中的while循环中将//ret = av_read_frame(ifmt_ctx, &pkt); 行代码注释掉,改为 pkt.data=pH264->h264;
        pkt.size=pH264->h264_size;读取共享内存中的视频数据和视频数据长度,就可以了。

@Jason 给我提供了很大的帮助,其中也参考了http://blog.csdn.net/leixiaohua1020/article/details/12980423#userconsent#这篇文章,但是没有调通,卡在了

if(avformat_open_input(&pFormatCtx,NULL,piFmt,NULL)!=0){//打开流媒体(或本地文件)的函数
        printf("无法打开文件\n");
        return -1;
这里 一直内存报错。就没有再调。
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h> //包含线程相关头文件
#include <errno.h>
#include <sys/ipc.h>
#include <semaphore.h> //包含信号量相关头文件
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include "QV_NET.h"
#include "share.h"
#define ZHANG_DEBUG
#ifdef ZHANG_DEBUG
sem_t sem1,sem2; //声明两个信号量
void pthread1(void *arg); //声明两个线程函数
void pthread2(void *arg);
struct H264_DATA *pH264;
#endif
void pthread1(void *arg) //线程1的执行内容
{   
    while(1)
    {
    sem_wait(&sem2); //线程阻塞一直等到sem2信号量大于0,执行后将sem2减1,代表资源已经被使用
    memcpy(pH264->h264,pu8Buffer,u32Length);
    pH264->h264_size = u32Length;
    sem_post(&sem1); //将信号量sem1的值加1,代表资源增加
    }
}
int main(int argc, char* argv[])
{   pthread_t id1,id2; //声明两个线程
    int ret;
    pH264 = (struct H264_DATA *)malloc(sizeof(struct H264_DATA));
    ret=sem_init(&sem1,0,1); //对信号量进行初始化,第一个0表示此信号量子整个进程中共享,第二个1表示信号量初始值
    ret=sem_init(&sem2,0,0);
    if(ret!=0)
    {
        perror("sem_init");
    }
    ret=pthread_create(&id1,NULL,(void *)pthread1, NULL); //创建线程
    if(ret!=0)
        perror("pthread cread1");
    ret=pthread_create(&id2,NULL,(void *)pthread2, NULL);
    if(ret!=0)
        perror("pthread cread2");
    pthread_join(id1,NULL); //用来等待线程1的结束
    pthread_join(id2,NULL); //用来等待线程2的结束
    exit(0);
return 0;
}
void pthread2(void *arg)
{
    int nolock=0;
    int ret;
    int err2;
    /*if((pfile2=fopen("pfile2.264","wb"))!=NULL)


    printf("The file is opened!\n");
    else
    printf("The file is not opened!\n");*/
    AVOutputFormat *ofmt = NULL;
    AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
    AVPacket pkt;
    const char *in_filename, *out_filename;
    int  i;


    in_filename  = "file1.264";
    out_filename = "file.flv";


    av_register_all();


    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
        fprintf(stderr, "Could not open input file '%s'", in_filename);
        goto end;
    }


    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information");
        goto end;
    }


    av_dump_format(ifmt_ctx, 0, in_filename, 0);


    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
    if (!ofmt_ctx) {
        fprintf(stderr, "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }


    ofmt = ofmt_ctx->oformat;


    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream *in_stream = ifmt_ctx->streams[i];
        AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            goto end;
        }


        ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
        if (ret < 0) {
            fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
            goto end;
        }
        out_stream->codec->codec_tag = 0;
        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
            out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }
    av_dump_format(ofmt_ctx, 0, out_filename, 1);


    if (!(ofmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open output file '%s'", out_filename);
            goto end;
        }
    }


    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        goto end;
    }
    sleep(5);
    while(1){


    sem_wait(&sem1);
    //fwrite(pH264->h264,pH264->h264_size,1,pfile2);
    printf("Thread 2\n");
    AVStream *in_stream, *out_stream;


        //ret = av_read_frame(ifmt_ctx, &pkt);
        //if (ret < 0)
         //   break;
        pkt.data=pH264->h264;
        pkt.size=pH264->h264_size;
        in_stream  = ifmt_ctx->streams[pkt.stream_index];
        out_stream = ofmt_ctx->streams[pkt.stream_index];


        //log_packet(ifmt_ctx, &pkt, "in");


        /* copy packet */
        pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
        pkt.pos = -1;
        //log_packet(ofmt_ctx, &pkt, "out");


        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
        if (ret < 0) {
            fprintf(stderr, "Error muxing packet\n");
            break;
        }
        av_free_packet(&pkt);
    sem_post(&sem2);
    }
    //fclose(pfile2);
     av_write_trailer(ofmt_ctx);
end:


    avformat_close_input(&ifmt_ctx);


    /* close output */
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
        avio_close(ofmt_ctx->pb);
    avformat_free_context(ofmt_ctx);


    if (ret < 0 && ret != AVERROR_EOF) {
        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
        return 1;
    }


    return 0;
}
代码不能够运行,因为我只写了一半,另一半需要你自己填上读其他摄像头的视频流,并把视频流添加到共享内存中去。你把线程一的采集视频加上去代码就可以运行了
附下载地址:

http://download.csdn.net/detail/chaoyuebaihu/7713187

0 0
原创粉丝点击