Jacky's blog
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)

Jack Yang

编程; 随笔
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)
  • shell

  • tool

    • mac
    • mac tool
    • c tool
    • platform
    • dev tool
    • java tool
    • IDEA
    • vscode
    • Docker 使用指南
    • unbuntuOnWindows
    • oh-my-zsh
    • github
    • lldb
    • ripgrep 高性能文本搜索工具使用指南
    • appium
    • mvn
    • ffmpeg
      • ffmpeg
        • main option
      • ffplay
      • ffprobe
      • case
        • 多个音频文件合并成一个 IAMF
        • 合并 ts 为 mp4
      • link
    • gradle
    • git 使用指南
    • jenv
    • php-fpm
    • raycast
    • Vim 编辑器入门指南
    • jadx
    • excalidraw
  • 网络

  • algo

  • compute_base

  • blog

  • growth

  • java

  • C&C++

  • ai

  • secure

  • cms

  • english

  • 生活

  • 金融学

  • more

  • other
  • tool
Jacky
2023-10-17
目录

ffmpeg

FFmpeg is the leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. It supports the most obscure ancient formats up to the cutting edge. No matter if they were designed by some standards committee, the community or a corporation. It is also highly portable: FFmpeg compiles, runs, and passes our testing infrastructure FATE across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, etc. under a wide variety of build environments, machine architectures, and configurations.

更多内容可以见官网 link (opens new window)

# ffmpeg

# main option

link (opens new window)

  • -i: input
  • -vf filtergraph (output) Create the filtergraph specified by filtergraph and use it to filter the stream. This is an alias for -filter:v, see the -filter option.
  • -af: This is an alias for -filter:a
  • -c:v/-codec:v: codec (input/output,per-stream)
    • copy: 拷贝原有流
    • libx264: h264 转码
  • -c:a/-codec:a: 指定对音频编码处理
  • metadata
    • -metadata <key>=<value>: 指定 metadata
  • -attach

# ffplay

ffplay (opens new window)

在播放视音频文件时, 可以通过下列快捷键来控制播放:

  • q/ESC: 退出播放
  • f: 全屏显示
  • p/空格键: 暂停/播放
  • w: 显示音频波形
  • s: 逐帧显示
  • 左/右方向键: 向后/向前跳跃 10 秒
  • 上/下方向键: 向后/向前跳跃 1 分钟
  • Page Down / Page Up: 向后/向前跳跃 10 分钟
  • 鼠标点击屏幕: 跳转到指定位置

此外, 在播放视频前, 还可以预设一些参数, 如:

  • -window_title title: 设置窗口标题
  • -loop number: 设置播放循环次数
  • -showmode mode: 设置显示模式(0 为显示视频, 1 为显示音频波形, 2 为显示音频频谱)
  • -vf filtergraph: 设置视频滤镜
  • -af filtergraph: 设置音频滤镜

# ffprobe

todo

# case

  • ffmpeg -i source2.mp4 -c:v libx264 -c:a copy result2.mp4: 对视频进行 h264 转码, 拷贝音频数据
  • ffmpeg -i source2.mp4 -c:v libx264 -ac 1 -ar 48000 result2.mp4: 对视频进行 h264 转码, 对音频进行单声道, 48000 采样率处理
  • ffmpeg -i in.avi -metadata title="my title" out.flv: setting the title in the output file:
  • ffmpeg -i IMG_8478-1.jpg -vf scale=iw/2:ih/2 output.jpg: 压缩图片为原图一半
  • ffmpeg -i INPUT -attach DejaVuSans.ttf -metadata:s:2 mimetype=application/x-truetype-font out.mkv: -metadata:s:2 mimetype=application/x-truetype-font 是用来设置附件的元数据。具体来说, 这个元数据指定了附件的类型为 TrueType 字体文件, 其 MIME 类型为 application/x-truetype-font 这里的s:2 指定了元数据应该应用于第三个流(字幕、附件等), 并且 mimetype=application/x-truetype-font 指定了这个附件的 MIME 类型
  • ffmpeg -i screen-20240208-114421.mp4 -vf "scale=720:1520,fps=30" -ss 00:00:12 2.mp4: 修改视频尺寸、帧率、剪切
    • -ss 00:00:12: 指定开始截取的时间点, 即从视频的第 12 秒开始截取
  • ffmpeg -i SVID_20241216_212215_1.mp4 -c:a copy -vf "fps=30" out.mp4: 压缩帧率

# 多个音频文件合并成一个 IAMF

# 指定了输入文件, 分别是前声道(front)、后声道(back)、中心声道(center)和低频声道(lfe)的音频文件
ffmpeg -i front.wav -i back.wav -i center.wav -i lfe.wav
# 指定输入流的映射关系, 将每个输入文件的第一个流(音频流)映射到输出; 指定了音频编解码器为 Opus
-map 0:0 -map 1:0 -map 2:0 -map 3:0 -c:a opus
# 创建音频流组
#   id=1 指定了该音频流组的 ID, 这个 ID 可以在后续参数中引用
#   st=0:st=1:st=2:st=3 定义了流组中包含的音频流。每个 st 参数后面跟着一个数字, 代表该音频流在输入文件中的索引。这里的 st=0 表示将第一个输入文件(front.wav)的音频流加入到流组中
-stream_group type=iamf_audio_element:id=1:st=0:st=1:st=2:st=3,
# 在 IAMF 音频元素流组中设置了 demixing 参数, 指定了音频分离的参数 ID
demixing=parameter_id=998,
# 设置了重建增益参数, 用于控制输出音频的增益
recon_gain=parameter_id=101,
# 设置了不同层的声道布局, 分别是立体声(stereo)和 5.1 声道
layer=ch_layout=stereo,
layer=ch_layout=5.1,
-stream_group type=iamf_mix_presentation:id=2:stg=0:annotations=en-us=Mix_Presentation,
submix=parameter_id=100:parameter_rate=48000|element=stg=0:parameter_id=100:annotations=en-us=Scalable_Submix|layout=sound_system=stereo|layout=sound_system=5.1
# 指定了每个输出流的 ID, 这些 ID 将用于标识输出文件中的各个流
-streamid 0:0 -streamid 1:1 -streamid 2:2 -streamid 3:3 output.iamf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 合并 ts 为 mp4

写一个 shell, 解析 M3U 文件, 并下载所有的 ts 段到本地, 然后使用 ffmpeg 合并 ts 为 mp4

#!/bin/bash

# 检查参数是否正确
if [ $# -ne 2 ]; then
    echo "Usage: $0 <m3u_url> <output_file>"
    exit 1
fi

# 定义变量
m3u_url="$1"
output_file="$2"

# 下载M3U文件
wget -q "$m3u_url" -O playlist.m3u

# 提取TS片段链接
ts_urls=$(grep -v '^#' playlist.m3u)

# 下载TS片段到本地
for ts_url in $ts_urls; do
    filename=$(basename "$ts_url")
    wget -q "$ts_url" -O "$filename"
    if [ $? -ne 0 ]; then
        echo "Failed to download $ts_url"
        exit 1
    fi
done

# 使用FFmpeg合并TS为MP4
ffmpeg -i "concat:$(ls -v *.ts | tr '\n' '|')" -c copy "$output_file"

# 清理临时文件
rm -f playlist.m3u *.ts

echo "Conversion completed successfully!"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# link

  • Documentation (opens new window)
上次更新: 2025/10/09, 23:53:03
mvn
gradle

← mvn gradle→

最近更新
01
npx 使用指南
10-12
02
cursor
09-28
03
inspect
07-20
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Jacky | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式