1. 程式人生 > >ffmpeg音訊轉碼,採用swr_convert重取樣

ffmpeg音訊轉碼,採用swr_convert重取樣


/*
*最簡單的音訊轉碼器(只處理音訊)
*繆國凱 Mickel
*[email protected]
*本程式實現從一個視訊格式轉碼到另一個視訊格式,只處理音訊,視訊忽略,若有多個音訊流,只處理第一個,採用swr_convert重取樣
*2015-5-8
*/


#include "stdafx.h"

#ifdef __cplusplus
extern"C"
{
#endif
#include <libavformat/avformat.h>
#include "libavcodec/avcodec.h"
#include "libavfilter/avfiltergraph.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavutil/avutil.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libswresample\swresample.h"
#include "libavutil\fifo.h"


#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
	//#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
	//#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
	//#pragma comment(lib, "swscale.lib")
#ifdef __cplusplus
};
#endif

#define BUF_SIZE_20K 2048000
#define BUF_SIZE_1K 1024000

SwrContext* pSwrCtx = NULL;
AVStream *out_stream = NULL;
AVFormatContext *in_fmt_ctx = NULL, *out_fmt_ctx = NULL;
int audio_index = -1;

void initSwr()
{
	if (out_fmt_ctx->streams[0]->codec->channels != in_fmt_ctx->streams[audio_index]->codec->channels
		|| out_fmt_ctx->streams[0]->codec->sample_rate != in_fmt_ctx->streams[audio_index]->codec->sample_rate
		|| out_fmt_ctx->streams[0]->codec->sample_fmt != in_fmt_ctx->streams[audio_index]->codec->sample_fmt)
	{
		if ( NULL == pSwrCtx ) 
		{
			pSwrCtx = swr_alloc();
		}
#if LIBSWRESAMPLE_VERSION_MINOR >= 17	// 根據版本不同,選用適當函式
		av_opt_set_int(pSwrCtx, "ich", in_fmt_ctx->streams[audio_index]->codec->channels, 0);
		av_opt_set_int(pSwrCtx, "och", out_fmt_ctx->streams[0]->codec->channels, 0);
		av_opt_set_int(pSwrCtx, "in_sample_rate",  in_fmt_ctx->streams[audio_index]->codec->sample_rate, 0);
		av_opt_set_int(pSwrCtx, "out_sample_rate",  out_fmt_ctx->streams[0]->codec->sample_rate, 0);
		av_opt_set_sample_fmt(pSwrCtx, "in_sample_fmt", in_fmt_ctx->streams[audio_index]->codec->sample_fmt, 0);
		av_opt_set_sample_fmt(pSwrCtx, "out_sample_fmt", out_fmt_ctx->streams[0]->codec->sample_fmt, 0);

#else
		pSwrCtx = swr_alloc_set_opts(pSwrCtx,			
			out_fmt_ctx->streams[0]->codec->channel_layout, 
			out_fmt_ctx->streams[0]->codec->sample_fmt, 
			out_fmt_ctx->streams[0]->codec->->sample_rate,
			in_fmt_ctx->streams[audio_index]->codec->channel_layout, 
			in_fmt_ctx->streams[audio_index]->codec->sample_fmt, 
			in_fmt_ctx->streams[audio_index]->codec->sample_rate,
			0, NULL);
#endif
		swr_init(pSwrCtx);
	}
}

//setup_array函式摘自ffmpeg例程
static void setup_array(uint8_t* out[SWR_CH_MAX], AVFrame* in_frame, int format, int samples)
{
	if (av_sample_fmt_is_planar((AVSampleFormat)format)) 
	{
		int i;int plane_size = av_get_bytes_per_sample((AVSampleFormat)(format & 0xFF)) * samples;format &= 0xFF;
		
		//從decoder出來的frame中的data資料不是連續分佈的,所以不能這樣寫:
		in_frame->data[0]+i*plane_size;
		for (i = 0; i < in_frame->channels; i++)
		{
			out[i] = in_frame->data[i];
		}
	} 
	else
	{
		out[0] = in_frame->data[0];
	}
}

int TransSample(AVFrame *in_frame, AVFrame *out_frame)
{
	int ret;
	int max_dst_nb_samples = 4096;
	//int64_t dst_nb_samples;
	int64_t src_nb_samples = in_frame->nb_samples;
	out_frame->pts = in_frame->pts;
	uint8_t* paudiobuf;
	int decode_size, input_size, len;
	if (pSwrCtx != NULL) 
	{
		out_frame->nb_samples = av_rescale_rnd(swr_get_delay(pSwrCtx, out_fmt_ctx->streams[0]->codec->sample_rate) + src_nb_samples,
			out_fmt_ctx->streams[0]->codec->sample_rate, in_fmt_ctx->streams[audio_index]->codec->sample_rate, AV_ROUND_UP);

		ret = av_samples_alloc(out_frame->data, 
			&out_frame->linesize[0], 
			out_fmt_ctx->streams[0]->codec->channels, 
			out_frame->nb_samples, 
			out_fmt_ctx->streams[0]->codec->sample_fmt, 0);
		
		if (ret < 0)
		{
			av_log(NULL, AV_LOG_WARNING, "[%s.%d %s() Could not allocate samples Buffer\n", __FILE__, __LINE__, __FUNCTION__);
			return -1;
		}
		
		max_dst_nb_samples = out_frame->nb_samples;
		//輸入也可能是分平面的,所以要做如下處理
		uint8_t* m_ain[SWR_CH_MAX];
		setup_array(m_ain, in_frame, in_fmt_ctx->streams[audio_index]->codec->sample_fmt, src_nb_samples);
		
		//注意這裡,out_count和in_count是samples單位,不是byte
		//所以這樣av_get_bytes_per_sample(in_fmt_ctx->streams[audio_index]->codec->sample_fmt) * src_nb_samples是錯的
		len = swr_convert(pSwrCtx, out_frame->data, out_frame->nb_samples, (const uint8_t**)m_ain, src_nb_samples);
		if (len < 0)
		{
			char errmsg[BUF_SIZE_1K];
			av_strerror(len, errmsg, sizeof(errmsg));
			av_log(NULL, AV_LOG_WARNING, "[%s:%d] swr_convert!(%d)(%s)", __FILE__, __LINE__, len, errmsg);
			return -1;
		}
	}
	else
	{
		printf("pSwrCtx with out init!\n");
		return -1;
	}
	return 0;
}

int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index)
{
	int ret;
	int got_frame;
	AVPacket enc_pkt;
	if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &CODEC_CAP_DELAY))
	{
		return 0;
	}
	
	int i = 0;
	while(1)
	{
		enc_pkt.data = NULL;
		enc_pkt.size = 0;
		av_init_packet(&enc_pkt);
		ret = avcodec_encode_audio2(out_fmt_ctx->streams[stream_index]->codec, &enc_pkt,NULL, &got_frame);
		if (ret < 0)
			break;
		
		if (!got_frame)
			break;
		
		/* prepare packet for muxing */
		enc_pkt.stream_index = stream_index;
		
		enc_pkt.dts = av_rescale_q_rnd(enc_pkt.dts,out_fmt_ctx->streams[stream_index]->codec->time_base,
			out_fmt_ctx->streams[stream_index]->time_base,(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
		
		enc_pkt.pts = av_rescale_q_rnd(enc_pkt.pts,out_fmt_ctx->streams[stream_index]->codec->time_base,
			out_fmt_ctx->streams[stream_index]->time_base,(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
		
		enc_pkt.duration = av_rescale_q(enc_pkt.duration,out_fmt_ctx->streams[stream_index]->codec->time_base,
			out_fmt_ctx->streams[stream_index]->time_base);
		
		/* mux encoded frame */
		ret = av_interleaved_write_frame(out_fmt_ctx, &enc_pkt);
		
		av_free_packet(&enc_pkt);
		
		if (ret < 0)
		{
			break;
		}
		i++;
	}
	
	printf("flusher write %d frame", i);
	
	return ret;
}

int _tmain(int argc, _TCHAR* argv[])
{
	AVFrame *frame, *frame_out;
	AVPacket pkt_in, pkt_out;
	int ret;
	
	if (argc < 3)
	{
		printf("error in input param");
		getchar();
		return -1;
	}
	
	av_register_all();
	avfilter_register_all();
	
	//input
	if (avformat_open_input(&in_fmt_ctx, argv[1], NULL, NULL) < 0)
	{
		printf("can not open input file context");
		goto end;
	}
	
	if (avformat_find_stream_info(in_fmt_ctx, NULL) < 0)
	{
		printf("can not find input stream info!\n");
		goto end;
	}
	
	//output
	avformat_alloc_output_context2(&out_fmt_ctx, NULL, NULL, argv[2]);
	if (!out_fmt_ctx)
	{
		printf("can not alloc output context!\n");
		goto end;
	}
	
	//open decoder & new out stream & open encoder
	for (int i = 0; i < in_fmt_ctx->nb_streams; i++)
	{
		if (in_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
		{
			//open decoder
			if(0 > avcodec_open2(in_fmt_ctx->streams[i]->codec, avcodec_find_decoder(in_fmt_ctx->streams[i]->codec->codec_id), NULL))
			{
				printf("can not find or open decoder!\n");
				goto end;
			}
			audio_index = i;
			
			//new stream
			out_stream = avformat_new_stream(out_fmt_ctx, NULL);
			if (!out_stream)
			{
				printf("can not new stream for output!\n");
				goto end;
			}
			//set codec context param
			//use default audio encoder
			out_stream->codec->codec = avcodec_find_encoder(out_fmt_ctx->oformat->audio_codec);
			//use the input audio encoder
			//
			out_stream->codec->codec = avcodec_find_encoder(in_fmt_ctx->streams[i]->codec->codec_id);
			out_stream->codec->sample_rate = in_fmt_ctx->streams[i]->codec->sample_rate;
			out_stream->codec->channel_layout = in_fmt_ctx->streams[i]->codec->channel_layout;
			out_stream->codec->channels = av_get_channel_layout_nb_channels(out_stream->codec->channel_layout);
			
			// take first format from list of supported formats
			out_stream->codec->sample_fmt = out_stream->codec->codec->sample_fmts[0];
			AVRational time_base={1, out_stream->codec->sample_rate};
			out_stream->codec->time_base = time_base;
			
			//open encoder
			if (!out_stream->codec->codec)
			{
				printf("can not find the encoder!\n");
				goto end;
			}
			if ((avcodec_open2(out_stream->codec, out_stream->codec->codec, NULL)) < 0)
			{
				printf("can not open the encoder\n");
				goto end;
			}
			if (out_fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
				out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
			break;
		}
	}
	//dump input info
	av_dump_format(in_fmt_ctx, 0, argv[1], 0);
	//dump output info
	av_dump_format(out_fmt_ctx, 0, argv[2], 1);
	if (-1 == audio_index)
	{
		printf("found no audio stream in input file!\n");
		goto end;
	}
	if (!(out_fmt_ctx->oformat->flags & AVFMT_NOFILE))
	{
		if(avio_open(&out_fmt_ctx->pb, argv[2], AVIO_FLAG_WRITE) < 0)
		{
			printf("can not open output file handle!\n");
			goto end;
		}
	}
	
	if(avformat_write_header(out_fmt_ctx, NULL) < 0)
	{
		printf("can not write the header of the output file!\n");
		goto end;
	}
	//
	initSwr();
	int got_frame, got_picture;
	int frame_index = 0;
	int i = 0;
	
	for(int i = 0; ;i++)
	{
		pkt_in.data = NULL;
		pkt_in.size = 0;
		got_frame = -1;
		got_picture = -1;
		if (av_read_frame(in_fmt_ctx, &pkt_in) < 0)
		{
			break;
		}

		if (pkt_in.stream_index != audio_index)
		{
			continue;
		}
		
		frame = av_frame_alloc();
		
		if ((ret = avcodec_decode_audio4(in_fmt_ctx->streams[audio_index]->codec, frame, &got_frame, &pkt_in)) < 0)
		{
			av_frame_free(&frame);
			printf("can not decoder a frame");
			break;
		}
		
		av_free_packet(&pkt_in);
		
		if (got_frame)
		{
			frame->pts = av_frame_get_best_effort_timestamp(frame);
			frame_out = av_frame_alloc();
			if (0 != TransSample(frame, frame_out))
			{
				printf("can not swr the audio data!\n");
				break;
			}
			
			av_init_packet(&pkt_out);
			int ret = avcodec_encode_audio2(out_fmt_ctx->streams[0]->codec, &pkt_out, frame_out, &got_picture);
			
			av_free(frame_out->data[0]);
			av_frame_free(&frame_out);
			
			if (got_picture ) 
			{
				pkt_out.stream_index = out_stream->index;
				pkt_out.dts = av_rescale_q_rnd(pkt_out.dts,
					out_fmt_ctx->streams[out_stream->index]->codec->time_base,
					out_fmt_ctx->streams[out_stream->index]->time_base,
					(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
				pkt_out.pts = av_rescale_q_rnd(pkt_out.pts,
					out_fmt_ctx->streams[out_stream->index]->codec->time_base,
					out_fmt_ctx->streams[out_stream->index]->time_base,
					(AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
				pkt_out.duration = av_rescale_q(pkt_out.duration,
					out_fmt_ctx->streams[out_stream->index]->codec->time_base,
					out_fmt_ctx->streams[out_stream->index]->time_base);
				
				av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
				
				/* mux encoded frame */
				ret = av_interleaved_write_frame(out_fmt_ctx, &pkt_out);
				av_free_packet(&pkt_out);
				
				if (ret < 0)
				{
					printf("write a frame failed!\n");
					break;
				}
				
				printf("success write a frame:index %d\n", frame_index++);
			}
		}
	}

	ret = flush_encoder(out_fmt_ctx, out_stream->index);
	if (ret < 0)
	{
		printf("Flushing encoder failed");
		return -1;
	}
	
	//write file trailer	
	av_write_trailer(out_fmt_ctx);
	
	//clean
	avcodec_close(out_stream->codec);
	avcodec_close(in_fmt_ctx->streams[audio_index]->codec);

end:
	avformat_close_input(&in_fmt_ctx);
	if (out_fmt_ctx && !(out_fmt_ctx->oformat->flags & AVFMT_NOFILE))
	{
		avio_close(out_fmt_ctx->pb);
	}
	avformat_free_context(out_fmt_ctx);
	getchar();
	return 0;
}



相關推薦

ffmpeg音訊採用swr_convert取樣

/* *最簡單的音訊轉碼器(只處理音訊) *繆國凱 Mickel *[email protected] *本程式實現從一個視訊格式轉碼到另一個視訊格式,只處理音訊,視訊忽略,若有多個音訊流,只處理第一個,採用swr_convert重取樣 *2015-5-8 */ #include "stdaf

FFmpeg音訊總結

最近自己在做音訊轉碼的事情。剛開始是想通過ffmpeg的示例程式來學習,可是比較複雜,短時間內看的頭昏腦脹。索性在網上搜索關於轉碼的例子。還是在雷神的部落格裡找到了類似的示例程式,地址是:http://blog.csdn.net/leixiaohua1020/article

C# 使用 ffmpeg 進行音訊

先放一下 ffmpeg 的官方文件以及下載地址: 官方文件:http://ffmpeg.org/ffmpeg.html 下載地址:http://ffmpeg.org/download.html 用 ffmpeg 進行轉碼很簡單,全部都用預設引數的話用下面這句就行: ff

瀏覽器音訊相容和ffmpeg音訊使用

1、百度搜索瀏覽器對於音訊檔案的相容,排在前面的文章大部分是複製貼上很久以前的文章,容易誤導搜尋資料的人, 因此重新驗證整理下。   以Firefox瀏覽器為例,Firefox對於mp3格式音訊的支援在釋出版本21時就已經支援了(2013年)。 下載Firefox各個版本,然後在audio標籤上引入mp3

CentOS7.4 yum和原始碼安裝ffmpeg 將amr格式音訊為mp3

原始碼安裝: 安裝依賴: # yum install nasm yasm 安裝依賴: # wget https://nchc.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz # tar -

FFmpeg簡單程序--視頻剪輯

read all 解碼 num avs key iba yuv start 學習了雷神的文章,慕斯人分享精神,感其英年而逝,不勝唏噓。他有分享一個轉碼程序《最簡單的基於FFMPEG的轉碼程序》其中使用了filter(參考了ffmpeg.c中的流程),他曾說想再編寫一個

javaweb中ffmpeg視訊h264出現卡住不執行的解決辦法(看到最後面就是答案了)

鄭文亮 專心地鑽研程式設計,每天進步一點點 部落格園 首頁 新隨筆 聯絡 管理 隨筆-1610  文章-3  評論-5

docker部署hadoop+ffmpeg分散式系統

本系統應用程式部分是基於此專案,也非常感謝這位大神修復了專案中的部分bug。 公司最近需要做一個分散式轉碼系統,百度之後發現使用hadoop+ffmpeg的人居多,因此決定使用上面提到的專案實現分散式轉碼的功能。該專案包含兩個程式:TranscoderClient和Tran

java視訊格式設定編碼格式

專案上傳後的視訊不能在video標籤裡面播放,視訊格式是MP4。重新編碼之後就可以了 package com.video.util; import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.FFmpe

Android FFmpeg視訊並儲存到本地

本文講述在Android中, 如何將FFmpeg視訊轉碼為YUV格式並儲存到本地。 配置好之後,我們先來明確下概念,FFmpeg解碼的流程是固定的 av_register_all: 註冊所有元件 avformat_open_input : 開啟輸入視訊檔案

最簡單的基於FFMPEG程式

                本文介紹一個簡單的基於FFmpeg的轉碼器。它可以將一種視訊格式(包括封轉格式和編碼格式)轉換為另一種視訊格式。轉碼器在視音訊編解碼處理的程式中,屬於一個比較複雜的東西。因為它結合了視訊的解碼和編碼。一個視訊播放器,一般只包含解碼功能;一個視訊編碼工具,一般只包含編碼功能;而一

C語言的url提交內容到網站

2011-09-07 url提交資訊為何要轉碼呢?因為url本身會有很多特殊字元。而提交的引數中如果再有特殊字元的話,url就不能區分哪些是引數內容,哪些是分隔符。尤其是unicode,gb18030,big5等多位元組的編碼,

【原創】音訊-Mp3AMR--轉載請註明出處

一、常見音訊轉碼方案 1.採用阿里雲OSS進行轉碼 採用阿里雲OSS僅支援amr -> mp3格式的轉碼,但是不支援mp3 -> amr 格式的轉碼,呼叫後會丟擲 amr 格式不支援的異常。OSS轉碼為非同步轉碼,呼叫時是通知OSS的轉碼服務,將

CUDA學習筆記一:CUDA+OpenCV的影象採用Shared Memory進行CUDA程式優化

原創文章,轉載請註明出處...... 一、問題背景         最近要做一個關於CUDA的學習分享報告,想在報告中舉一個利用CUDA進行影象處理的例子,並使用Shared Memory避免Global Memory不合並訪存情況,提高影象處理效能。但是對於CUDA程式如

騰訊視訊把qlv格式轉換成mp4格式

     最近需要在公眾號上傳視訊,但是大於20M的視訊需要到騰訊視訊上傳,在騰訊視訊上下載的視訊是qlv格式,要在騰訊視訊上傳必須是其他主流視訊格式,如mp4這樣的,真是坑。所以就需要我把視訊轉換格式,但是在格式工廠等都不能轉,而有個迅捷視訊轉換非VIP使用者只有一次轉換同

ffmpeg 視訊例子集合

http://rodrigopolo.com/ffmpeg/cheats.php vfilter 加水印 ffmpeg -i d:/l.mpg -vf "movie=720576.png [watermark]; [in][watermark] overlay=10:10 [out]"  -b 3500k

iOS 音訊 使用lame轉為MP3格式

由於AVAudioRecorder不能錄製編碼為MP3,所以就需要我們將錄音後的音訊檔案格式進行轉換(注意:AV Foundation和Core Audio提供對MP3資料解碼的支援,但是不提供對其進行編碼。所以我們要藉助第三方庫進行MP3編碼)。如何轉換?lame無疑是一個很好的選擇,

使用VLC在HTML5頁面播放實時監控

首先要獲取攝像機品牌的RTSP地址: 如大華的是      rtsp://user:[email protected]:port/cam/ realmonitor?channel=1&subtype=0 海康的是     rtsp://user:[ema

ffmpeg 音訊amr

其編譯方法; chmod 755 opencore-amr-0.1.3.tar.gz //改變檔案操作許可權 tar -xzvf opencore-amr-0.1.3.tar.gz //解壓檔案 cd openco

海思arm平臺AAC音訊cpu佔用高、效率低的問題解決

問題背景 目前市面上的大部分IPC攝像機音訊輸出基本都是G711、G726編碼格式,而在類似於《基於EasyNVR實現RTSP/Onvif監控攝像頭Web無外掛化直播監控》這種業務中,都是在類似於EasyNVR這樣的適配服務中進行音訊的轉碼,也有在類似於《Ea