ffmpeg分离ts为h264、aac
2015-03-18 20:08
302 查看
把雷神的例子直接拿来的
DeMuxerAndroid.c
Android.mk参照复用为ts的mk文件,其他的和复用ts的一样
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的一样
相关文章推荐
- ffmpeg将ts解封装为h264和aac文件测试代码
- ffmpegj将h264与aac封装成mkv/ts或者h264与mp3封装成mp4/flv/avi测试代码
- ffmpeg将h264和aac合成ts,内存输入输出
- TS 解析 h264 aac 保存到文件
- 实现用FFmpeg接收RTSP,把H264视频和AAC音频录制成MP4文件(附完整代码)
- 基于FFMPEG的RTP推流H264和AAC文件
- [置顶] hls直播实现源码V2(h264,aac流输入,ffmpeg实现,MFC,VC环境,ts,m3u8)
- FFMPEG视频h264和音频aac混合编码过程
- Android FFMpeg(三)——使用FFMpeg解码h264、aac
- Ubuntu安装ffmpeg带h264,aac,MP3等功能的版本的官方文档
- 使用ffmpeg将mp4文件中的h264码流转为raw h264文件格式或ts文件格式
- FFmpeg合成h264视频流和aac音频流为mp4
- ffmpeg复用h264、aac为ts流
- FFmpeg合成h264视频流和aac音频流为mp4
- 【FFMPEG】各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式
- 笔记:ffmpeg使用实例(bgr->yuv->h264, pcm->aac,加入音频重采样)
- iOS ffmpeg将音视频写入文件aac+h264
- 笔记:ffmpeg使用实例:bgr->yuv->h264,pcm->aac,没有音频重采样
- 笔记:ffmpeg使用实例:yuv->h264,pcm->aac
- Android使用FFmpeg 解码H264并播放(一)