FFMPEG Bash Iwatch Script convertire automata

Dupa ce am instalat ffmpeg, am cutat sa-mi convertesc fisierele dintr-un folder automat, la urcarea acestora printr-un ftp. Am folosit iwatch pentru a sti tot timpul cand apar fisiere noi intr-un folder si un script bash pentru a executa ffmpeg.

Instalarea iwatch se face foarte simplu pe Ubuntu 12.04 /14.04:

sudo apt-get install iwatch

In timpul instalarii iwatch, s-ar putea sa fii intrebat de configuratia Postfix. Asta pentru ca iwatch sa poata sa trimita mail-uri cu diferite lucruri. Daca nu te intereseaza sa primesti mail-uri, poti selecta “No configuration”.

Acum putem sa configuram iwatch.

root@black:/# vim /etc/iwatch/iwatch.xml

<?xml version="1.0" ?>
<!DOCTYPE config SYSTEM "/etc/iwatch/iwatch.dtd" >

<config>
  <guard email="root@localhost" name="IWatch"/>
  <watchlist>
    <title>New video encoding started.</title>
    <contactpoint email="mail@mail.ro" name="admin"/>
    <path type="recursive" alert="off" exec="find %f -iname '*.mp4' -execdir /data/upload/encode_files.sh {} \;" events="close_write">/data/upload</path>
  </watchlist>
</config>

Dupa ce am configurat iwatch trebuie modificat configul /etc/default/iwatch pentru a putea porni ca daemon.

root@black:/# vim /etc/default/iwatch

## iwatch configuration file

# START_DAEMON:
#   should iwatch start the iwatch daemon during boot?
START_DAEMON=true

# CONFIG_FILE:
#   configuration file for iwatch daemon
#
CONFIG_FILE=/etc/iwatch/iwatch.xml

Configul trebuie sa arate ca mai sus, unde am schmbat START_DAEMON=false in START_DAEMON=true.

root@black:/# /etc/init.d/iwatch start

Deci dupa pornire, iwatch va monitoriza in permanenta si in timp real calea /data/upload.

In acceasi cale va trebui sa avem si scriptul bash care sa fie executat in cazul in care apar fisiere noi.

root@black:/# vim /data/upload/encode_files.sh

f=$(basename "$1")
if ! [ -f "/data/files/$f" ]; then
  /usr/local/bin/ffmpeg -i "$1" -codec:v libx264 -profile:v high -preset slow -b:v 800k -maxrate 1000k -bufsize 1200k -vf scale=-1:720 -threads 0 -codec:a libfaac -b:a 128k "/data/files/$f"

fi

Iwatch va executa scriptul bash numai atunci cand un fisier a fost uploadat complet iar scriptul la randul lui va verifica daca in calea /data/files mai exista un fisier cu acelasi nume, daca nu exista executa ffmpeg si automat noul fisier va fi pus in calea /data/files.

Leave a Reply

Your email address will not be published. Required fields are marked *