百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

视频多线程解码 视频多线程解码软件

csdh11 2024-12-23 09:25 25 浏览

最近在做视频解码相关的工作,有一个功能需要将整个视频解码之后放到内存里面,经测试,一分钟的视频解码需要20s,不算太理想,考虑用多线程来实现。

基本思路是获取所有关键帧信息,然后开启不同的线程来从不同的关键帧开始解码。

1,获取所有关键帧信息,获取所有的关键帧时间戳,大约会花费0.2s:

+ (NSMutableArray *)keyFramePtsWithM3u8Path:(NSString *)path{
 NSMutableArray *timestamp_array = [[NSMutableArray alloc] init];
 canceled = false;
 AVFormatContext *qFormatCtx = NULL;
 AVCodecContext *qCodeCtx = NULL;
 AVCodec *qCodec = NULL;
 AVPacket qPacket;
 struct SwsContext *qSwsCtx;
 const char *filePath = [path UTF8String];
 if (filePath == nil) {
 return nil;
 }
 av_register_all();
 if (avformat_open_input(&qFormatCtx, filePath, NULL, NULL) != 0) {
 return nil;
 }
 if (avformat_find_stream_info(qFormatCtx, NULL) < 0) {
 return nil;
 }
 av_dump_format(qFormatCtx, 0, filePath, 0);
 int videoStream = -1;
 for (int i = 0;i < qFormatCtx->nb_streams;i++) {
 if (qFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
 videoStream = i;
 break;
 }
 }
 if (videoStream == -1) {
 return nil;
 }
 qCodeCtx = avcodec_alloc_context3(NULL);
 if (avcodec_parameters_to_context(qCodeCtx, qFormatCtx->streams[videoStream]->codecpar) < 0) {
 return nil;
 }
 qCodec = avcodec_find_decoder(qCodeCtx->codec_id);
 if (qCodeCtx == NULL) {
 return nil;
 }
 if (avcodec_open2(qCodeCtx, qCodec, NULL) < 0) {
 return nil;
 }
 qSwsCtx = sws_getContext(qCodeCtx->width,
 qCodeCtx->height,
 qCodeCtx->pix_fmt,
 qCodeCtx->width,
 qCodeCtx->height,
 AV_PIX_FMT_RGBA,
 SWS_BICUBIC, NULL, NULL, NULL);
 while (av_read_frame(qFormatCtx, &qPacket) >= 0) {
 if (qPacket.stream_index == videoStream) {
 if(canceled){
 av_packet_unref(&qPacket);
 break;
 }
 if(qPacket.flags==1){
 [timestamp_array addObject:[NSString stringWithFormat:@"%lld",qPacket.pts]];
 }
 }
 av_packet_unref(&qPacket);
 }
 sws_freeContext(qSwsCtx);
 avcodec_close(qCodeCtx);
 avformat_close_input(&qFormatCtx);
 return timestamp_array;
}

2,根据关键帧信息开启多个队列(这里为每5个关键帧开启一个队列,可以适当调整):

- (void)parseVideoToImagesMultiThreadWithM3u8Path:(NSString *)path andError:(NSError *__autoreleasing *)error{
 NSMutableArray *timestamps = [Decoder keyFramePtsWithM3u8Path:path andError:nil];
 int i=0;
 int gap = 5;//每五个关键帧开启一个队列
 __block long thread_count = ceil([timestamps count]/(float)gap);
 __block long complete_count = 0;
 __block NSMutableDictionary *imgs = [[NSMutableDictionary alloc] init];
 UInt64 t = [[NSDate date] timeIntervalSince1970]* 1000;
 for(NSString *time in timestamps){
 if(i%gap==0){
 NSString *start = time;
 NSString *end = (i+gap)<=([timestamps count]-1)?timestamps[i+gap]:@"1000000000";
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 [Decoder decodeWithM3u8Path:path andStart:start andEnd:end andBlock:^(unsigned char *buffer, int width, int height, NSString *timestamp) {
 const size_t bufferLength = width * height * 3;
 NSData *data = [NSData dataWithBytes:buffer length:bufferLength];
 [imgs setObject:data forKey:timestamp];
 } andError:nil];
 complete_count++;
 if(complete_count==thread_count){
 if ([self.delegate respondsToSelector:@selector(finished:)]) {
 [self.delegate finished:imgs];
 }
 NSLog(@"Cutter---:%fms",[[NSDate date] timeIntervalSince1970]* 1000 - t);
 }
 });
 }
 i++;
 }
}

3,那么我们就需要一个有开始和结束时间戳的方法来实现区间解码:

+ (void)decodeWithM3u8Path:(NSString *)path andStart:(NSString *)start andEnd:(NSString *)end andBlock:(void (^)(unsigned char *, int, int, NSString *))block andError:(NSError *__autoreleasing *)error{
 canceled = false;
 AVFormatContext *qFormatCtx = NULL;
 AVCodecContext *qCodeCtx = NULL;
 AVCodec *qCodec = NULL;
 AVPacket qPacket;
 AVFrame *qFrame = NULL;
 AVFrame *qFrameRGB = NULL;
 struct SwsContext *qSwsCtx;
 uint8_t *buffer = NULL;
 int64_t start_timestamp = [start longLongValue];
 int64_t end_timestamp = [end longLongValue];
 const char *filePath = [path UTF8String];
 if (filePath == nil) {
 return;
 }
 av_register_all();
 if (avformat_open_input(&qFormatCtx, filePath, NULL, NULL) != 0) {
 return;
 }
 if (avformat_find_stream_info(qFormatCtx, NULL) < 0) {
 return;
 }
 av_dump_format(qFormatCtx, 0, filePath, 0);
 int videoStream = -1;
 for (int i = 0;i < qFormatCtx->nb_streams;i++) {
 if (qFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
 videoStream = i;
 break;
 }
 }
 if (videoStream == -1) {
 return;
 }
 qCodeCtx = avcodec_alloc_context3(NULL);
 if (avcodec_parameters_to_context(qCodeCtx, qFormatCtx->streams[videoStream]->codecpar) < 0) {
 return;
 }
 qCodec = avcodec_find_decoder(qCodeCtx->codec_id);
 if (qCodeCtx == NULL) {
 return;
 }
 if (avcodec_open2(qCodeCtx, qCodec, NULL) < 0) {
 return;
 }
 qFrame = av_frame_alloc();
 qFrameRGB = av_frame_alloc();
 if (qFrame == NULL || qFrameRGB == NULL) {
 return;
 }
 int heigth = qCodeCtx->height>>2;
 int width = qCodeCtx->width>>2;
 int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24,
 width,
 heigth, 1);
 buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
 av_image_fill_arrays(qFrameRGB->data,
 qFrameRGB->linesize,
 buffer,
 AV_PIX_FMT_RGB24,
 width,
 heigth, 1);
 qSwsCtx = sws_getContext(qCodeCtx->width,
 qCodeCtx->height,
 qCodeCtx->pix_fmt,
 width,
 heigth,
 AV_PIX_FMT_RGB24,
 SWS_FAST_BILINEAR, NULL, NULL, NULL);
 while (av_read_frame(qFormatCtx, &qPacket) >= 0) {
 if (qPacket.stream_index == videoStream) {
 if(canceled){
 av_packet_unref(&qPacket);
 break;
 }
 if(qPacket.pts>=start_timestamp){
 if(qPacket.pts<end_timestamp){
 int ret = avcodec_send_packet(qCodeCtx, &qPacket);
 if (ret < 0) {
 return;
 }
 ret = avcodec_receive_frame(qCodeCtx, qFrame);
 if (ret ==0) {
 sws_scale(qSwsCtx,
 (uint8_t const * const *)qFrame->data,
 qFrame->linesize, 0,
 qCodeCtx->height,
 qFrameRGB->data,
 qFrameRGB->linesize);
 block(buffer,width,heigth,[NSString stringWithFormat:@"%lld",qFrame->pts]);
 }
 }
 }
 }
 av_packet_unref(&qPacket);
 }
 av_free(buffer);
 av_frame_free(&qFrameRGB);
 av_frame_free(&qFrame);
 sws_freeContext(qSwsCtx);
 avcodec_close(qCodeCtx);
 avformat_close_input(&qFormatCtx);
}

这样就可以实现多线程的解码了,由于机器为双核,经测最后多线程解码的时间缩减为11s,还是很可观的。鉴于大部分视频长度都在30s左右,解码时间可以控制在五六秒的范围能,对于用户体验有不少的提升。

或者直接:av_dict_set(&opt, “threads”, “auto”, 0);

也可以实现多线程解码,当然这个是ffmpeg内部实现的。

文章首发于我的博客(yangf.vip)

相关推荐

Github霸榜的SpringBoot全套学习教程,从入门到实战,内容超详细

前言...

SpringBoot+LayUI后台管理系统开发脚手架

源码获取方式:关注,转发之后私信回复【源码】即可免费获取到!项目简介本项目本着避免重复造轮子的原则,建立一套快速开发JavaWEB项目(springboot-mini),能满足大部分后台管理系统基础开...

Spring Boot+Vue全栈开发实战,中文版高清PDF资源

SpringBoot+Vue全栈开发实战,中文高清PDF资源,需要的可以私我:)SpringBoot致力于简化开发配置并为企业级开发提供一系列非业务性功能,而Vue则采用数据驱动视图的方式将程序...

2021年超详细的java学习路线总结—纯干货分享

本文整理了java开发的学习路线和相关的学习资源,非常适合零基础入门java的同学,希望大家在学习的时候,能够节省时间。纯干货,良心推荐!第一阶段:Java基础...

探秘Spring Cache:让Java应用飞起来的秘密武器

探秘SpringCache:让Java应用飞起来的秘密武器在当今快节奏的软件开发环境中,性能优化显得尤为重要。SpringCache作为Spring框架的一部分,为我们提供了强大的缓存管理能力,让...

3,从零开始搭建SSHM开发框架(集成Spring MVC)

目录本专题博客已共享在(这个可能会更新的稍微一些)https://code.csdn.net/yangwei19680827/maven_sshm_blog...

Spring Boot中如何使用缓存?超简单

SpringBoot中的缓存可以减少从数据库重复获取数据或执行昂贵计算的需要,从而显著提高应用程序的性能。SpringBoot提供了与各种缓存提供程序的集成,您可以在应用程序中轻松配置和使用缓...

我敢保证,全网没有再比这更详细的Java知识点总结了,送你啊

接下来你看到的将是全网最详细的Java知识点总结,全文分为三大部分:Java基础、Java框架、Java+云数据小编将为大家仔细讲解每大部分里面的详细知识点,别眨眼,从小白到大佬、零基础到精通,你绝...

1,从零开始搭建SSHM开发框架(环境准备)

目录本专题博客已共享在https://code.csdn.net/yangwei19680827/maven_sshm_blog1,从零开始搭建SSHM开发框架(环境准备)...

做一个适合二次开发的低代码平台,把程序员从curd中解脱出来-1

干程序员也有好长时间了,大多数时间都是在做curd。现在想做一个通用的curd平台直接将我们解放出来;把核心放在业务处理中。用过代码生成器,在数据表设计好之后使用它就可以生成需要的controller...

设计一个高性能Java Web框架(java做网站的框架)

设计一个高性能JavaWeb框架在当今互联网高速发展的时代,构建高性能的JavaWeb框架对于提升用户体验至关重要。本文将从多个角度探讨如何设计这样一个框架,让我们一起进入这段充满挑战和乐趣的旅程...

【推荐】强&amp;牛!一款开源免费的功能强大的代码生成器系统!

今天,给大家推荐一个代码生成器系统项目,这个项目目前收获了5.3KStar,个人觉得不错,值得拿出来和大家分享下。这是我目前见过最好的代码生成器系统项目。功能完整,代码结构清晰。...

Java面试题及答案总结(2025版持续更新)

大家好,我是Java面试分享最近很多小伙伴在忙着找工作,给大家整理了一份非常全面的Java面试场景题及答案。...

Java开发网站架构演变过程-从单体应用到微服务架构详解

Java开发网站架构演变过程,到目前为止,大致分为5个阶段,分别为单体架构、集群架构、分布式架构、SOA架构和微服务架构。下面玄武老师来给大家详细介绍下这5种架构模式的发展背景、各自优缺点以及涉及到的...

本地缓存GuavaCache(一)(guava本地缓存原理)

在并发量、吞吐量越来越大的情况下往往是离不开缓存的,使用缓存能减轻数据库的压力,临时存储数据。根据不同的场景选择不同的缓存,分布式缓存有Redis,Memcached、Tair、EVCache、Aer...