FFmpeg情景分析 1. FFmpeg转码框架解析

ffmpeg转码视频 转码和编辑视频FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。FFmpeg在Linux平台下开发,但它同样也可以在其它操作系统环境中编译运行,包括Windows、Mac OS X等。 阅读详情

如果说我比别人看得更远些,那是因为我站在巨人的肩膀上。

                                                                                            ---- 牛顿

本文的参考和借鉴:http://dranger.com/ffmpeg/tutorial01.html(文章在开篇就声明,它有些过时了)。所以本文分析的FFmpeg源码为Version0.8.2

我们在网络上看到的“视频”通常都是一种三层数据封装结构:

        编码图像和声音形成的裸视频流(如H.264, VP8, 等)和音频流(mp3, AAC等);

        用容器格式将这些裸流封装形成文件(FLV,rmvb, mkv等)或流(mpeg2-TS等);

        再利用传输协议(如RTSP, RTMP, HTTP, UDP等)将数据通过 互联网,电视线路或无线网络等传输到用户端(电视机,PC,移动设备等)以收看。

FFmpeg的强大与复杂都体现在对这三层封装数据做了大量的支持和处理,从而使它能完成流或文件输入,解码显示,转码存储,封装推送等多项工作。

   闲言少絮,进入正题。

   对于C语言构建的面向过程编程的程序最基本的分析原则是:

           去掉无关,分层推进。

FFmpeg最精简的伪码描述可如下图所示:

简而言之就是,打开文件,读取一帧数据 ,处理一帧数据,如此循环直到文件结束。

这个描述是简单而正确的。我们以转码一个文件来对应实际的代码进行分析。

1.main函数(ffmpeg.c)

int main(int argc, char **argv)

{

    /* *_register_all类函数是注册库中所有有效的文件格式和codec。它们只用在main中调用一次即可*/

    avcodec_register_all();
    av_register_all();               // This registers all file formats and codecs  

   

    /*分析命令行输入参数,进行相关的设置。*/

    init_opts();
    
    /* parse options */
    parse_options(argc, argv, options, opt_output_file);

            

    /*所有的数据解码,处理,编码等都是在这个函数中实现,它的函数参数可以分成三组,输出,输入以及两者间的映射关系。*/

    if (transcode(output_files, nb_output_files, input_files, nb_input_files,
                  stream_maps, nb_stream_maps)
< 0)
        ffmpeg_exit(1);

    return ffmpeg_exit(0);
}

转码结束后的收尾工作。

2.     transcode函数(ffmpeg.c)

/*
 * The following code is the main loop of the file converter
 */
static int transcode(AVFormatContext **output_files,int nb_output_files,
                     InputFile *input_files, int nb_input_files,
                     StreamMap *stream_maps, int nb_stream_maps)
{

    /* 分配输出流的空间 */
    ost_table = av_mallocz(sizeof(OutputStream *) * nb_ostreams);
   //av_mallocz:是ffmpeg对“malloc”的简单封装,它确保了存储地址的对齐,但不保证存储器的内存泄漏,二次释放或其它的问题。

    /* 针对每个输出文件的流,找到对应输入流的索引,以得到正确的流映射 */
    for(k=0;k<nb_output_files;k++)
    {  ... }

    /* 对每个输出流,计算相应的编码参数 */
    for(i=0;i<nb_ostreams;i++)
    {
            if (!ost->enc)
                ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
           
            switch(codec->codec_type)
            {
            case AVMEDIA_TYPE_AUDIO:
                /* 计算音频编码参数 */
                ost->fifo= av_fifo_alloc(1024);
                
                ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE);

                break;
            case AVMEDIA_TYPE_VIDEO:
                /* 计算视频编码参数 */
                choose_pixel_fmt(ost->st, ost->enc);

                break;
            case AVMEDIA_TYPE_SUBTITLE:
                ost->encoding_needed = 1;
                ist->decoding_needed = 1;
                break;
            default:
                abort();
                break;
            }  
    }

    /* 打开每个编码器 open each encoder */
    for(i=0;i<nb_ostreams;i++)
    {
        ost = ost_table[i];
        if (ost->encoding_needed)
        {
            AVCodec *codec = ost->enc;
            AVCodecContext *dec = input_streams[ost->source_index].st->codec;
                   
            /* 打开匹配的编码器,并打印出编码参数信息 */
            if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0)
            {
              ...
            }
            
        }
    }

    /* 找到要解码的流,并打开相应的解码器 */
    for (i = 0; i < nb_input_streams; i++)
    {
        ist = &input_streams[i];
        
        /* 只对要解码的输入流才打开相应的解码器 */
        if (ist->decoding_needed)
        {  
           /* 打开解码器 */
            if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0)
            {
               ...
            }
        }
    }


    /* open files and write file headers */
    for(i=0;i<nb_output_files;i++)
    {
      os = output_files[i];
      if (avformat_write_header(os, &output_opts[i]) < 0)
      {
          ...
      }
    }



    /* 转码主循环 */
    for(; received_sigterm == 0;)
    {
        /* 从输入的文件或流中读取一帧数据 */
        is = input_files[file_index].ctx;
        ret= av_read_frame(is, &pkt);
                             
        /*  处理一帧数据 */
        if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt)< 0)
        {
          ...
        }

    }

    /* at the end of stream, we must flush the decoder buffers */
    for (i = 0; i < nb_input_streams; i++)
    {
        ist = &input_streams[i];
        if (ist->decoding_needed)
        {
            output_packet(ist, i, ost_table, nb_ostreams, NULL);
        }
    }


    /* write the trailer if needed and close file */
    for(i=0;i<nb_output_files;i++)
    {
        os = output_files[i];
        av_write_trailer(os);
    }

    /* close each encoder */
    for(i=0;i<nb_ostreams;i++)
    {
        ost = ost_table[i];
        if (ost->encoding_needed)
        {
            av_freep(&ost->st->codec->stats_in);
            avcodec_close(ost->st->codec);
        }
    }

    /* close each decoder */
    for (i = 0; i < nb_input_streams; i++)
    {
        ist = &input_streams[i];
        if (ist->decoding_needed) {
            avcodec_close(ist->st->codec);
        }
    }

    return ret;
}


3.     output_packet函数(ffmpeg.c)

static int output_packet(InputStream *ist, int ist_index,
                         OutputStream **ost_table, int nb_ostreams,
                         const AVPacket *pkt)
{

    AVFrame picture;
    AVPacket avpkt;

    // 先进行解码
    while (avpkt.size > 0 || (!pkt && got_output))
    {  
        /* decode the packet if needed */
        if (ist->decoding_needed) {
            switch(ist->st->codec->codec_type)
            {
            case AVMEDIA_TYPE_AUDIO:
                ret = avcodec_decode_audio3(ist->st->codec, samples, &decoded_data_size, &avpkt);
                break;
            case AVMEDIA_TYPE_VIDEO:                   
                /* XXX: allocate picture correctly */
                avcodec_get_frame_defaults(&picture);
                ret = avcodec_decode_video2(ist->st->codec, &picture, &got_output, &avpkt);
                break;
            case AVMEDIA_TYPE_SUBTITLE:
                ret = avcodec_decode_subtitle2(ist->st->codec,&subtitle, &got_output, &avpkt);
                break;
            default:
                return -1;
            }
        }

        
        /* 再进行编码 */
        if (start_time == 0 || ist->pts >= start_time)
            for(i=0;i<nb_ostreams;i++)
            {
                if (ost->source_index == ist_index)
                {
                    os = output_files[ost->file_index];

                    if (ost->encoding_needed)
                    {
                        av_assert0(ist->decoding_needed);
                        switch(ost->st->codec->codec_type)
                        {
                        case AVMEDIA_TYPE_AUDIO:
                            do_audio_out(os, ost, ist, decoded_data_buf, decoded_data_size);
                            break;
                        case AVMEDIA_TYPE_VIDEO:

                            do_video_out(os, ost, ist, &picture, &frame_size,same_quality ? quality : ost->st->codec->global_quality);
                            if (vstats_filename && frame_size)
                                do_video_stats(os, ost, frame_size);
                            break;
                        case AVMEDIA_TYPE_SUBTITLE:
                            do_subtitle_out(os, ost, ist, &subtitle,
                                            pkt->pts);
                            break;
                        default:
                            abort();
                       }
                    }
                }
            }

        av_free(buffer_to_free);
    }
    return 0;
}

至此,框架分析结束。






FFmpeg从入门:FFmpeg框架 FFmpeg(Fast forword mpeg,音视频转换器)是一个开源免费跨平台的视频和音频流方案,它提供了录制/音视频编解码、转换以及流化音视频的完整解决方案。ffmpeg4.0.2源码目录结构如下:目录说明:FFmpeg|—compat 该目录存放的是兼容文件,以便兼容早期版本 |—doc 说明文档 |—ffbuild |—libavcodec音视频编解码核心库 |—各种设备的输入输出,比如Video4Linux2, VfW, DShow以及 ALSA |—滤镜特效处理 |— 阅读详情

相关推荐

基于ffmpeg实现音视频转码

使用b站手机客户端下载的视频格式为m4s的两个文件(video.m4s和audio.m4s),需要转成普通播放器支持的mp4格式,得以知道支持音视频转码的ffmpeg这个强大的开源工具。但是,我们可以使用第三方YUM源(Nux Dextop)完成此工作。以上内容只是简单介绍ffmpeg软件基本使用方法,想要集成到公司产品中还需要一定的前端、运维等相关知识等。准备好前端模板,引用video标签进行播放音视频源,前端引freemark源。ffmpeg支持windows、linux和mac,安装简单,使用方便。

yinshipin007的博客 1370

FFmpeg开发之旅(四)---全字幕解码

【写在前面】 在前一篇,我已经讲过了读取外挂字幕并显示的方法:理解过滤图并使用字幕过滤器 但是,全字幕不仅仅是外挂字幕,还有内封字幕和内嵌字幕,因此我们还得考虑其他两种字幕。 不过,对于内嵌字幕,我们根本不需要解码,因为它是直接绘制在视频图像上的。 所以,本篇只需要讲解内封字幕的解码方法,主要内容有: 1、ass 等格式内封字幕解码。 2、sub+idx 格式内封字幕解码。 3、同步...

梦起丶的博客 4174

FFMPEG总结 -- 音视频编解码和转码(全)_ffmpeg音频转码

} /** * FFMPEG视频解码流程 * 1、av_register_all():注册所有组件。 * 2、avformat_open_input():打开输入视频文件。 * 3、avformat_find_stream_info():获取视频文件信息 * 4、avcodec_find_decoder():查找解码器。 * 5、avcodec_open2():打开解码器。 * 6、av_read_frame():从输入文件读取一帧压缩数据。 * 7、avcodec_decode_video2():解码一帧

2401_89694064的博客 1855

FFmpeg4.3系列之26:视频监控之H265多路摄像头播控项目实战

敬告:该系列的课程在抓紧录制更新中,敬请大家关注。敬告:本课程项目仅供学习参考,请不要直接商用,概不负责任何法律责任。 该系列的课程涉及:FFmpeg,WebRTC,SRS,Nginx,Darwin,Live555,等。包括:音视频、流媒体、直播、Android、视频监控28181、等。 我将带领大家一起来学习使用FFmpeg开发视频监控项目,并动手操练。具体内容包括: 一、视频监控的架构和流程二、FFmpeg4.3+SDL2+Qt5开发环境的搭建三、FFmpeg的SDK编程回顾总结并操练四、SDL2.0的编程回顾总结并操练五、颜色空间转换RGB和YUV的原理与实战六、Qt5+FFmpeg本地摄像头采集预览实战七、代码封装:摄像头h264/5编码并存储八、Qt5+FFmpeg单路网络摄像头采集预览九、Qt5+FFmpeg单路网络摄像头采集预览录制会看十、onvif与GB/T-28181的简介  音视频与流媒体是一门很复杂的技术,涉及的概念、原理、理论非常多,很多初学者不学 基础理论,而是直接做项目,往往会看到c/c++的代码时一头雾水,不知道代码到底是什么意思,这是为什么呢?   因为没有学习音视频和流媒体的基础理论,就比如学习英语,不学习基本单词,而是天天听英语新闻,总也听不懂。 所以呢,一定要认真学习基础理论,然后再学习播放器、转码器、非编、流媒体直播、视频监控、等等。   梅老师从事音视频与流媒体行业18年;曾在永新视博、中科大洋、百度、美国Harris广播事业部等公司就职,经验丰富;曾亲手主导广电直播全套项目,精通h.264/h.265/aac,曾亲自参与百度app上的网页播放器等实战产品。  目前全身心自主创业,主要聚焦音视频+流媒体行业,精通音视频加密、流媒体在线转码快编等热门产品。  

FFmpeg编解码处理1-转码全流程简介

看一下 FFmpeg 常规处理流程:正在上传…重新上传取消大流程可以划分为输入、输出、转码、播放四大块。其中转码涉及比较多的处理环节,从图中可以看出,转码功能在整个功能图中占比很大。转码的核心功能在解码和编码两个部分,但在一个可用的示例程序中,编码解码与输入输出是难以分割的。解复用器为解码器提供输入,解码器会输出原始帧,对原始帧可进行各种复杂的滤镜处理,滤镜处理后的帧经编码器生成编码帧,多路流的编码帧经复用器输出到输出文件。转码功能复杂,示例程序很难写得简短,这几篇笔记共用同一份示例代码。

yinshipin007的博客 1032

STM32F407硬件IIC驱动OLED避坑实录:从‘不显示’到‘稳定刷屏’的完整调试过程

本文详细记录了使用STM32F407硬件IIC驱动OLED屏幕的完整调试过程,从初始的‘不显示’问题到最终实现‘稳定刷屏’。文章涵盖了硬件接线、I2C外设配置、常见问题排查及性能优化技巧,特别针对STM32标准库开发中的难点提供了实用解决方案,是嵌入式开发者驱动OLED屏幕的实用指南。

weixin_30315723的博客 303

FFmpeg常见命令行(三):FFmpeg转码

本文是音视频系列blog的其中一个, 对应的要学习的内容是:如何使用FFmpeg命令行进行媒体格式的转码。

分享计算机基础,android,C/C++,音视频等内容 6288

Deepseek+Word插件:一键搞定论文翻译与润色的学术写作新革命

本文介绍了Deepseek Word插件如何革新学术写作流程,实现论文翻译与润色的一键操作。该插件深度集成于Word环境,能智能完成中英互译、语法修正、术语统一及逻辑优化,显著提升写作效率与文本质量,是研究者的高效AI助手。

weixin_29252171的博客 513

【FFmpeg API】avcodec_decode_video2函数简单分析

本博客只是记录自己的学习过程,也许会有错误的地方,请见谅 在目前avcodec_decode_video2函数处于deprecated废弃状态,但是仍然可以使用 /** * Decode the video frame of size avpkt->size from avpkt->data into picture. * Some decoders may support multiple frames in a single AVPacket, such * decoders wo.

qq_46470984的博客 7009

【开关电源】开关电源实战经验分享

开关电源原理设计、Layout设计经验分享

weixin_42098658的博客 4170

App渗透 - 人脸识别登录绕过

一、APP抓包和逆向破解加密算法打开APP是一个登录框抓包后发现参数被加密了使用Jadx脱源码发现,并没有加壳也没有混淆,运气很好根据经验,先搜索Encrypt、Decrypt等关键字,发现在Common.js中有一个encryptData函数定位过去,一套加解密算法都写好了放在这放到浏览器console里面调试,果然没错二、寻找注入点首先测试了一下注入明文:{"userName":"TEST'...

渗透测试研究中心 2826

FFmpeg转码框架解析

FFmpeg情景分析 1. FFmpeg转码框架解析 本文的参考和借鉴:http://dranger.com/ffmpeg/tutorial01.html(文章在开篇就声明,它有些过时了)。所以本文分析的FFmpeg源码为Version0.8.2 我们在网络上看到的“视频”通常都是一种三层数据封装结构:        编码图像和声音形成的裸视频流(如H.264, VP8,

matthew0618band的专栏 797

Arco Design Pro PC端商家后台框架 | Axure高保真原型库

摘要:本Axure原型设计方案基于字节跳动ArcoDesignPro规范,提供企业级后台快速搭建方案。包含动态表格、表单验证等核心组件,采用中继器驱动设计,支持响应式布局。方案覆盖80%后台常用交互,可节省60%原型制作时间,配套完整设计文档和组件库,实现原型到开发的高效衔接。特别适用于电商、SaaS等后台系统开发,支持权限管理、数据分析等场景,显著提升团队协作效率。

招风的黑耳优快云 3975

FFmpeg源码剖析-框架:transcode()

transcode()函数位于ffmpeg.c 1. 函数概述 它是ffmpeg的转码的主函数。 工作模式是每次读取一个packet,并处理。 可见《 FFmpeg情景分析 1. FFmpeg转码框架解析》 2. 函数框架图 ​ 3. 函数伪码解析 static int transcode(void) {     /* 对每个输出流,计算出正确的编码

北雨南萍 1501

智能能效管家:基于 ESP32 的智能插座设计与精细化功耗管理实现

针对传统插座无法远程控制、功耗监测缺失、能源浪费严重等问题,本文设计一款基于 ESP32 主控芯片的智能插座。该系统集成 WiFi 无线通信、交流功耗采集、继电器通断控制模块,实现四大核心功能。硬件采用模块化选型与安全接线设计,软件基于 Arduino-ESP32 框架开发,搭载 Mqtt 通信协议与功耗算法,可通过手机 / 云端实时管控设备,有效提升用电安全性与能源利用率。:ESP32;智能插座;功耗管理;远程控制;能耗监测。

weixin_42300449的博客 101

Skills入门学习路线

简单来说,Skills就像是给Claude准备的”插件包"或”工具箱”。它是一个集合,里面装了特定的操作指令(Instructions)、可执行脚本(Scripts)和相关素材(Resources)。2025 年 10 月中旬,Anthropic 正式发布 Claude Skills。两个月后,Agent Skills 作为开放标准被进一步发布,意在引导一个新的 AI Agent 开发生态。OpenAI、Github、VS Code、Cursor 均已跟进。为了更好的理解,你可以把。

十二测试录 1098

音视频转码ffmpeg(十六)

前言 音视频转码主要指这样的概念: 1、容器格式的转换,比如MP4转换为MOV 2、容器中音视频数据编码方式转换,比如H264编码转换成MPEG4编码,MP3换为AAC 3、音视频码率的转换,比如4M的视频码率降为2M 4、视频分辨率的转换,比如1080P视频变为720P,音频重采样等等 转码技术点分析 转码流程图 image.png 流拷贝是指源文件音/视频编码方式也被目标文件支持,那么此情况下音/视频数据拷贝就可以直接拷贝到目标文件下 转码类型分析 容器格式的转换 .

翻肚鱼儿的博客 2181

【从零开始学习 UVM】1.2、UVM 概述 —— UVM Installation(UVM 安装)

【从零开始学习 UVM】1.2、UVM 概述 —— UVM Installation(UVM 安装)

努力,加油每一天鸭~ 1945

【FFmpeg】 音视频转码详细流程

【FFmpeg】 音视频转码详细流程

m0_56051805的博客 3899

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

FFmpeg 转码:转换视频格式

FFmpeg 转码:转换视频格式 关键词:FFmpeg, 视频转码, 视频格式转换, 高效转码, 视频编解码, 高质量输出 1. 背景介绍 1.1 问题由来 在数字媒体时代,视频

AI大模型应用之禅 2497

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

使用FFmpeg进行视频转码

以上两种方法都能有效地在Java中使用FFmpeg进行视频转码。选择哪种方式取决于你的具体需求以及项目环境。如果你需要直接控制FFmpeg命令行,可以选择第一种方法;如果你希望使用封装好的Java API,可以选择JAVE2。/*** 将PCM格式转成其他音频格式*/} else {return cmd;/*** 添加片头*/// 老系统加了片头、片尾、背景音之后,都会变成双声道。

jakeswang的博客 1237

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

FFmpeg 格式转换:常用命令使用汇总

FFmpeg 是用于转换音频和视频文件的非常强大和通用的命令行工具。它是免费的,适用于 Windows,Mac 和 Linux 系统。无论您要加入两个视频文件,从视频文件中提取音频组件,将视频转换为动画 GIF,FFmpeg 可以做到这一切,甚至更多。FFmpeg 支持所有流行的音频和视频格式。或者,您可以运行命令./ffmpeg -formats 来获取 FFmpeg 安装支持的每种格式的列表。如果你刚刚开始,这里有一些命令可以帮助你了解这个工具的功能。

winkexin的博客 7314

ffmpge转码常用指令介绍(不断更新...)

帧率、分辨率、码率、文件大小转换 ffmpeg -i input.mp4 -r 30 -b:a 100k -b:v 200k -s 1920x1080 -fs 4000K output.mp4 -i 输入视频文件 -r 输出帧率 -b:a 输出音频码率 -b:v 输出视频码率 -s 输入视频分辨率 -fs 输出文件大小 output.mp4 输出视频文件 提取视频文件的pcm、yuv、h264、aac // 提取pcm ffmpeg -i input.mp4 -ar 16000 -ac 1 -f

weixin_36623563的博客 1150

FFmpeg工具使用(一)

其中,"-i input.mp4"表示指定输入文件为"input.mp4",而"output.avi"则是指定输出文件名,并将视频转码成AVI格式。其中,"-i input.mp4"表示指定输入文件为"input.mp4",而"output.avi"则是指定输出文件名,并将视频转码成AVI格式。具体来说,该命令的作用是通过指定不同的选项和参数组合,将一个或多个输入文件进行处理,并生成一个或多个输出文件。解码:根据输入文件的编码格式,FFmpeg会将视频、音频等数据进行解码,将其转换成原始的未压缩数据。

zw_ggr_2017的博客 3670

FFmpeg 音视频转码全攻略:参数详解与实战指南

在数字化浪潮下,相机、手机等设备生成的音视频文件格式繁杂,因编码和压缩算法的差异,常常面临 “此设备能播、彼设备不能播” 的兼容性难题。FFmpeg 作为开源免费的跨平台音视频处理神器,集录制、转换、流媒体功能于一体,支持几乎所有主流音视频格式的解码、编码与封装。本文将深入剖析 FFmpeg 核心转码参数,帮助开发者和技术爱好者轻松实现格式转换、画质优化、码率控制等需求,解决跨平台播放痛点。兼容性优先:优先选择 H.264(视频)+ AAC(音频)+ MP4(容器)组合,适配所有设备;体积与画质平衡。

代码煮茶君 1813
上一篇: vlc在Ubuntu下的自动安装和手动安装
下一篇: 一个简单的使用SDL播放YUV文件的示例
北雨南萍
北雨南萍 领域专家: 操作系统技术领域 领域专家: 操作系统技术领域
博客等级 码龄20年 2665粉丝 453原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北雨南萍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值