主要使用函数

  • 特征码:Start code

  • 解码的一些视频参数,分辨率和帧率:SPS/PPS

  • ffmpeg获取SPS/PPS:codec->extradata

实例

#include <stdio.h>
#include <libavutil/log.h>
#include <libavformat/avformat.h> #ifndef AV_WB32
# define AV_WB32(p, val) do { \
uint32_t d = (val); \
((uint8_t*)(p))[3] = (d); \
((uint8_t*)(p))[2] = (d)>>8; \
((uint8_t*)(p))[1] = (d)>>16; \
((uint8_t*)(p))[0] = (d)>>24; \
} while(0)
#endif #ifndef AV_RB16
# define AV_RB16(x) \
((((const uint8_t*)(x))[0] << 8) | \
((const uint8_t*)(x))[1])
#endif static int alloc_and_copy(AVPacket *out,
const uint8_t *sps_pps, uint32_t sps_pps_size,
const uint8_t *in, uint32_t in_size)
{
uint32_t offset = out->size;
uint8_t nal_header_size = offset ? 3 : 4;
int err; err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);
if (err < 0)
return err; if (sps_pps)
memcpy(out->data + offset, sps_pps, sps_pps_size);
memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);
if (!offset) {
AV_WB32(out->data + sps_pps_size, 1);
} else {
(out->data + offset + sps_pps_size)[0] =
(out->data + offset + sps_pps_size)[1] = 0;
(out->data + offset + sps_pps_size)[2] = 1;
} return 0;
} int h264_extradata_to_annexb(const uint8_t *codec_extradata, const int codec_extradata_size, AVPacket *out_extradata, int padding)
{
uint16_t unit_size;
uint64_t total_size = 0;
uint8_t *out = NULL, unit_nb, sps_done = 0,
sps_seen = 0, pps_seen = 0, sps_offset = 0, pps_offset = 0;
const uint8_t *extradata = codec_extradata + 4;
static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size, 用于指示表示编码数据长度所需字节数 sps_offset = pps_offset = -1; /* retrieve sps and pps unit(s) */
unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
if (!unit_nb) {
goto pps;
}else {
sps_offset = 0;
sps_seen = 1;
} while (unit_nb--) {
int err; unit_size = AV_RB16(extradata);
total_size += unit_size + 4;
if (total_size > INT_MAX - padding) {
av_log(NULL, AV_LOG_ERROR,
"Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
av_free(out);
return AVERROR(EINVAL);
}
if (extradata + 2 + unit_size > codec_extradata + codec_extradata_size) {
av_log(NULL, AV_LOG_ERROR, "Packet header is not contained in global extradata, "
"corrupted stream or invalid MP4/AVCC bitstream\n");
av_free(out);
return AVERROR(EINVAL);
}
if ((err = av_reallocp(&out, total_size + padding)) < 0)
return err;
memcpy(out + total_size - unit_size - 4, nalu_header, 4);
memcpy(out + total_size - unit_size, extradata + 2, unit_size);
extradata += 2 + unit_size;
pps:
if (!unit_nb && !sps_done++) {
unit_nb = *extradata++; /* number of pps unit(s) */
if (unit_nb) {
pps_offset = total_size;
pps_seen = 1;
}
}
} if (out)
memset(out + total_size, 0, padding); if (!sps_seen)
av_log(NULL, AV_LOG_WARNING,
"Warning: SPS NALU missing or invalid. "
"The resulting stream may not play.\n"); if (!pps_seen)
av_log(NULL, AV_LOG_WARNING,
"Warning: PPS NALU missing or invalid. "
"The resulting stream may not play.\n"); out_extradata->data = out;
out_extradata->size = total_size; return length_size;
} int h264_mp4toannexb(AVFormatContext *fmt_ctx, AVPacket *in, FILE *dst_fd)
{ AVPacket *out = NULL;
AVPacket spspps_pkt; int len;
uint8_t unit_type;
int32_t nal_size;
uint32_t cumul_size = 0;
const uint8_t *buf;
const uint8_t *buf_end;
int buf_size;
int ret = 0, i; out = av_packet_alloc(); buf = in->data;
buf_size = in->size;
buf_end = in->data + in->size; do {
ret= AVERROR(EINVAL);
if (buf + 4 /*s->length_size*/ > buf_end)
goto fail; for (nal_size = 0, i = 0; i<4/*s->length_size*/; i++)
nal_size = (nal_size << 8) | buf[i]; buf += 4; /*s->length_size;*/
unit_type = *buf & 0x1f;
unit_type = *buf & 0x1f if (nal_size > buf_end - buf || nal_size < 0)
goto fail; /*
if (unit_type == 7)
s->idr_sps_seen = s->new_idr = 1;
else if (unit_type == 8) {
s->idr_pps_seen = s->new_idr = 1;
*/
/* if SPS has not been seen yet, prepend the AVCC one to PPS */
/*
if (!s->idr_sps_seen) {
if (s->sps_offset == -1)
av_log(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");
else {
if ((ret = alloc_and_copy(out,
ctx->par_out->extradata + s->sps_offset,
s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset,
buf, nal_size)) < 0)
goto fail;
s->idr_sps_seen = 1;
goto next_nal;
}
}
}
*/ /* if this is a new IDR picture following an IDR picture, reset the idr flag.
* Just check first_mb_in_slice to be 0 as this is the simplest solution.
* This could be checking idr_pic_id instead, but would complexify the parsing. */
/*
if (!s->new_idr && unit_type == 5 && (buf[1] & 0x80))
s->new_idr = 1; */
/* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
if (/*s->new_idr && */unit_type == 5 /*&& !s->idr_sps_seen && !s->idr_pps_seen*/) { h264_extradata_to_annexb( fmt_ctx->streams[in->stream_index]->codec->extradata,
fmt_ctx->streams[in->stream_index]->codec->extradata_size,
&spspps_pkt,
AV_INPUT_BUFFER_PADDING_SIZE); if ((ret=alloc_and_copy(out,
spspps_pkt.data, spspps_pkt.size,
buf, nal_size)) < 0)
goto fail;
/*s->new_idr = 0;*/
/* if only SPS has been seen, also insert PPS */
}
/*else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen) {
if (s->pps_offset == -1) {
av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
goto fail;
} else if ((ret = alloc_and_copy(out,
ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,
buf, nal_size)) < 0)
goto fail;
}*/ else {
if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
goto fail;
/*
if (!s->new_idr && unit_type == 1) {
s->new_idr = 1;
s->idr_sps_seen = 0;
s->idr_pps_seen = 0;
}
*/
} len = fwrite( out->data, 1, out->size, dst_fd);
if(len != out->size){
av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",
len,
out->size);
}
fflush(dst_fd); next_nal:
buf += nal_size;
cumul_size += nal_size + 4;//s->length_size;
} while (cumul_size < buf_size); /*
ret = av_packet_copy_props(out, in);
if (ret < 0)
goto fail; */
fail:
av_packet_free(&out); return ret;
} int main(int argc, char argv[]){
int err_code;
char errors[1024]; char *src_filename = NULL;
char *dst_filename = NULL; FILE *dst_fd = NULL;
int video_stream_index = -1; // 创建文件上下文,和包管理
AVFormatContext *fmt_ctx = NULL;
AVPacket pkt; // 设置日志等级
av_log_set_level(AV_LOG_DEBUG); // 判断参数个数
if (argc < 3){
av_log(NULL, AV_LOG_DEBUG, "参数不足");
return -1
}
// 输出输出文件
src_filename = argv[1];
dst_filename = argv[2]; // 判断参数不可以为空
if (src_filename == NULL || dst_filename == NULL){
av_log(NULL, AV_LOG_ERROR, "传入的参数不可以为空");
return -1;
} // 全局注册函数
av_register_all(); // 打开目标文件
dst_fd = fopen(dst_filename, "wb");
if (!dst_fd){
av_log(NULL, AV_LOG_DEBUG, "不能打开文件%s", dst_filename);
return -1;
} // 读取流文件
if ((err_code=avformat_open_input(&fmt_ctx, src_filename, NULL, NULL))<0){
// 设置错误信息
av_strerror(err_code, errors, 1024);
// 打印日志输出
av_log(NULL, AV_LOG_DEBUG, "流文件读取失败: %s, %d(%s)\n",
src_filename,
err_code,
errors;);
return -1;
}
av_dump_format(fmt_ctx, 0 , src_filename, 0); // 数据包处理
av_init_packet(&pkt);
pkt.data=NULL;
pkt.size = 0; // 找到最好的流
video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (video_stream_index<0){
av_log(NULL, AV_LOG_DEBUG, "不能找到流输出", av_get_media_type_string(AVMEDIA_TYPE_VIDEO),
src_filename);
return AVERROR(EINVAL);
} // 拿到视频的index,去视频流中去取一个一个包文件
while (av_read_frame(fmt_ctx, &pkt) >= 0){
if(pkt.stream_index == video_stream_index){ // 处理h264处理函数
h264_mp4toannexb(fmt_ctx, &pkt, dst_fd); }
av_packet_unref(&pkt);
} // 关闭流文件
avformat_close_input(&fmt_ctx);
if(dst_fd){
fclose(dst_fd);
} return 0; }

ffmpeg使用C语言sdk实现抽取视频中的视频数据的更多相关文章

  1. 如何使用ffmpeg的c语言sdk实现对文件夹的操作

    重要函数 打开文件夹:avio_open_dir() 读取文件夹:avio_read_dir() 关闭文件夹:avio_close_dir() 结构体, 操作目录的上下文:AVIODirContext ...

  2. FFmpeg调用c语言SDK实现日志的打印

    日志文件的三大步 // 导入头文件 #include <libavutil/log.h> // 设置日志级别 av_log_set_level(AV_LOG_DEBUG); //DEBUG ...

  3. ios 从相册视频中获取视频截图

    //给image添加个分类 +(UIImage *)getImage:(NSURL: *)videoURL { AVURLAsset *asset = [[AVURLAsset alloc] init ...

  4. moviepy音视频剪辑:视频剪辑基类VideoClip的属性及方法详解

    ☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>和<moviepy音视频剪辑:moviepy中的剪辑基类Cl ...

  5. Android中直播视频技术探究之---采集摄像头Camera视频源数据进行推流(采用金山云SDK)

    一.前言 在之前已经详细介绍了Android中的一种视频数据源:Camera,不了解的同学可以点击进入:Android中Camera使用详解 ,在这篇文章中我们介绍了如何采集摄像头的每一帧数据,然后进 ...

  6. 从QQ音乐开发,探讨如何利用腾讯云SDK在直播中加入视频动画

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云发表于云+社区专栏 看着精彩的德甲赛事,突然裁判一声口哨,球赛断掉了,屏幕开始自动播放"吃麦趣鸡盒,看德甲比赛&q ...

  7. ffmpeg用法小结,教你抓各大网站视频

    最近受邀朋友帮忙需要抓取一段某酷电影,偶然间发现ffmpeg程序.说到此可能会有人提到you-get和youtube-dl,期间也接触了该两款程序,但是由于版权原因,该软件仅仅可以抓取前几分钟预览版, ...

  8. php使用ffmpeg向视频中添加文字字幕

    这篇文章主要介绍了PHP使用ffmpeg给视频增加字幕显示的方法,实例分析了php操作ffmpeg给视频增加字母的技巧,具有一定参考借鉴价值,需要的朋友可以参考下. 本文实例讲述了PHP使用ffmpe ...

  9. FFmpeg的使用——PHP转换视频、截取视频以及JW Player播放器控制

    转载:http://blog.csdn.net/zm2714/article/details/7916440 给朋友做的一个项目中,涉及到上传视频.转换视频.自动截取已上传视频内容中的一帧做为缩略图片 ...

随机推荐

  1. 批处理设置IP

    @echo off title 静态IP设置 set netName=本地连接 set address=192.168.1.202 netsh netsh interface ipv4 set dns ...

  2. tensorflow按需分配GPU问题

    使用tensorflow,如果不加设置,即使是很小的模型也会占用整块GPU,造成资源浪费. 所以我们需要设置,使程序按需使用GPU. 具体设置方法: gpu_options = tf.GPUOptio ...

  3. webservice笔记

    Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...

  4. 机器学习 — 从mnist数据集谈起

    做了一些简单机器学习任务后,发现必须要对数据集有足够的了解才能动手做一些事,这是无法避免的,否则可能连在干嘛都不知道,而一些官方例程并不会对数据集做过多解释,你甚至连它长什么样都不知道... 以skl ...

  5. 鸡汤 - Choice is yours

    传送门 https://kamranahmed.info/blog/2018/03/24/choice-is-yours/ Our whole lives are driven by the choi ...

  6. python opencv:摄像头捕获图像

  7. 《创业者技能树》--创业课--14天复盘第一课candy2--HHR计划

    1,厉害的人是如何分析问题的?

  8. P1893山峰瞭望

    传送门 看完这个题,大家都懂意思吧,然后代码呢,emmmmm #include <bits/stdc++.h> using namespace std; const int maxn = ...

  9. Java IO流详解(五)——缓冲流

    缓冲流也叫高效流,是处理流的一种,即是作用在流上的流.其目的就是加快读取和写入数据的速度. 缓冲流本身并没有IO功能,只是在别的流上加上缓冲效果从而提高了效率.当对文件或其他目标频繁读写或操作效率低, ...

  10. ➡️➡️➡️IELTS reading by Simon on Bili

    高分必备 雅思考官Simon手把手教你做阅读 p1 https://www.bilibili.com/video/av40131278?p=2 p2 https://www.bilibili.com/ ...