ffmpeg分离ts为h264、aac

本文介绍了一个基于FFmpeg的Android应用示例,该应用能够从多媒体文件中分离出视频和音频流,并将它们分别保存为独立的文件。文章通过具体的C代码展示了如何打开多媒体文件、查找流信息、遍历各个流并确定视频和音频流的位置,最终实现了音视频的分离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

把雷神的例子直接拿来的

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)
{
	AVFormatContext *ifmt_ctx = NULL;
	AVPacket 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==AVMEDIA_TYPE_VIDEO){
			videoindex=i;
		}else if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_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
	AVBitStreamFilterContext* 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 AVPacket 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 != AVERROR_EOF) {
		LOGI( "Error occurred.");
		return -1;
	}
	return 0;
}


Android.mk参照复用为ts的mk文件,其他的和复用ts的一样
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值