author: hjjdebug
date: 2025年 12月 10日 星期三 17:17:44 CST
descrip: ffmpeg -map 是什么意思?


  1. -map 选项: 是每一个文件对应的选项,用于指定输出流对应的输入流.
    用ffmpeg -h long |grep map
    可以看到帮助:
Advanced per-file options:
-map [-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]  set input stream mapping

这个帮助信息,来源于ffmpeg_opt.c 中有一项map 的描述.

    { "map", HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_map },
        "set input stream mapping",
        "[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },

我们跟踪一下函数执行代码 opt_map, 可知道它到底干了什么.
从map 的value 中可得到file_index, 可得到stream_index, 把这个流或这些流都添加到optctx中,供以后打开输出文件时使用

  1. 举例
    例如: ffmpeg 执行如下命令:
$ ffmpeg -i 1.ts -filter_complex "[0]split[out1][out2]" -map "[out1]" output1.ts -map "[out2]" output2.ts -v debug -y

打开output1.ts 有选项 -map “[out1]”, “[” 说明数据来源是filter
打开输出文件时,会分析输出文件的选项, 见map选项 write_option 时要执行
static int opt_map(void* optctx, const char* opt, const char* arg)
其中optctx 就是OptionsContext 对象, OptionsContext* o = (OptionsContext *)optctx;
opt=“map”,
arg=“[out1]”
该函数执行后要在o->stream_maps 上 重新分配出一个map 对象. 上面写上:

p *m
$7 = {
  disabled = 0,
  mp_file_index = 0,
  stream_index = 0,
  linklabel = 0x51b340 "out1"
}

在打开文件时会用到OptionsContext o,

ret = open_file(&o, g->arg); //g->arg 就是output1.ts

具体代码还是挺复杂的,看看它的log输出吧. (verbose)

Opening an output file: output1.ts.
20251017 16:47:49.906 [out#0/mpegts @ 0x4f63c0] Adding streams from explicit maps...
20251017 16:49:45.213 [out#0/mpegts @ 0x4f63c0] Creating output stream from an explicitly mapped complex filtergraph 0, output [out1]
20251017 16:51:28.857 [vost#0:0/mpeg2video @ 0x6c9bc0] Created video stream from complex filtergraph 0:[split]
Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐