ffmpegの技

アスペクト比変換/センタリング/ダイエット
古いカーナビ向け調整。ベンチマークでも良く使う。

av_aspect.sh:

#!/bin/bash
width=800
height=$(( $width * 480 / 800 ))
# PV000_ は変換後ファイルの目印、兼ランダムプレイのヘッダとして付与
ffmpeg -i "$1" -c:a copy \
-vf "scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2" \
PV000_"$1"


ランダム再生支援
PV000_*.mp4はリネームしない(優先プレイ)。解消するにはav_rename.sh -cを先に実行。

av_rename.sh:

#!/bin/bash

_usage() {
    echo "usage:"
    echo "${0} -c -u -t"
    exit 1
}

if [ "$#" = "0" ]; then
    _usage
fi

while getopts "ctu" OPT
do
    case $OPT in
        c)  headerclean=1
            ;;
        t)  testmode=1
            ;;
        u)  update=1
            ;;
        :|\?) _usage;;
    esac
done

if [ $headerclean ] ; then
    ## ヘッダがあれば除去し終了
    echo "mp4ファイルのPVXXXを除去します。"
    for file in *.mp4; do
    if [ "${file::2}" = "PV" ] ; then
        ## 既に一度ヘッダを付加済みのファイル
        newfile="${file:6}"
        mv "$file" "$newfile"
    fi
    done
    echo "mp4ファイルのPVXXXを除去しました。"
    exit 0
else
    ## ヘッダ付加または更新
    for file in *.mp4; do
        ## PV000で始まるファイルはリネームしない。
        if [ "${file::5}" = "PV000" ] ; then
            continue
        fi
        rand=$((RANDOM%1000))
        head=`printf "PV%03d_" $rand`
        if [ "${file::2}" = "PV" ] ; then
            ## 既に一度ヘッダを付加済みのファイル
            newfile="${head}${file:6}"
        else
            newfile="${head}${file}"
        fi
        printf "${file} ==> ${newfile}"
        if [ $testmode ] ; then
            printf " :: テストモード\n"
        else
            mv "$file" "$newfile"
            printf " :: 更新完了\n"
        fi
    done
fi



その他のコマンド:

## 先頭7秒カット(再エンコせずカット&コピーする方式)
ffmpeg -ss 7 -i input.mp4 -vcodec copy -acodec copy output.mp4

## うまくいかないときは、-ssの位置を変える。処理が異なるらしい。
ffmpeg  -i input.mp4 -ss 7 -vcodec copy -acodec copy output.mp4

## 800:480 中央からカット(クロップ)
ffmpeg -i input.mp4 -c:a copy -vf crop=800:480 output.mp4

## ボリュームアップ
ffmpeg -i input.mp4 -vcodec copy -af volume=10dB output.mp4

## オーディオ抽出
ffmpeg -i input.mp4 -vn -f mp3 output.mp3

## 左右反転
ffmpeg -i input.mp4 -vf hflip output.mp4

## 静止画にする
convert -resize 800x480! 静止画.jpg seisiga.jpg  ## 静止画のアスペクト比調整
ffmpeg -i input.mp4 -vf output.mp4  ## 動画を除去
ffpmeg -i seisiga.jpg -i output.mp4 -vcodec mpeg4 -acodec copy new.mp4  ## マージ