首页 > 其他分享 >ffmpeg之avformat_alloc_output_context2

ffmpeg之avformat_alloc_output_context2

时间:2024-02-21 14:11:39浏览次数:34  
标签:alloc name flv avformat ff pcm output muxer filename

函数原型:

int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat,
                                   const char *format_name, const char *filename);

功能:

  查找根据format_name或者filename或者oformat输出类型,并且初始化ctx结构。

参数:

ctx:      AVFormatContext结构体,ffmpeg核心结构体,会在函数内部给ctx分配AVFormatContext空间并初始化。

oformat:    指定输出格式的 AVOutputFormat 结构体指针。可以使用 NULL,此时将根据 format_name 自动选择输出格式。

format_name:  输出格式名称

filename:    输出文件/URL名称

返回值:

  成功:0;失败:非0

工作流程:

1. 首先判断oformat是否为NULL;

2. 如果oformat不为NULL;

  直接初始化ctx结构((*ctx)->oformat, 保存filename到(*ctx)->url);

3. 如果oformat为NULL:

  format_name不为NULL:会循环查找muxer_list结构成员的name成员是否与format_name相同;

  filename不为NULL:会循环查找muxer_list结构成员的extensions成员与filename的后缀是否相同;

  找到后初始化ctx结构((*ctx)->oformat, 保存filename到(*ctx)->url); 如果没找到的话释放分配的ctx空间,返回错误码!

  有关muxer_list的信息参考后续代码;

注意:

  那么会产生一个问题:如果format_name和filename都不为NULL呢?当然如果format_name = "flv"和filename="file.flv"这是没问题的,肯定找到同一个结构;那么如果format_name = "flv"和filename="file.mp4"呢?到底以谁为准呢?

  其实在avformat_alloc_output_context2函数内部有一个评分机制:format_name是100分;filename是5分;

  如果匹配到filename会获得5分,继续循环匹配;如果又匹配到format_name获得100分;100分大于5分所以以format_name的类型为准;

  也就是以format_name类型优先,mime_type次之(avformat_alloc_output_context2不考虑),filename最后。

附件

muxer_list结构

static const AVOutputFormat * const muxer_list[] = {
    &ff_a64_muxer,
    &ff_ac3_muxer,
    &ff_adts_muxer,
    &ff_adx_muxer,
    &ff_aiff_muxer,
    &ff_alp_muxer,
    &ff_amr_muxer,
    &ff_amv_muxer,
    &ff_apm_muxer,
    &ff_apng_muxer,
    &ff_aptx_muxer,
    &ff_aptx_hd_muxer,
    &ff_argo_asf_muxer,
    &ff_argo_cvg_muxer,
    &ff_asf_muxer,
    &ff_ass_muxer,
    &ff_ast_muxer,
    &ff_asf_stream_muxer,
    &ff_au_muxer,
    &ff_avi_muxer,
    &ff_avif_muxer,
    &ff_avm2_muxer,
    &ff_avs2_muxer,
    &ff_avs3_muxer,
    &ff_bit_muxer,
    &ff_caf_muxer,
    &ff_cavsvideo_muxer,
    &ff_codec2_muxer,
    &ff_codec2raw_muxer,
    &ff_crc_muxer,
    &ff_dash_muxer,
    &ff_data_muxer,
    &ff_daud_muxer,
    &ff_dfpwm_muxer,
    &ff_dirac_muxer,
    &ff_dnxhd_muxer,
    &ff_dts_muxer,
    &ff_dv_muxer,
    &ff_eac3_muxer,
    &ff_f4v_muxer,
    &ff_ffmetadata_muxer,
    &ff_fifo_muxer,
    &ff_fifo_test_muxer,
    &ff_filmstrip_muxer,
    &ff_fits_muxer,
    &ff_flac_muxer,
    &ff_flv_muxer,
    &ff_framecrc_muxer,
    &ff_framehash_muxer,
    &ff_framemd5_muxer,
    &ff_g722_muxer,
    &ff_g723_1_muxer,
    &ff_g726_muxer,
    &ff_g726le_muxer,
    &ff_gif_muxer,
    &ff_gsm_muxer,
    &ff_gxf_muxer,
    &ff_h261_muxer,
    &ff_h263_muxer,
    &ff_h264_muxer,
    &ff_hash_muxer,
    &ff_hds_muxer,
    &ff_hevc_muxer,
    &ff_hls_muxer,
    &ff_ico_muxer,
    &ff_ilbc_muxer,
    &ff_image2_muxer,
    &ff_image2pipe_muxer,
    &ff_ipod_muxer,
    &ff_ircam_muxer,
    &ff_ismv_muxer,
    &ff_ivf_muxer,
    &ff_jacosub_muxer,
    &ff_kvag_muxer,
    &ff_latm_muxer,
    &ff_lrc_muxer,
    &ff_m4v_muxer,
    &ff_md5_muxer,
    &ff_matroska_muxer,
    &ff_matroska_audio_muxer,
    &ff_microdvd_muxer,
    &ff_mjpeg_muxer,
    &ff_mlp_muxer,
    &ff_mmf_muxer,
    &ff_mov_muxer,
    &ff_mp2_muxer,
    &ff_mp3_muxer,
    &ff_mp4_muxer,
    &ff_mpeg1system_muxer,
    &ff_mpeg1vcd_muxer,
    &ff_mpeg1video_muxer,
    &ff_mpeg2dvd_muxer,
    &ff_mpeg2svcd_muxer,
    &ff_mpeg2video_muxer,
    &ff_mpeg2vob_muxer,
    &ff_mpegts_muxer,
    &ff_mpjpeg_muxer,
    &ff_mxf_muxer,
    &ff_mxf_d10_muxer,
    &ff_mxf_opatom_muxer,
    &ff_null_muxer,
    &ff_nut_muxer,
    &ff_obu_muxer,
    &ff_oga_muxer,
    &ff_ogg_muxer,
    &ff_ogv_muxer,
    &ff_oma_muxer,
    &ff_opus_muxer,
    &ff_pcm_alaw_muxer,
    &ff_pcm_mulaw_muxer,
    &ff_pcm_vidc_muxer,
    &ff_pcm_f64be_muxer,
    &ff_pcm_f64le_muxer,
    &ff_pcm_f32be_muxer,
    &ff_pcm_f32le_muxer,
    &ff_pcm_s32be_muxer,
    &ff_pcm_s32le_muxer,
    &ff_pcm_s24be_muxer,
    &ff_pcm_s24le_muxer,
    &ff_pcm_s16be_muxer,
    &ff_pcm_s16le_muxer,
    &ff_pcm_s8_muxer,
    &ff_pcm_u32be_muxer,
    &ff_pcm_u32le_muxer,
    &ff_pcm_u24be_muxer,
    &ff_pcm_u24le_muxer,
    &ff_pcm_u16be_muxer,
    &ff_pcm_u16le_muxer,
    &ff_pcm_u8_muxer,
    &ff_psp_muxer,
    &ff_rawvideo_muxer,
    &ff_rm_muxer,
    &ff_roq_muxer,
    &ff_rso_muxer,
    &ff_rtp_muxer,
    &ff_rtp_mpegts_muxer,
    &ff_rtsp_muxer,
    &ff_sap_muxer,
    &ff_sbc_muxer,
    &ff_scc_muxer,
    &ff_segafilm_muxer,
    &ff_segment_muxer,
    &ff_stream_segment_muxer,
    &ff_smjpeg_muxer,
    &ff_smoothstreaming_muxer,
    &ff_sox_muxer,
    &ff_spx_muxer,
    &ff_spdif_muxer,
    &ff_srt_muxer,
    &ff_streamhash_muxer,
    &ff_sup_muxer,
    &ff_swf_muxer,
    &ff_tee_muxer,
    &ff_tg2_muxer,
    &ff_tgp_muxer,
    &ff_mkvtimestamp_v2_muxer,
    &ff_truehd_muxer,
    &ff_tta_muxer,
    &ff_ttml_muxer,
    &ff_uncodedframecrc_muxer,
    &ff_vc1_muxer,
    &ff_vc1t_muxer,
    &ff_voc_muxer,
    &ff_w64_muxer,
    &ff_wav_muxer,
    &ff_webm_muxer,
    &ff_webm_dash_manifest_muxer,
    &ff_webm_chunk_muxer,
    &ff_webp_muxer,
    &ff_webvtt_muxer,
    &ff_wsaud_muxer,
    &ff_wtv_muxer,
    &ff_wv_muxer,
    &ff_yuv4mpegpipe_muxer,
    NULL };

  

muxer_list的成员结构(&ff_flv_muxer为例)

const AVOutputFormat ff_flv_muxer = {
    .name           = "flv",//输出格式类型,avformat_alloc_output_context2的第三个参数(format_name)会与该字段匹配
    .long_name      = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
    .mime_type      = "video/x-flv",
    .extensions     = "flv",//输出文件的后缀,avformat_alloc_output_context2的第四个参数(filename)会与该字段匹配
    .priv_data_size = sizeof(FLVContext),
    .audio_codec    = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
    .video_codec    = AV_CODEC_ID_FLV1,
    .init           = flv_init,
    .write_header   = flv_write_header,
    .write_packet   = flv_write_packet,
    .write_trailer  = flv_write_trailer,
    .deinit         = flv_deinit,
    .check_bitstream= flv_check_bitstream,
    .codec_tag      = (const AVCodecTag* const []) {
                          flv_video_codec_ids, flv_audio_codec_ids, 0
                      },
    .flags          = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
                      AVFMT_TS_NONSTRICT,
    .priv_class     = &flv_muxer_class,
};

  

mime_type

  MIME类型:是一种标准化的互联网媒体类型,用于标识在互联网上传输的数据的格式。每种文件都有一个唯一的 MIME 类型,它描述了文件的内容和格式。MIME 类型通常由两个部分组成:主类型和子类型,中间由斜杠分隔。avformat_alloc_output_context2函数不作考虑!

  常见的 MIME 类型包括:

  文本文件:text/plain

  HTML 文件:text/html

  图片文件:image/jpeg、image/png

  音频文件:audio/mpeg

  视频文件:video/mp4

  应用程序文件:application/pdf、application/json

标签:alloc,name,flv,avformat,ff,pcm,output,muxer,filename
From: https://www.cnblogs.com/yhfs/p/18025080

相关文章

  • flink的分流器-sideoutput Flink 有两种常见的 State类型,分别是:Keyed State (键控状态
    flink的分流器-sideoutputFlink有两种常见的State类型,分别是:KeyedState(键控状态)和OperatorState(算子状态)为了说明侧输出(sideouptut)的作用,浪尖举个例子,比如现在有一篇文章吧,单词长度不一,但是我们想对单词长度小于5的单词进行wordcount操作,同时又想记录下来哪些单词的长度......
  • C语言-calloc
    #include<stdio.h>#include<stdlib.h>intmain(){int*p=(int*)calloc(10,sizeof(int));if(p==NULL){perror("calloc");exit;}printf("%d\n",&p);//6487576printf("%......
  • C语言-calloc
    学习网址C语言动态内存函数(malloc、calloc、realloc、free)详解:https://www.jb51.net/program/295325hjh.htmC语言动态内存函数详解:https://www.jb51.net/article/223725.htmcalloc函数需要头文件:<stdlib.h>calloc函数(动态内存分配函数):void*calloc(size_tnum,size_tsize);功能......
  • [MIT 6.S081] Lab: xv6 lazy page allocation
    Lab:xv6lazypageallocationEliminateallocationfromsbrk()这一步比较简单,直接在sys_sbrk中将分配内存改为对内存大小进行修改而不分配内存即可。uint64sys_sbrk(void){intaddr;intn;if(argint(0,&n)<0)return-1;addf=myproc()->sz;my......
  • C语言-malloc学习
    学习网址C语言动态内存函数(malloc、calloc、realloc、free)详解:https://www.jb51.net/program/295325hjh.htmC语言动态内存函数详解:https://www.jb51.net/article/223725.htm在C语言中,动态内存函数是块重要的知识点,以往,我们开辟空间都是固定的。数组编译结束后就不能继续给它开......
  • [git] out of memory malloc failed 解决
    [git]outofmemorymallocfailed解决-个人文章-SegmentFault思否git提交时,报错"Fatal:Outofmemory,mallocfailed(triedtoallocate42446849bytes)" 解决这个报错1、需要把windowMemory容量调大(默认10m,调整为50m)2、建议同时调整postbuffer的容量。3、最后还......
  • ffmpeg-读取媒体文件信息-avformat_open_input
    实例代码Codeintvideo_audio_info(intargc,char*argv[]){ if(argc<2){ printf("Usage:%smediaFile\n",argv[0]); return-1; } AVFormatContext*ic=NULL; charpath[20]={0}; strcpy(path,argv[1]); //1.打开媒体文件 intret=avfo......
  • 监控malloc时brk系统调用
    我们都知道malloc底层是调用brk系统调用实现的,但是不是每次malloc都会调用brk。我们来使用bpftrace验证以下。写一个malloc的程序。#include"stdio.h"#include<stdlib.h>intmain(){void*p;p=malloc(5000);printf("%p\n",p);p=malloc(8);......
  • tensorflow-gpu error:CUDNN_STATUS_ALLOC_FAILED或者self._traceback = tf_stack.extr
    tensorflow-gpuerror:CUDNN_STATUS_ALLOC_FAILED或者self._traceback=tf_stack.extract_stack() 在有些情况下,因为深度学习框架版本更新,细节的变动会使我们的代码最初对应修改:报错信息(出现其中一种):1.Couldnotcreatecudnnhandle:CUDNN_STATUS_ALLOC_FAILED2.self._trac......
  • C++中,new与malloc的区别何在?(代码实验向)
    在C++中,new与malloc()都可用于在堆中分配一块内存。其中,new是C++的语法,而malloc则来自古老的C语言,二者在使用时有何区别?new会调用构造函数,而malloc()不会假设有一个矩形类Rect,定义如下:classRect{public:Rect(){/*初始化矩形的宽和高为10*/mWidth......