ffmpeg编码mpeg格式发生下溢

u010421898 2014-09-04 07:03:27

有人用过ffmpeg做mpeg的编码么? 一起讨论一下。我现在在编码中遇到了发生下溢问题,我觉得可能是bvb空间不够,想知道在哪儿设置他的bvb,它默认的是130KB ,应该怎么解决?
还有一个问题是能出位图,但是视频格式里字节为0.我觉得是出在刚才我说的地方的问题了。
麻烦各位大神解答一下了还有另一个帖也没有结,没有人回答。如果现在这个问题解决了 2个帖一起给分.在此先谢谢各位了。
...全文
685 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010421898 2014-09-10
  • 打赏
  • 举报
回复
引用 6 楼 youngalmond11 的回复:
还有你是想先转码么?为什么不直接参照ffmpeg的例子呢?
看过,如果是ffmpeg.c又太庞大了。它每个函数都封装的很好。我可能下载的不对,源码只能在官网上看,无法在程序里打开
youngalmond11 2014-09-06
  • 打赏
  • 举报
回复
还有你是想先转码么?为什么不直接参照ffmpeg的例子呢?
youngalmond11 2014-09-06
  • 打赏
  • 举报
回复
竟然邀请我了……抱歉我还不是大神,只做过flv转码相关的工作……
u010421898 2014-09-05
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include "windows.h"
#include "time.h"
extern "C"
{
#include "libavformat\avformat.h"
#include "libavutil\avutil.h"
#include "libavcodec\avcodec.h"
#include "libswscale\swscale.h"
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
}
void SaveBmp(AVCodecContext *CodecContex, AVFrame *Picture, int width, int height);

#define MAX_BUF_SIZE 256*1024
int _tmain(int argc, char* argv[])
{
	int ret=-1, i=0, videoindex=-1,audioindex=-1, nComplete=0, len=0, bianma=0,wa=0,frame_index=0;//wa专用测试值
	unsigned char *pEnCodeBuf = new unsigned char[MAX_BUF_SIZE];
	char *sourceFile = "666.asf";
	char *destFile = "123.mpeg";
	av_register_all();	
	AVFormatContext *pInputFormatContext=NULL;
	AVCodec *pInputCodec = NULL;
	AVCodecContext *pInputCodecContext = NULL;
	AVPacket InPack;
	int videoWidth, videoHeight;
	AVOutputFormat *pOutputFmt = NULL;
	AVFormatContext *pOutFormatContext = NULL;
	AVCodecContext *pOutCodecContext = NULL;
	AVCodec *pOutCodec = NULL;
	AVStream *pOutStream = NULL;
		AVPacket OutPack;
	AVFrame *OutFrame;
	
	if(ret=avformat_open_input(&pInputFormatContext, sourceFile, NULL, NULL) <0 )
	{
		//打开输入文件
		printf("打开源文件失败 %s—_—\n",sourceFile);
		exit(1);
	}

	if(avformat_find_stream_info(pInputFormatContext,NULL)<0)
	{
		//查找流信息
		printf("找不到相应的解码信息—_—\n");
		goto lk_error;
	}
	av_dump_format(pInputFormatContext, 0, sourceFile, false);
	for(i=0; i<pInputFormatContext->nb_streams; i++)
	{
		if(pInputFormatContext->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
		{
			videoindex=i;
			break;
		}
	/*	else if(pInputFormatContext->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
		{
			audioindex=i;
		}*/
	}

	if(-1 == videoindex)
	{
		//没有找到视频流
		printf("视频流未找到—_—\n");
		goto lk_error;
	}
	
	pInputCodecContext = pInputFormatContext->streams[videoindex]->codec;/
	pInputCodec = avcodec_find_decoder(pInputCodecContext->codec_id);
	
	if(NULL == pInputCodec)
	{
		
		printf("解码器未找到—_—\n");
		//没有找到解码器
		goto lk_error;
	}
	if(avcodec_open2(pInputCodecContext, pInputCodec,NULL) != 0)
	{
		printf("解码器打开失败了—_—\n");//打开解码器失败
		goto lk_error;
	}


	videoWidth  = pInputCodecContext->width ;
	videoHeight = pInputCodecContext->height;

	pOutputFmt = av_guess_format (NULL, destFile, NULL);
	if(NULL == pOutputFmt)
	{
		printf("分析输出文件失败了—_—\n");
		//分析输出文件格式失败
		goto lk_error;
	}

	pOutFormatContext =avformat_alloc_context();
	if(NULL == pOutFormatContext)
	{
		goto lk_error;
	}

	pOutFormatContext->oformat = pOutputFmt;
	pOutStream = av_new_stream(pOutFormatContext, 0);
	if(NULL == pOutStream)
	{
		printf("创建流失败了—_—\n");
		//创建流失败
		goto lk_error;
	}
	
	pOutCodecContext = pOutStream->codec;
	pOutCodecContext->codec_id = CODEC_ID_MPEG1VIDEO;
	pOutCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
	pOutCodecContext->bit_rate = 400000;
	pOutCodecContext->width = 352;
	pOutCodecContext->height =288;
	pOutCodecContext->time_base = pInputCodecContext->time_base;
	pOutCodecContext->gop_size = pInputCodecContext->gop_size;
	pOutCodecContext->pix_fmt = pInputCodecContext->pix_fmt;
	pOutCodecContext->max_b_frames = pInputCodecContext->max_b_frames;
	pOutCodecContext->time_base.den = 25;
	pOutCodecContext->time_base.num = 1;
	pOutStream->r_frame_rate = pInputFormatContext->streams[videoindex]->r_frame_rate;
	
	
	
	pOutCodec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
	if(NULL == pOutCodec)
	{
		printf("相应的编码器找不到不是MPEG1—_—\n");
		//找不到指定编码器
		goto lk_error;
	}
	
	if(avcodec_open2(pOutCodecContext, pOutCodec,NULL) < 0)
	{
		printf("编码器打开出错。不是mpeg1—_—\n");
		//打开指定编码器错误
		goto lk_error;
	}
	
	if (!(pOutFormatContext->flags & AVFMT_NOFILE))
	{
		if(avio_open(&pOutFormatContext->pb, destFile, AVIO_FLAG_WRITE)<0)//(url_fopen(&pOutFormatContext->pb, destFile, URL_WRONLY)<0)AVIO_FLAG_READ_WRITE
			//1表示只读,2表示只写 还有个可读可写
		{
			printf("无法打开输出文件—_—\n");
			//打开输出文件
			goto lk_error;
		}
	}

	 if(!pOutFormatContext->nb_streams)
    {
           fprintf(stderr,"输出文件里没有流\n");
           exit(0);
    }



	if(avformat_write_header(pOutFormatContext,NULL) < 0)
	{
		printf("输出容器写失败了!—_—\n");
		//写入输出文件头失败
		goto lk_error;
	}

	av_dump_format(pOutFormatContext, 0, destFile, true);//输出信息

	while((wa=av_read_frame(pInputFormatContext, &InPack)) >= 0)
	{
		OutFrame=avcodec_alloc_frame();

		
		len = avcodec_decode_video2(pInputCodecContext, OutFrame, &nComplete, &InPack);
		if(nComplete >0)
		{   
			memset(pEnCodeBuf,0, MAX_BUF_SIZE);
			
		
			
			fflush(stdout);
			OutFrame->pts = av_rescale(frame_index, AV_TIME_BASE*(int64_t)pOutCodecContext->time_base.num, pOutCodecContext->time_base.den);
			。
			OutFrame->pict_type = AV_PICTURE_TYPE_NONE;
		
			
			 bianma= avcodec_encode_video(pOutCodecContext, pEnCodeBuf, MAX_BUF_SIZE, OutFrame);  
			
			
			
			if (bianma >= 0)        //编码成功了                                                       
			{  
				//ffmpeg写基本输出方式

				av_init_packet(&OutPack); //输出包初始化                              
				if(pOutCodecContext->coded_frame && pOutCodecContext->coded_frame->key_frame)                                       
				{
					OutPack.flags |= AV_PKT_FLAG_KEY;   
				}

				OutPack.flags = InPack.flags;                 
				OutPack.stream_index = InPack.stream_index;                                               
				OutPack.data = pEnCodeBuf;    
				OutPack.size = bianma;         // bianma包大小                                 
				ret=av_write_frame(pOutFormatContext, &OutPack);   
			
			
			}
			frame_index++;

		}
		av_free_packet(&OutPack);
		
	//	av_free(IInPack);
        av_free(OutFrame);
	}
	
	av_write_trailer(pOutFormatContext);
	for(i=0; i<(pOutFormatContext->nb_streams); i++)
	{            
		av_freep(&pOutFormatContext->streams[i]->codec);                       
		av_freep(&pOutFormatContext->streams[i]);                           
	}

lk_error:
	delete  pEnCodeBuf;
	av_free(pOutCodec);
	
	av_free(pOutputFmt);
	avcodec_close(pInputCodecContext);
	av_free(pInputFormatContext);
	av_free(pOutFormatContext);
	

	return 0;
}
上码 求帮助
u010421898 2014-09-05
  • 打赏
  • 举报
回复
引用 1 楼 fang098 的回复:
[quote=引用 楼主 u010421898 的回复:] 有人用过ffmpeg做mpeg的编码么? 一起讨论一下。我现在在编码中遇到了发生下溢问题,我觉得可能是bvb空间不够,想知道在哪儿设置他的bvb,它默认的是130KB ,应该怎么解决? 还有一个问题是能出位图,但是视频格式里字节为0.我觉得是出在刚才我说的地方的问题了。 麻烦各位大神解答一下了还有另一个帖也没有结,没有人回答。如果现在这个问题解决了 2个帖一起给分.在此先谢谢各位了。
下溢问题不知道楼主说的是什么,位图?你是YUV转成BMP存储出来的你不是要MEPG编码吗?[/quote] 是这样引用 才能看到么
u010421898 2014-09-05
  • 打赏
  • 举报
回复
引用 1 楼 fang098 的回复:
[quote=引用 楼主 u010421898 的回复:] 下溢问题不知道楼主说的是什么,位图?你是YUV转成BMP存储出来的你不是要MEPG编码吗?
位图是用来保存在解码后,编码中后几帧图像无法正确编码的图像。下溢问题是这样的(我不会发图片,只能手敲了 希望楼下教教) 【mpeg @ 01eb1d00】buffer underflow st=0 bufi=2014 size=3962 【mpeg @ 01eb1d00】buffer underflow st=0 bufi=2014 size=3962 【mpeg @ 01eb1d00】buffer underflow st=0 bufi=2014 size=3962 全是这个。 然后size的大是根据我设置的编码 比特率和宽高改变的,但最小也只能这么多。 我的想法是想把BUFI调大一些
fang 2014-09-05
  • 打赏
  • 举报
回复
引用 楼主 u010421898 的回复:
有人用过ffmpeg做mpeg的编码么? 一起讨论一下。我现在在编码中遇到了发生下溢问题,我觉得可能是bvb空间不够,想知道在哪儿设置他的bvb,它默认的是130KB ,应该怎么解决? 还有一个问题是能出位图,但是视频格式里字节为0.我觉得是出在刚才我说的地方的问题了。 麻烦各位大神解答一下了还有另一个帖也没有结,没有人回答。如果现在这个问题解决了 2个帖一起给分.在此先谢谢各位了。
下溢问题不知道楼主说的是什么,位图?你是YUV转成BMP存储出来的你不是要MEPG编码吗?

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧