2016-07-21 13:48:49 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# loglevels: quiet, panic, fatal, error, warning, info, verbose, debug
|
|
|
|
LOGLEVEL=info
|
|
|
|
|
|
|
|
# set nice level, 20 lowest, -20 highest
|
|
|
|
NICE="nice -n 19"
|
|
|
|
|
|
|
|
# on pass or two pass encoding
|
|
|
|
PASSES="2"
|
|
|
|
|
|
|
|
###
|
|
|
|
###
|
|
|
|
###
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
if ! [ -f "$1" ]; then
|
|
|
|
echo ""$!" is not a file. Nothing to convert."
|
|
|
|
exit
|
2016-07-21 13:48:49 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
trap cleanup EXIT
|
|
|
|
cleanup() {
|
2018-03-10 11:47:55 +00:00
|
|
|
set +e
|
|
|
|
rm -f "${PASSLOGFILE}-0.log" "${PASSLOGFILE}-0.log.mbtree" "${TEMPFILE}"
|
2016-07-21 13:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# get creation date
|
|
|
|
TIMESTAMP=$(stat -l -t "%Y-%m-%d %H:%M:%S" "$1" | awk '{ print $6" "$7 }')
|
|
|
|
# get betrate
|
2018-03-10 11:47:55 +00:00
|
|
|
BITRATE=$(ffmpeg -i "$1" 2>&1 | grep bitrate | awk '{print $6}')
|
2016-07-21 13:48:49 +00:00
|
|
|
|
|
|
|
# set task priority to low
|
|
|
|
NICE="nice -n 19"
|
|
|
|
|
|
|
|
EXT="mp4"
|
|
|
|
|
|
|
|
DIRECTORY=$(dirname "$1")
|
|
|
|
FILENAME=$(basename "$1")
|
|
|
|
NOW=$(date "+%Y%m%d%-H%M%S")
|
|
|
|
TEMPFILE="$DIRECTORY/.${FILENAME%.*}_${NOW}_ffmpeg.${EXT}"
|
|
|
|
|
|
|
|
#METADATA="-metadata creation_time=\"$TIMESTAMP\""
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
if [ "$PASSES" == "1" ]; then
|
|
|
|
# 1 pass encoding
|
|
|
|
$NICE ffmpeg -i "$1" -threads auto -loglevel "$LOGLEVEL" -vcodec libx264 -b:v "${BITRATE}k" -vf yadif=1 -acodec libfaac -ab 192k -ar 48000 -sn -metadata creation_time="$TIMESTAMP" -f mp4 -y "${TEMPFILE}"
|
2016-07-21 13:48:49 +00:00
|
|
|
else
|
2018-03-10 11:47:55 +00:00
|
|
|
# 2 pass encoding
|
|
|
|
PASSLOGFILE="$DIRECTORY/.${FILENAME%.*}_${NOW}_ffmpeg"
|
|
|
|
$NICE ffmpeg -i "$1" -threads auto -loglevel "$LOGLEVEL" -vcodec libx264 -b:v "${BITRATE}k" -vf yadif=1 -pass 1 -passlogfile "$PASSLOGFILE" -an -sn -f rawvideo -y /dev/null
|
|
|
|
$NICE ffmpeg -i "$1" -threads auto -loglevel "$LOGLEVEL" -vcodec libx264 -b:v "${BITRATE}k" -vf yadif=1 -pass 2 -passlogfile "$PASSLOGFILE" -acodec libfaac -ab 192k -ar 48000 -sn -metadata creation_time="$TIMESTAMP" -f mp4 -y "${TEMPFILE}"
|
|
|
|
rm "${PASSLOGFILE}-0.log"
|
|
|
|
rm "${PASSLOGFILE}-0.log.mbtree"
|
2016-07-21 13:48:49 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
touch -r "$1" "${TEMPFILE}"
|
|
|
|
|
2018-03-10 11:47:55 +00:00
|
|
|
if [ -f "${1%.*}.${EXT}" ]; then
|
|
|
|
mv -f "${TEMPFILE}" "${1%.*}_${NOW}.${EXT}"
|
|
|
|
echo
|
|
|
|
echo "File "${1%.*}.${EXT}" already exist, moved to: "${1%.*}_${NOW}.${EXT}""
|
|
|
|
echo
|
2016-07-21 13:48:49 +00:00
|
|
|
else
|
2018-03-10 11:47:55 +00:00
|
|
|
mv -i "${TEMPFILE}" "${1%.*}.${EXT}"
|
2016-07-21 13:48:49 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
exit
|