YANO's digital garage

Copyright ©YANO All rights reserved. https://www.bravotouring.com/~yano/

Last-modified: 2024-04-10 (水)


[一語一絵/IT系]

ffmpeg / 2012-10-14 (日)

PT3@ubuntu 10.04によるgt110bサーバ強化策。

9月30日10月3日4日の作業で予約録画まで機能できるようになったので、録画したTSファイルをmp4にエンコードできるよう[External]コピペでできる録画サーバー on Linux with PT2 (エンコード編)等を参考に環境整備。

まずはyasmだが、ubuntu 10.04のaptリポジトリ経由で入手可能なyasmが0.8.0-1と古い事から、一旦削除してソースからinstallしなおし。

yano@GT110b:~$ sudo apt-get remove yasm
yano@GT110b:~$ cd ~/software/
yano@GT110b:~/software$ wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
yano@GT110b:~/software$ tar xvf yasm-1.2.0.tar.gz
yano@GT110b:~/software$ cd yasm-1.2.0
yano@GT110b:~/software/yasm-1.2.0$ ./configure
yano@GT110b:~/software/yasm-1.2.0$ make && sudo make install
続いてx264
yano@GT110b:~$ cd ~/software/tv/
yano@GT110b:~/software/tv$ git clone git://git.videolan.org/x264.git
yano@GT110b:~/software/tv$ cd ~/software/tv/x264
yano@GT110b:~/software/tv/x264$ ./configure --enable-shared
yano@GT110b:~/software/tv/x264$ make && make install
更にfaac
yano@GT110b:~$ cd ~/software/tv/
yano@GT110b:~/software/tv$ wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz
yano@GT110b:~/software/tv$ tar xvfz faac-1.28.tar.gz
yano@GT110b:~/software/tv$ cd ~/software/tv/faac-1.28
yano@GT110b:~/software/tv/faac-1.28$ ./configure --with-mp4v2
common/mp4v2/mpeg4ip.hのstrcasestrの宣言が標準のstring.hのそれと不整合してエラーになるので、全体を#ifndef _STRING_Hで括る次の
*** common/mp4v2/mpeg4ip.h.orig 2009-01-27 07:42:35.000000000 +0900
--- common/mp4v2/mpeg4ip.h      2012-10-13 16:29:30.593109085 +0900
***************
*** 120,125 ****
--- 120,126 ----
  #endif
  #include <sys/param.h>

+ #ifndef _STRING_H
  #ifdef __cplusplus
  extern "C" {
  #endif
***************
*** 127,132 ****
--- 128,134 ----
  #ifdef __cplusplus
  }
  #endif
+ #endif // _STRING_H

  #define OPEN_RDWR O_RDWR
  #define OPEN_CREAT O_CREAT
パッチを適用した後、make。
yano@GT110b:~/software/tv/faac-1.28$ make && make install
最後にffmpeg。ffmpegにバンドルされるlibfaacは、コードの一部がLGPLに沿っていないらしく、コンパイルオプションで-enable-libfaac -enable-nonfreeとしなくてはならないそうだ。
yano@GT110b:~$ cd ~/software/tv/
yano@GT110b:~/software/tv$ git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
yano@GT110b:~/software/tv$ cd ~/software/tv/ffmpeg/
yano@GT110b:~/software/tv/ffmpeg$ ./configure --enable-libx264 --enable-libfaac --enable-gpl --enable-nonfree
yano@GT110b:~/software/tv/ffmpeg$ make && make install
yano@GT110b:~/software/tv/ffmpeg$ echo "/usr/local/lib" > ./ffmpeg.conf;
yano@GT110b:~/software/tv/ffmpeg$ sudo mv ./ffmpeg.conf /etc/ld.so.conf.d/
yano@GT110b:~/software/tv/ffmpeg$ sudo updatedb

/usr/local/share/ffmpeg/libx264-ipod640.ffpreset[External]ffmpeg で TS をできるだけ高画質な mp4 へ変換してみたを参考に、/usr/local/share/ffmpeg/libx264-hq.ffpresetを作成。

vcodec=libx264

vprofile=baseline
maxrate=10000000
bufsize=10000000

level=41
crf=25
coder=1
flags=+loop
cmp=+chroma
partitions=+parti8x8+parti4x4+partp8x8+partb8x8
me_method=umh
subq=7
me_range=16
g=250
keyint_min=25
sc_threshold=40
i_qfactor=0.71
b_strategy=1
qmin=10
rc_eq='blurCplx^(1-qComp)'
bf=16
bidir_refine=1
refs=6

~/bin/ts2mp4_1280x720.shを作成。

#!/bin/bash

CPU_CORES=$(/usr/bin/getconf _NPROCESSORS_ONLN)

FFMPEG=/usr/local/bin/ffmpeg;
OUTDIR=/mnt/newpool/Videos;
LOGDIR=${OUTDIR}/.logs

OPT_THREADS="-threads ${CPU_CORES}";

X264_PRESET=/usr/local/share/ffmpeg/libx264-hq.ffpreset
OPT_FILTER="-filter:v yadif"
OPT_ASPECT="-aspect 16:9"
OPT_SIZE="-s 1280x720"
OPT_CODEC_VIDEO="-fpre ${X264_PRESET} -r 30000/1001 ${OPT_FILTER} -b:a 2M -bt 2M ${OPT_ASPECT} ${OPT_SIZE} -vsync 1"
OPT_CODEC_AUDIO="-acodec libfaac -ac 2 -ar 48000 -ab 128k"

INFILE=$1;
FNAME=`basename "${INFILE}" .ts`;
OUTFILE=${OUTDIR}/${FNAME}.mp4
LOGFILE=${LOGDIR}/${FNAME}.log

FFMPEG_CMDLINE="-y -i ${INFILE} -f mp4 ${OPT_THREADS} ${OPT_CODEC_VIDEO} ${OPT_CODEC_AUDIO} -map 0:0 -map 0:1 ${OUTFILE}"

echo "${INFILE} -> ${OUTFILE}"
(time ${FFMPEG} ${FFMPEG_CMDLINE}) >"${LOGFILE}" 2>&1
次のスクリプトでファイル名の半角スペースをアンダーバーに置き換えてから、
for f in *.ts;do n=`echo $f | sed 's/ /_/g'`;mv "$f" "$n";done
次のスクリプトで、ディレクトリ内のtsファイルのmp4変換を実施。
for f in *.ts;do ~/bin/ts2mp4_1280x720.sh "$f";done
…という事で最終的にできるようになったのだが、今のところ変換に実時間の6倍近くかかってしまうのがアレ。

録画時間が週平均28時間を超えると追いつかなくなってしまうので、処理能力の改善を図る必要がありそうだ。

[External]VAAPI対応が期待できるのならばIvy Bridgeに移行するのもありなんだけどな…

【参照】
●GeekなNooblog http://d.hatena.ne.jp/sona-zip/
コピペでできる録画サーバー on Linux with PT2 (エンコード編) 2012年1月15日
●I’m just another TeXnician. http://d.hatena.ne.jp/munepi/
ffmpeg で TS をできるだけ高画質な mp4 へ変換してみた 2009年12月27日
●Intel Linux Graphics https://01.org/linuxgraphics/
VAAPI | Linux Graphics
●VAAPI http://freedesktop.org/wiki/Software/vaapi/
●Wikipedia http://en.wikipedia.org/wiki/
Video Acceleration API