您的位置:首页 > 其它

ffmpeg分离ts为h264、aac

2015-03-18 20:08 302 查看
把雷神的例子直接拿来的

DeMuxerAndroid.c

#include <jni.h>
#include <string.h>
#include <stdio.h>
#include <android/log.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/frame.h>
#include <libavutil/pixfmt.h>

#define  LOG_TAG    "FFMPEGSample"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

#include "libavformat/avformat.h"

//'1': Use H.264 Bitstream Filter
#define USE_H264BSF 1

JNIEXPORT jint JNICALL Java_com_example_ffmpegdemuxer_DeMuxerAndroid_Demuxering(JNIEnv * env, jclass obj)
{
	***FormatContext *ifmt_ctx = NULL;
	***Packet pkt;
	int ret, i;
	int videoindex=-1,audioindex=-1;
	char *in_filename  = "/sdcard/DeMuxer/test.ts";//输入文件名(Input file URL)
	char *out_filename_v = "/sdcard/DeMuxer/schoool.264";//输出文件名(Output file URL)
	char *out_filename_a = "/sdcard/DeMuxer/haiou.aac";

	av_register_all();
	//输入(Input)
	if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
		LOGI( "Could not open input file.");
		return -1;
	}
	if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
		LOGI( "Failed to retrieve input stream information");
		return -1;
	}

	videoindex=-1;
	for(i=0; i<ifmt_ctx->nb_streams; i++) {
		if(ifmt_ctx->streams[i]->codec->codec_type==***MEDIA_TYPE_VIDEO){
			videoindex=i;
		}else if(ifmt_ctx->streams[i]->codec->codec_type==***MEDIA_TYPE_AUDIO){
			audioindex=i;
		}
	}
	//Dump Format------------------
	LOGI("\nInput Video===========================\n");
	av_dump_format(ifmt_ctx, 0, in_filename, 0);
	LOGI("\n======================================\n");

	FILE *fp_audio=fopen(out_filename_a,"wb+");
	FILE *fp_video=fopen(out_filename_v,"wb+");

	/*
	FIX: H.264 in some container format (FLV, MP4, MKV etc.) need
	"h264_mp4toannexb" bitstream filter (BSF)
	  *Add SPS,PPS in front of IDR frame
	  *Add start code ("0,0,0,1") in front of NALU
	H.264 in some container (MPEG2TS) don't need this BSF.
	*/
#if USE_H264BSF
	***BitStreamFilterContext* h264bsfc =  av_bitstream_filter_init("h264_mp4toannexb");
#endif

	while(av_read_frame(ifmt_ctx, &pkt)>=0){
		if(pkt.stream_index==videoindex){
#if USE_H264BSF
			av_bitstream_filter_filter(h264bsfc, ifmt_ctx->streams[videoindex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
#endif
			LOGI("Write Video Packet. size:%d",pkt.size);
			LOGI("Write Video Packet.pts---->%d",pkt.pts);
			fwrite(pkt.data,1,pkt.size,fp_video);
		}else if(pkt.stream_index==audioindex){
			/*
			AAC in some container format (FLV, MP4, MKV etc.) need to add 7 Bytes
			ADTS Header in front of ***Packet data manually.
			Other Audio Codec (MP3...) works well.
			*/
			LOGI("Write Audio Packet. size:%d",pkt.size);
			LOGI("Write Audio Packet.pts--->%d",pkt.pts);
			fwrite(pkt.data,1,pkt.size,fp_audio);
		}
		av_free_packet(&pkt);
	}

#if USE_H264BSF
	av_bitstream_filter_close(h264bsfc);
#endif

	fclose(fp_video);
	fclose(fp_audio);

	avformat_close_input(&ifmt_ctx);

	if (ret < 0 && ret != ***ERROR_EOF) {
		LOGI( "Error occurred.");
		return -1;
	}
	return 0;
}


Android.mk参照复用为ts的mk文件,其他的和复用ts的一样
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: