ffmpeg源码分析二:main函数和transcode函数

2024-08-21 16:18

本文主要是介绍ffmpeg源码分析二:main函数和transcode函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先从main函数看起,关键解释部分已加注释,该函数在ffmpeg.c文件中。代码如下:

int main(int argc, char **argv)
{int ret;int64_t ti;// 注册清理回调函数register_exit(ffmpeg_cleanup);setvbuf(stderr,NULL,_IONBF,0); /* win32 runtime needs this */av_log_set_flags(AV_LOG_SKIP_REPEATED);parse_loglevel(argc, argv, options);if(argc>1 && !strcmp(argv[1], "-d")){run_as_daemon=1;av_log_set_callback(log_callback_null);argc--;argv++;}// 注册组件们avcodec_register_all();
#if CONFIG_AVDEVICEavdevice_register_all();
#endifavfilter_register_all();av_register_all();avformat_network_init();show_banner(argc, argv, options);term_init();/* parse options and open all input/output files */// 解析参数,并且打开输入输出文件ret = ffmpeg_parse_options(argc, argv);if (ret < 0)exit_program(1);if (nb_output_files <= 0 && nb_input_files == 0) {show_usage();av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);exit_program(1);}/* file converter / grab */if (nb_output_files <= 0) {av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");exit_program(1);}//     if (nb_input_files == 0) {
//         av_log(NULL, AV_LOG_FATAL, "At least one input file must be specified\n");
//         exit_program(1);
//     }current_time = ti = getutime();// 音视频转换函数if (transcode() < 0)exit_program(1);ti = getutime() - ti;if (do_benchmark) {printf("bench: utime=%0.3fs\n", ti / 1000000.0);}av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" frames successfully decoded, %"PRIu64" decoding errors\n",decode_error_stat[0], decode_error_stat[1]);if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])exit_program(69);exit_program(received_nb_signals ? 255 : main_return_code);return main_return_code;
}

下面阅读transcode函数:

static int transcode(void)
{int ret, i;AVFormatContext *os;OutputStream *ost;InputStream *ist;int64_t timer_start;// 初始化,打开所有输出流的编码器,打开所有输入流的解码器,写入所有输出文件的文件头。后面详细介绍。ret = transcode_init();if (ret < 0)goto fail;if (stdin_interaction) {av_log(NULL, AV_LOG_INFO, "Press [q] to stop, [?] for help\n");}timer_start = av_gettime();#if HAVE_PTHREADSif ((ret = init_input_threads()) < 0)goto fail;
#endif// 循环,直到收到系统信号才退出 。while (!received_sigterm) {int64_t cur_time= av_gettime();/* if 'q' pressed, exits */if (stdin_interaction)if (check_keyboard_interaction(cur_time) < 0)break;/* check if there's any stream where output is still needed */if (!need_output()) {av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");break;}// 具体的转换工作,后面将详细介绍。ret = transcode_step();if (ret < 0) {if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))continue;av_log(NULL, AV_LOG_ERROR, "Error while filtering.\n");break;}/* dump report by using the output first video and audio streams */print_report(0, timer_start, cur_time);}
#if HAVE_PTHREADSfree_input_threads();
#endif// 文件处理完了,把缓冲中剩余的数据写到输出文件中。/* at the end of stream, we must flush the decoder buffers */for (i = 0; i < nb_input_streams; i++) {ist = input_streams[i];if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {output_packet(ist, NULL);}}flush_encoders();term_exit();/* write the trailer if needed and close file */// 为输出文件写文件尾(有的不需要)。  for (i = 0; i < nb_output_files; i++) {os = output_files[i]->ctx;av_write_trailer(os);}/* dump report by using the first video and audio streams */print_report(1, timer_start, av_gettime());// 关闭所有编码器。/* close each encoder */for (i = 0; i < nb_output_streams; i++) {ost = output_streams[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);if (ist->hwaccel_uninit)ist->hwaccel_uninit(ist->st->codec);}}/* finished ! */ret = 0;fail:
#if HAVE_PTHREADSfree_input_threads();
#endifif (output_streams) {for (i = 0; i < nb_output_streams; i++) {ost = output_streams[i];if (ost) {if (ost->stream_copy)av_freep(&ost->st->codec->extradata);if (ost->logfile) {fclose(ost->logfile);ost->logfile = NULL;}av_freep(&ost->st->codec->subtitle_header);av_freep(&ost->forced_kf_pts);av_freep(&ost->apad);av_dict_free(&ost->opts);av_dict_free(&ost->swr_opts);av_dict_free(&ost->resample_opts);}}}return ret;
}



这篇关于ffmpeg源码分析二:main函数和transcode函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1093653

相关文章

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺