ffmpeg分离ts为h264、aac

来源:互联网 发布:mac outlook 离线文件 编辑:程序博客网 时间:2024/04/30 02:24

把雷神的例子直接拿来的

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 1JNIEXPORT 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 NALUH.264 in some container (MPEG2TS) don't need this BSF.*/#if USE_H264BSFAVBitStreamFilterContext* h264bsfc =  av_bitstream_filter_init("h264_mp4toannexb");#endifwhile(av_read_frame(ifmt_ctx, &pkt)>=0){if(pkt.stream_index==videoindex){#if USE_H264BSFav_bitstream_filter_filter(h264bsfc, ifmt_ctx->streams[videoindex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);#endifLOGI("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 BytesADTS 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_H264BSFav_bitstream_filter_close(h264bsfc);#endiffclose(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的一样

0 0