#!/bin/bash
#
#  flv to mp4 conversion script
#
#  Converts all or specified number of files from source folder into destination folder
#  and optionally also deletes source file
#
#  Author: Arto Jääskeläinen, ajaaskel.forum.ubuntu-fi@nic.fi
#
#  GPL3 -- feel free to use the way you like
#
#  v1.0  2014-12-29  <Initial version
#  v1.1  2015-02-20  Exit if ffmpeg or avconv missing
#  v1.2  2015-02-23  Rename once if destination exists, overwrite after that
#  v1.3  2015-03-01  Destination path bug fix
#
###############################################################################################


log_file="$PWD"/"flv_mp4.log"

# Check command line parameters
if [ "$1" = "" ] || [ "$2" = "" ]; then
echo -e "\nConverts .flv files into .mp4 files\n"
echo  "Usage: " "$0" " <input_folder>  <output_folder>  [max_number_of_files_to_convert]  [--delete]"
echo
echo  "        Quits after N files if limit is set, --delete = delete source file if conversion successful "
echo
exit 1
fi
if ! [ -d "$1" ]; then echo "Invalid folder $1"; exit 1; fi
if ! [ -d "$2" ]; then echo "Invalid folder $2"; exit 1; fi
if [ "$3" = "--delete" ]; then
set "$1" "$2" "" "--delete"    # move "--delete" to right position
fi

echo "$(date +%F_%T) $0 started" | tee -a "$log_file"

# Check that either "avconv" or "ffmpeg" is available
if  [ "$(which avconv)" = "" ] && [ "$(which ffmpeg)" = "" ]; then
    echo  "Prerequisite avconv or ffmpeg not found, please install first." | tee -a "$log_file"
    exit 1
else
    if [ "$(which avconv)" != "" ]; then 
        conv_prog="avconv"
    else
        conv_prog="ffmpeg"
    fi
echo  "$(date +%F_%T) Detected conversion program: $(which $conv_prog)" | tee -a "$log_file"
fi

# Check whether limiting number of files is wanted
if [ "$3" != "" ]; then
    limit="$3"
else
    limit="no"
fi

count_ok=0
count_fail=0
for i in "$1"/*.flv; do
if [ "$i" = "$1"/"*.flv" ]; then    # avoid invalid action with empty glob
    echo "$(date +%F_%T) Nothing to do, no matching files found" | tee -a "$log_file"
    break
fi
if [ "$limit" != "no" ]; then
    (( limit-- ))
    if [ "$limit" -lt 0 ]; then echo "File number limit reached"; break; fi
fi
file="${i##*/}"                  # drop path
dest_file="${file/flv/mp4}"    # replace flv with mp4
[[ -e "./$2"/"$dest_file" ]] && dest_file="${dest_file/.mp4/-1.mp4}" #rename if file exists

"$conv_prog" -y -i "$i" -v quiet -vcodec copy -acodec copy "$2"/"$dest_file"  #&>/dev/null
result="$?"
if [ "$result" != "0" ]; then 
    echo "$(date +%F_%T) Failure $result converting $i" | tee -a "$log_file"
    (( count_fail++ ))
    echo "$file FAILED"
    echo "$i" >> "$PWD"/failed.log
else
    (( count_ok++ ))
    echo "$i converted ok"
    echo "$i" >> "$PWD"/converted.log
    if [ "$4" = "--delete" ]; then
    rm "$i"
    if [ -e "$i" ]; then
        echo "FAILED to delete $i" | tee -a "$log_file"
    else
        echo "$i deleted"
    fi
    fi
fi
done

# Check the way we finished
# Exit can happen only due to all done when no number limit used
if [ "$limit" = "no" ]; then
echo "$(date +%F_%T) $0 all done exit, $count_ok ok, $count_fail failed" | tee -a "$log_file"
exit
fi
# Exit can happen due to all done or number limit reached when limit is used
if [ "$limit" -lt 0 ]; then
echo "$(date +%F_%T) $0 number limit exit, $count_ok ok, $count_fail failed" | tee -a "$log_file"
else
echo "$(date +%F_%T) $0 all done exit, $count_ok ok, $count_fail failed" | tee -a "$log_file"
fi
