FFmpeg编写的代码
//初始化解封装
av_register_all();
avformat_network_init();
avcodec_register_all();
//封装文件的上下文
AVFormatContext *ic = NULL;
char path[] = "sdcard/shape.mp4";
//打开文件并且解析
int re = avformat_open_input(&ic, path, NULL, NULL);
if(re != 0)
{
Logw("avformat_open_input %s failed", av_err2str(re));
return env->NewStringUTF(hello.c_str());
}
Logw("duration = %lld nb_stream = %d", ic->duration , ic->nb_streams);
//找到流的信息
int re1 = avformat_find_stream_info(ic, NULL);
if(re1!=0)
{
Logw("avformat_find_stream_info failed");
}
Logw("duration = %lld nb_stream = %d" , ic->duration , ic->nb_streams);
//找流的信息有两种方案
//1.遍历整个信息
int fps = 0 ;
int wigth = 0;
int height = 0;
int videoStream = 0;
int audioStream = 0;
for (int i = 0; i < ic->nb_streams; i++)
{
AVStream *as = ic->streams[i];
if(as->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
Logw("视频数据");
videoStream = i;
fps = r2d(as->avg_frame_rate);
Logw("fps = %d width = %d height = %d codeid = %d" , fps , as->codecpar->width ,
as->codecpar->height , as->codecpar->codec_id);
Logw("tag = %d format = %d" , as->codecpar->codec_tag , as->codecpar->format);
}
else if(as->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
{
Logw("音频数据");
audioStream = i;
Logw("sample_rate = %d channel = %d format = %d" , as->codecpar->sample_rate ,
as->codecpar->channels , as->codecpar->format);
}
}
//av_find_best_stream()来找到对应的流
audioStream = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
Logw("av_find_best_stream audioStream = %d " , audioStream);
Logw("sample_rate = %d channel = %d format = %d" , ic->streams[audioStream]->codecpar->sample_rate ,
ic->streams[audioStream]->codecpar->channels , ic->streams[audioStream]->codecpar->format);
/*****************打开视频解码器*********************************/
//找到软解码器
AVCodec *codec = avcodec_find_decoder(ic->streams[videoStream]->codecpar->codec_id);
//找到硬解码器
codec = avcodec_find_decoder_by_name("h264_mediacodec");
//解码器初始化
AVCodecContext *vc = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(vc, ic->streams[videoStream]->codecpar);
vc->thread_count = 4;
//打开解码器
re = avcodec_open2(vc, 0, 0);
if(re != 0)
{
Logw("avcodec open failed");
return env->NewStringUTF(hello.c_str());
}
/*****************打开音频解码器*********************************/
AVCodec *avCodec = avcodec_find_decoder(ic->streams[audioStream]->codecpar->codec_id);
//初始化软解码器
AVCodecContext *av = avcodec_alloc_context3(avCodec);
avcodec_parameters_to_context(av, ic->streams[audioStream]->codecpar);
av->thread_count = 4;
//打开音视频解码器
re = avcodec_open2(av, 0, 0);
if(re != 0)
{
Logw("avcodec open failed");
return env->NewStringUTF(hello.c_str());
}
//读取帧数据
AVPacket *pkt = av_packet_alloc();
AVFrame *frame = av_frame_alloc();
long long start = GetNowMs();
int frameCount = 0;
for(;;)
{
//时间超过3秒
if(GetNowMs() - start >= 3000)
{
Logw("now decodec fps is %d", frameCount / 3);
start = GetNowMs();
frameCount = 0;
}
int re = av_read_frame(ic, pkt);
if(re != 0)
{
Logw("读取到结尾处");
// int pos = 10 * r2d(ic->streams[videoStream]->time_base);
// av_seek_frame(ic, videoStream, pos,
// AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
break;
}
AVCodecContext *cc = vc;
if(pkt->stream_index == audioStream) {
cc = av;
}
//把数据发送到数据缓冲空间去
re = avcodec_send_packet(cc, pkt);
av_packet_unref(pkt);
if(re != 0)
{
Logw("avcodec_send_packet failed");
continue;
}
for(;;)
{
re = avcodec_receive_frame(cc, frame);
if(re != 0)
{
//Logw("avcodec_receive_frame failed");
break;
}
//Logw("avcodec_receive_frame %lld " , frame->pts);
//如果是视频帧
if(cc == vc)
{
frameCount++;
}
}
}
avformat_close_input(&ic);
return env->NewStringUTF(hello.c_str());
}
FFmpeg编写的代码的更多相关文章
- [转]通过Visual Studio为Linux编写C++代码
Build 2016大会上Microsoft首次公布的Visual Studio 2015扩展提供了在VS2015中编写C++代码,随后通过Linux/UNIX计算机进行编译和执行的能力.这种想法非常 ...
- 基于CkEditor实现.net在线开发之路(2)编写C#代码,怎么调用它。
上一章简约的介绍了CkEditor编辑器,可以编辑js逻辑代码,css,html,C#代码,这章我根据实际例子,讲解怎么编写C#代码和怎么调用它. 大家都还记得刚刚接触程序编时的hello Word吧 ...
- 解决VS2012编写JQuery代码不能智能提示的问题(其他js库的代码提示设置估计类似)
VS默认设置下编写jQuery代码是这样的: 解决办法: 1.在项目的"管理NuGet程序包"中安装JQuery: 2.打开:工具 -> 选项 -> 文本编辑器 -&g ...
- JNI技术基础(2)——从零开始编写JNI代码
书接上文: <JNI技术基础(1)——从零开始编写JNI代码> 2.编译源程序HelloWorld.java并生成HelloWorld.class 3.生成头文件HelloWorld.h ...
- 最新的JavaScript核心语言标准——ES6,彻底改变你编写JS代码的方式!【转载+整理】
原文地址 本文内容 ECMAScript 发生了什么变化? 新标准 版本号6 兑现承诺 迭代器和for-of循环 生成器 Generators 模板字符串 不定参数和默认参数 解构 Destructu ...
- AppleWatch开发教程之Watch应用对象新增内容介绍以及编写运行代码
AppleWatch开发教程之Watch应用对象新增内容介绍以及编写运行代码 添加Watch应用对象时新增内容介绍 Watch应用对象添加到创建的项目中后,会包含两个部分:Watch App 和 Wa ...
- mvn编写主代码与测试代码
maven编写主代码与测试代码 3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目 ...
- [JS进阶] 编写可维护性代码 (1)
今天的web应用大至成千上万行的javascript代码,执行各种复杂的过程,这种演化让我们开发者必须得对可维护性有一定的认识!编写可维护性代码很重要,很多情况下我们是以他人的工作成果为基础,确保代码 ...
- Java认证:JavaRunnable线程编写接口代码
Java认证:JavaRunnable线程编写接口代码.JavaRunnable线程如何才能更好的适应目前的编程环境呢?下面我们就看看如何才能更好的进行相关环境.希望下面的文章对大家有所帮助.Java ...
随机推荐
- PAT 1072 开学寄语(20)(代码+思路)
1072 开学寄语(20 分) 下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其 QQ,封其电脑,夺其手机,收其 ipad,断其 wifi,使其百无聊赖,然后,净面.理发.整衣, ...
- 设计神器 - 摹客设计系统上线了 | 晒出你的设计规范,赢iPad Pro!
在国内,设计规范也许还是个不太常用的概念,但是如果你正好有参与互联网公司的产品设计,你应该早就已经体会到设计规范的重要性了.UI设计师总是要花费大量的时间和精力向开发描述一大堆设计细节,但是产品最后呈 ...
- 微信分享 apicloud方式 中遇到的坎
1 appid填错了. 2 图片地址没写对,可能因为拼写问题,图片不存在 3 图片大小有限制.不能太大.
- NSNotificationCenter 注意
成对出现 意思很简单,NSNotificationCenter消息的接受线程是基于发送消息的线程的.也就是同步的,因此,有时候,你发送的消息可能不在主线程,而大家都知道操作UI必须在主线程,不然会出现 ...
- java利用递归实现汉诺塔算法
package 汉诺塔; //引入Scanner包,用于用户输入 import java.util.Scanner; public class 汉诺塔算法 { public static void m ...
- swiper3d横向滚动多张炫酷切换banner
最近有了个新需求,swiper3d横向滚动多张炫酷切换banner要和elementUI里边走马灯的卡片化card 类似,但是还需要h5手机触摸滚动啊啊啊啊,昨天折腾了半个早上总算完成,今天乖乖跑来m ...
- 2018.09.05 任务安排(斜率优化dp)
描述 这道题目说的是,给出了n项必须按照顺序完成的任务,每项任务有它需要占用机器的时间和价值.现在我们有一台机器可以使用,它每次可以完成一批任务,完成这批任务所需的时间为一个启动机器的时间S加上所有任 ...
- Django入门指南-第8章:第一个测试用例(完结)
python manage.py test python manage.py test --verbosity=2 # boards/tests.py from django.core.urlreso ...
- mysql CONCAT用法
1.全表查询 SELECT * FROM `wh_statistics_service_api_request`; 由于上面时间是按year,month,day三个数值字段来存时间的,现在想通过时间段 ...
- sql语句增删改查(方便你我Ta)
又自学,把SQL的一些常用语句复习了一遍. 整理如下: 1增 1.1[插入单行]insert [into] <表名> (列名) values (列值)例:insert into Strde ...