Skip to content

Instantly share code, notes, and snippets.

@OldBigBuddha
Last active October 28, 2018 08:02
Show Gist options
  • Select an option

  • Save OldBigBuddha/d10e4f3ac3229c8c8d3ae62f342102ae to your computer and use it in GitHub Desktop.

Select an option

Save OldBigBuddha/d10e4f3ac3229c8c8d3ae62f342102ae to your computer and use it in GitHub Desktop.
ラジオ動画(静止画の動画化及び音声の合成)を作成するスクリプト
#!/bin/bash
# 参考: ffmpegで静止画と音声をくっつけた映像を作る https://qiita.com/ottyajp/items/4cd5280b4b8e8ff331e1
CMDNAME=`basename $0`
if [ $# -ne 6 ]; then
echo "Usage: $CMDNAME [-i image] [-s sound] [-o output]" 1>&2
exit 1
fi
while getopts i:s:o: OPT
do
case $OPT in
"i" ) image="$OPTARG";;
"s" ) sound="$OPTARG";;
"o" ) output="$OPTARG";;
* ) echo "Usage: $CMDNAME [-i image] [-s sound] [-o output]" 1>&2
exit 1;;
esac
done
# 音声ファイルの長さ取得
dur=`avprobe $sound 2>&1 | grep Duration | sed -e s/Duration:\ // -e s/,.*//`
# 静止動画生成
# H.265なmp4を生成
ffmpeg -f image2 -r 1 -loop 1 -t $dur -i $image temp.mp4 -s 1280x720 -c:v libx265 -tune ssim -y > /dev/null 2>&1
# 静止動画に音声追加
# TODO: 更新を標準出力で出せるようにする
ffmpeg -i temp.mp4 -i $sound -s 1280x720 -c:a libmp3lame -c:v libx265 -tune ssim -y $output > /dev/null 2>&1
rm temp.mp4
# 動画出力結果
avprobe $output 2>&1 | grep -e Duration -e Stream | sed 's/^[ \t]*//' | sed -n 's/\(Duration: [^,]*\).*/\1/p; s/.*\(Video: [^(]*\).* \([0-9][0-9]*x[0-9][0-9]*\).*/\1\2/p; s/.*.*\(Audio: [^(]*\).*/\1/p;'
exit
# 結果例
# $ ./encode -i base.png -s radio.mp3 -o dest.mp4
# Duration: 00:18:04.40
# Video: hevc 1280x720
# Audio: mp3 (mp4a / 0x6134706D)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment