利用ffmpeg將YUV420P轉成jpg格式文件,保存

我的程序需要在文件轉碼的同時去截圖,按首禎截圖和時間序列截圖,但困擾於如何保存成jpg格式。

方案一:

利用ffmpeg將從文件讀取出來的禎解碼成YUV420P像素格式,用ffmpeg編碼成jpg格式

借鑑於雷博士的最簡單的基於FFmpeg的圖像編碼器    連接http://blog.csdn.net/leixiaohua1020/article/details/25346147

方案二:

自己按照jpg格式,寫jpg文件或者利用libjpeg轉換

提供一個jpg格式頭分析,連接 : http://www.cnblogs.com/lakeone/p/3596996.html

用libjpeg開源庫去轉換,我的程序內沒采用這種方案,提供個連接:http://blog.csdn.net/yixianfeng41/article/details/52181578

 

下面貼上我的代碼:

/**************************************************
* 函數名稱: Frame2JPG
* 功能描述: 將AVFrame(YUV420格式)保存爲JPEG格式的圖片
* 參    數: AVPacket packetav_read_frame讀取的一包數據
* 參    數: AVFrame * pFrame解碼完的幀
* 參    數: stream_index流下標,標記是視頻流還是音頻流
* 參    數: int widthYUV420的寬
* 參    數: int heightYUV420的高
* 返 回 值: int 0 代表成功,其他失敗
* 其他說明: 
* 其它說明: 
* 修改日期 修改人 修改內容
* --------------------------------------------
**************************************************/

int Frame2JPG(AVPacket packet, AVFrame* pFrame, unsigned int stream_index, 
	int width, int height)
{
	// 輸出文件路徑  
	char out_file[MAX_PATH] = { 0 };
	sprintf_s(out_file, sizeof(out_file), "%s%d.jpg", "e:\\", packet.pts);

	// 分配AVFormatContext對象  
	AVFormatContext* pFormatCtx = avformat_alloc_context();

	// 設置輸出文件格式  
	pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);

	// 創建並初始化一個和該url相關的AVIOContext  
	if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) 
	{
		printf("Couldn't open output file");
		return -1;
	}

	// 構建一個新stream  
	AVStream* pAVStream = avformat_new_stream(pFormatCtx, 0);
	if (pAVStream == NULL) 
	{
		printf("Frame2JPG::avformat_new_stream error.");
		return -1;
	}

	// 設置該stream的信息  
	AVCodecContext* pCodecCtx = pAVStream->codec;

	pCodecCtx->codec_id = pFormatCtx->oformat->video_codec;
	pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
	pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;
	pCodecCtx->width = width;
	pCodecCtx->height = height;
	pCodecCtx->time_base.num = 1;
	pCodecCtx->time_base.den = 25;

	// 查找解碼器  
	AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
	if (!pCodec) 
	{
		printf("avcodec_find_encoder() error.");
		return -1;
	}
	// 設置pCodecCtx的解碼器爲pCodec  
	if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) 
	{
		printf("Could not open codec.");
		return -1;
	}

	//Write Header  
	int ret = avformat_write_header(pFormatCtx, NULL);
	if (ret < 0)
	{
		printfe("avformat_write_header() error.\n");
		return -1;
	}

	int y_size = pCodecCtx->width * pCodecCtx->height;

	//Encode  
	// 給AVPacket分配足夠大的空間  
	AVPacket pkt;
	av_new_packet(&pkt, y_size * 3);

	int got_picture = 0;
	ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_picture);
	if (ret < 0) 
	{
		printf("avcodec_encode_video2() error.\n");
		return -1;
	}

	if (got_picture == 1) 
	{ 
		ret = av_write_frame(pFormatCtx, &pkt);
	}

	av_free_packet(&pkt);

	//Write Trailer  
	av_write_trailer(pFormatCtx);

	if (pAVStream) 
	{
		avcodec_close(pAVStream->codec);
	}

	avio_close(pFormatCtx->pb);
	avformat_free_context(pFormatCtx);

	return 0;
}


如有不足之處,還請指出。

 

下一步將把編譯ffmpeg以及轉碼demo附上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章