FFmpeg で動画に字幕を付ける方法
FFmpeg で字幕を付ける手順。SRT/ASS のハードコード、字幕トラックのソフトコード、スタイル調整、多言語対応、字幕抽出まで網羅。

字幕は動画のアクセシビリティを高め、検索可能にし、無音環境でも視聴できるようにします。FFmpeg では字幕を映像に焼き込んだり、選択可能なトラックとして埋め込んだり、既存の字幕を抽出したり、字幕フォーマット間を変換したりできます。本記事では実用コマンドとともに、これらを一通り解説します。
ハードコードとソフトコード、どちらを選ぶ?
字幕を付ける前に、方式を決めておく必要があります。
| 方式 | 説明 | 長所 | 短所 |
|---|---|---|---|
| ハードコード(焼き込み) | 字幕を映像のピクセルに描画 | 常に表示される、再生環境を選ばない | オフにできない、再エンコードが必要 |
| ソフトコード(埋め込み) | 字幕を別トラックとして保存 | 切替可能、多言語対応 | プレイヤー側の対応が必要 |
目安:SNS や互換性重視ならハードコード、長尺コンテンツや多言語対応ならソフトコード。ユーザー投稿動画を扱う UGC プラットフォーム を作る場合は、たいていソフトコードのほうが向いています。
SRT 字幕をハードコードするには?
SRT は字幕フォーマット(subtitle format)でもっとも一般的です。映像に焼き込むには次のようにします。
# Hardcode SRT subtitles
ffmpeg -i input.mp4 -vf "subtitles=subs.srt" output.mp4
これだけです。FFmpeg は subtitles フィルター(libass ベース)で SRT を各フレームに直接描画します。
SRT の見た目をカスタマイズ
デフォルトの SRT 表示はかなり地味です。force_style でスタイルを指定できます。
# Custom font, size, and color
ffmpeg -i input.mp4 -vf "subtitles=subs.srt:force_style='FontName=Arial,FontSize=24,PrimaryColour=&H00FFFFFF,OutlineColour=&H00000000,Outline=2,Shadow=1'" output.mp4
主なスタイルパラメータ:
| パラメータ | 例 | 説明 |
|---|---|---|
| FontName | Arial | フォント名 |
| FontSize | 24 | サイズ(ポイント) |
| PrimaryColour | &H00FFFFFF | 文字色(ABGR hex) |
| OutlineColour | &H00000000 | 輪郭色 |
| BackColour | &H80000000 | 影・背景色 |
| Outline | 2 | 輪郭の太さ |
| Shadow | 1 | 影の深さ |
| Bold | 1 | 太字(0 か 1) |
| Alignment | 2 | 配置(2=下中央) |
| MarginV | 30 | 縦方向の余白 |
注意:色は通常の RGB ではなく ABGR(Alpha, Blue, Green, Red)形式です。
# Yellow text with black outline, larger font, positioned higher
ffmpeg -i input.mp4 -vf "subtitles=subs.srt:force_style='FontSize=28,PrimaryColour=&H0000FFFF,OutlineColour=&H00000000,Outline=3,MarginV=40'" output.mp4
ファイルパスに特殊文字が入っているとき
字幕ファイルのパスに特殊文字が含まれる場合はエスケープが必要です。
# Escape colons in Windows paths or special characters
ffmpeg -i input.mp4 -vf "subtitles='my\ subtitles.srt'" output.mp4
# Or use the subtitle file index approach
ffmpeg -i input.mp4 -i subs.srt -filter_complex "[0:v][1:s]overlay" output.mp4
ASS/SSA でリッチな字幕を焼き込むには?
ASS(Advanced SubStation Alpha)はフォント・色・アニメーション・行ごとの位置指定など、リッチなスタイル指定をサポートします。ハードコードすればすべて維持されます。
# Hardcode ASS subtitles (all styling preserved)
ffmpeg -i input.mp4 -vf "ass=subs.ass" output.mp4
シンプルな ASS ファイルを作る
ASS の基本構造はこんな感じです。
[Script Info]
Title: My Subtitles
ScriptType: v4.00+
PlayResX: 1920
PlayResY: 1080
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,Arial,48,&H00FFFFFF,&H000000FF,&H00000000,&H80000000,0,0,0,0,100,100,0,0,1,2,1,2,10,10,40,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:01.00,0:00:04.00,Default,,0,0,0,,Hello, this is a subtitle.
Dialogue: 0,0:00:05.00,0:00:08.00,Default,,0,0,0,,This line has {\b1}bold{\b0} text.
SRT を ASS に変換してスタイルを付ける
# Convert SRT to ASS (then you can edit the ASS styles)
ffmpeg -i subs.srt subs.ass
変換後、ASS ファイルの [V4+ Styles] セクションを編集すると見た目を細かく調整できます。配信先によっては、字幕を付ける前に 動画フォーマットの変換 も検討してください。
ソフトコード(埋め込みトラック)はどう書く?
ソフトコードの字幕は、コンテナ内に別ストリームとして格納されます。視聴者はオン/オフを切り替えられます。
SRT を字幕トラックとして追加
# Add SRT subtitle track to MP4
ffmpeg -i input.mp4 -i subs.srt -c copy -c:s mov_text output.mp4
# Add SRT subtitle track to MKV (MKV supports SRT natively)
ffmpeg -i input.mp4 -i subs.srt -c copy -c:s srt output.mkv
注意:MP4 コンテナでは字幕に mov_text codec を使う必要があります。MKV は SRT・ASS など多くの形式をネイティブにサポートします。
多言語の字幕トラックを追加
# Add English and Spanish subtitle tracks to MKV
ffmpeg -i input.mp4 -i english.srt -i spanish.srt \
-map 0:v -map 0:a -map 1 -map 2 \
-c copy -c:s srt \
-metadata:s:s:0 language=eng -metadata:s:s:0 title="English" \
-metadata:s:s:1 language=spa -metadata:s:s:1 title="Spanish" \
output.mkv
MP4 の場合:
# Add multiple subtitle tracks to MP4
ffmpeg -i input.mp4 -i english.srt -i chinese.srt \
-map 0:v -map 0:a -map 1 -map 2 \
-c copy -c:s mov_text \
-metadata:s:s:0 language=eng -metadata:s:s:0 title="English" \
-metadata:s:s:1 language=chi -metadata:s:s:1 title="Chinese" \
output.mp4
デフォルトの字幕トラックを設定
# Mark the first subtitle track as default
ffmpeg -i input.mp4 -i subs.srt \
-map 0 -map 1 \
-c copy -c:s mov_text \
-disposition:s:0 default \
output.mp4
ASS をソフトトラックとして追加
# Add ASS to MKV (preserves all styling for compatible players)
ffmpeg -i input.mp4 -i subs.ass -map 0 -map 1 -c copy -c:s ass output.mkv
動画から字幕を抽出するには?
SRT で抽出
# Extract the first subtitle track to SRT
ffmpeg -i input.mkv -map 0:s:0 output.srt
# Extract the second subtitle track
ffmpeg -i input.mkv -map 0:s:1 output.srt
ASS で抽出
# Extract subtitle track as ASS (preserves styling)
ffmpeg -i input.mkv -map 0:s:0 output.ass
含まれる字幕トラックの一覧
# Show all streams including subtitles
ffprobe -v error -show_entries stream=index,codec_name,codec_type -of csv input.mkv
# More detailed subtitle info
ffprobe -v error -select_streams s -show_entries stream=index,codec_name:stream_tags=language,title -of json input.mkv
すべての字幕トラックをまとめて抽出
#!/bin/bash
# Extract all subtitle tracks from a video
INPUT="input.mkv"
# Count subtitle streams
COUNT=$(ffprobe -v error -select_streams s -show_entries stream=index -of csv=p=0 "$INPUT" | wc -l)
for i in $(seq 0 $((COUNT - 1))); do
# Get language tag
LANG=$(ffprobe -v error -select_streams s:$i -show_entries stream_tags=language -of csv=p=0 "$INPUT")
LANG=${LANG:-"track$i"}
ffmpeg -i "$INPUT" -map 0:s:$i "${INPUT%.*}_${LANG}.srt"
done
字幕フォーマットを変換するには?
FFmpeg は字幕フォーマット間の変換にも対応します。
# SRT to ASS
ffmpeg -i subs.srt subs.ass
# ASS to SRT (loses styling)
ffmpeg -i subs.ass subs.srt
# SRT to WebVTT
ffmpeg -i subs.srt subs.vtt
# SUB/IDX (DVD) to SRT (requires OCR — FFmpeg can't do this directly)
# Use a tool like SubtitleEdit or Tesseract for bitmap-to-text conversion
| 変換元 | 変換先 | スタイル維持 |
|---|---|---|
| SRT → ASS | はい | デフォルトスタイルが付く |
| ASS → SRT | いいえ | スタイルはすべて失われる |
| SRT → VTT | 一部 | 基本書式は残る |
| VTT → SRT | 一部 | VTT 固有の機能は失われる |
字幕のタイミングがずれている、どう直す?
字幕を遅らせる・早める
# Delay subtitles by 2.5 seconds
ffmpeg -i input.mp4 -itsoffset 2.5 -i subs.srt -map 0 -map 1 -c copy -c:s mov_text output.mp4
ハードコードの場合は setpts フィルターを使うか、SRT ファイル自体を直接編集します(動画のトリミングや結合 を行うときは特に重要です)。
# Shift hardcoded subtitles (using subtitle filter with offset)
ffmpeg -i input.mp4 -vf "subtitles=subs.srt" -ss 00:00:02.500 output.mp4
解像度を考慮した焼き込みは?
ハードコードの字幕の見た目は、動画の解像度に大きく左右されます。元解像度に合わせるのが基本です。
# Hardcode subtitles while keeping original resolution
ffmpeg -i input.mp4 -vf "subtitles=subs.srt" -c:v libx264 -crf 23 -c:a copy output.mp4
# Hardcode and scale to 1080p
ffmpeg -i input.mp4 -vf "subtitles=subs.srt,scale=-2:1080" -c:v libx264 -crf 23 -c:a copy output.mp4
解像度ごとのフォントサイズ調整
# For 720p video — smaller font
ffmpeg -i input.mp4 -vf "subtitles=subs.srt:force_style='FontSize=20'" -c:v libx264 -crf 23 output.mp4
# For 4K video — larger font
ffmpeg -i input.mp4 -vf "subtitles=subs.srt:force_style='FontSize=48,Outline=3'" -c:v libx264 -crf 23 output.mp4
字幕処理をバッチ化するには?
全動画にハードコード
# Assumes each video has a matching .srt file
for f in *.mp4; do
srt="${f%.mp4}.srt"
if [ -f "$srt" ]; then
ffmpeg -i "$f" -vf "subtitles=$srt" -c:v libx264 -crf 23 -c:a copy "subtitled_${f}"
fi
done
全動画にソフトコード
# Add matching SRT files as subtitle tracks
for f in *.mp4; do
srt="${f%.mp4}.srt"
if [ -f "$srt" ]; then
ffmpeg -i "$f" -i "$srt" -c copy -c:s mov_text "soft_${f}"
fi
done
トラブルシューティング
「No such filter: subtitles」 — FFmpeg が libass サポートなしでビルドされています。フル版をインストールしてください。
# macOS
brew install ffmpeg
# Ubuntu/Debian
sudo apt install ffmpeg libass-dev
字幕が表示されない — 字幕ファイルの文字コードを確認してください。FFmpeg は UTF-8 を期待します。
# Convert subtitle encoding to UTF-8
iconv -f ISO-8859-1 -t UTF-8 subs_latin.srt > subs_utf8.srt
字幕の位置やサイズがおかしい — 動画解像度と字幕側で想定された解像度がずれている場合に起きます。force_style で上書きします。
ffmpeg -i input.mp4 -vf "subtitles=subs.srt:force_style='FontSize=24,MarginV=30'" output.mp4
特殊文字が四角になる(豆腐) — フォントが該当文字をサポートしていません。対象言語に合うフォントを指定します。
ffmpeg -i input.mp4 -vf "subtitles=subs.srt:force_style='FontName=Noto Sans CJK SC'" output.mp4
MP4 出力で「unknown codec」エラーが出る — MP4 は mov_text のみ対応です。他のフォーマットを使うなら MKV にするか、ハードコードに切り替えてください。
クラウドで動かす選択肢
字幕のハードコードには再エンコードが必要で、CPU 負荷が高くなります。1 時間の動画でも一般的なマシンで 20 分以上かかることがあります。FFHub は API 経由でクラウド上の FFmpeg を実行できるため、字幕の焼き込みを高速なクラウドサーバーに任せて、ローカルマシンを解放できます。
同じソース動画から複数バージョン(多言語、複数スタイル)の字幕付き動画を生成したいときに特に便利です。
まとめ
- どの環境でも確実に表示するなら ハードコード(
-vf "subtitles=subs.srt") - 切り替え可能にするなら ソフトコード(MP4 は
-c:s mov_text、MKV は-c:s srt) - SRT の見た目を整えるなら
force_style - リッチなスタイルやアニメーションが必要なら ASS フォーマット
- 多言語の字幕は
-mapで追加 - 抽出前は
ffprobeで字幕トラックを確認 - 字幕ファイルは必ず UTF-8 にする
関連記事
- How to Trim and Merge Videos with FFmpeg - Cut, split, and concatenate video clips while keeping subtitle timing in sync
- How to Convert Video Format with FFmpeg - Switch between MP4, MKV, and WebM containers that each handle subtitles differently
- How to Extract Audio from Video with FFmpeg - Pull audio tracks from video for transcription and subtitle generation