Skip to content

Commit

Permalink
enhance encoding shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
benIT committed Jan 31, 2018
1 parent 8736846 commit 6a8d54f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
46 changes: 46 additions & 0 deletions bin/encoder/encodeAll.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
####################################
#Usage:
#bin/encoder/encodeAll.sh: will encode all videos that are not already encoded.
#bin/encoder/encodeAll.sh -f: will encode all videos that are although if already encoded.
####################################
source 'bin/encoder/lib.sh'
ENCODE_EXISTING=false

function launchEncoding(){
CURRENTDIR=$1
for i in "${VIDEO_EXTENSION[@]}"
do
ls $CURRENTDIR/*.$i > /dev/null 2>&1
if [ $? -eq 0 ]; then
bin/encoder/encoder.sh ${CURRENTDIR}/*.$i
fi
done
}

while getopts ":f" opt; do
case $opt in
f)
echo "-f was triggered!" >&2
ENCODE_EXISTING=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done

for CURRENTDIR in $(ls -d ${MONITORDIR}/*)
do
echo ${CURRENTDIR}
if [ -f ${CURRENTDIR}/index.m3u8 ]; then
if [ "${ENCODE_EXISTING}" = true ]; then
launchEncoding ${CURRENTDIR}
else
log "$LEVEL_INFO" "${CURRENTDIR} already encoded!"
fi
else
launchEncoding ${CURRENTDIR}
fi
done
13 changes: 12 additions & 1 deletion bin/encoder/lib.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#!/bin/bash

##################################################################
#Variables
##################################################################
MONITORDIR="/vagrant/shared/e-media/web/e-media-data/video"
LOGFILE='var/logs/encoder.log'
declare -a VIDEO_EXTENSION=("mp4" "flv" "mkv");

##################################################################
#Functions
##################################################################
in_array() {
local haystack=${1}[@]
local needle=${2}
Expand All @@ -17,5 +28,5 @@ readonly LEVEL_ERROR='ERROR'
#$1: log level
#$2: log message
function log {
echo -e "$(date) - $1 - $2" >> var/logs/encoder.log
echo -e "$(date) - $1 - $2" >> ${LOGFILE}
}
11 changes: 5 additions & 6 deletions bin/encoder/watcher.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/bin/bash
#run this script to launch file watcher
source 'bin/encoder/lib.sh'
declare -a VIDEO_EXTENSION=("mp4" "flv" "mkv");

MONITORDIR="/vagrant/shared/e-media/web/e-media-data/video"
inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
in_array VIDEO_EXTENSION "${NEWFILE#*.}"
if [ $? -eq 0 ]; then
bin/encoder/encoder.sh $NEWFILE &
if [ -f $NEWFILE ]; then
in_array VIDEO_EXTENSION "${NEWFILE#*.}"
if [ $? -eq 0 ]; then
bin/encoder/encoder.sh $NEWFILE &
fi
fi
done

0 comments on commit 6a8d54f

Please sign in to comment.