Mint Serena 64 bit using the Mate desktop environment. Most of the following scripts will run on this machine, although some have only been tested with the Gnome desktop. Extra software requirements can be met by the Ubuntu and safe PPA repositories. Scripts may be re-written or added to from time to time, the date serves as the version number. Syntax highlighting on these pages is provided by GVim's "Convert to HTML" option.

FFmpeg with progress bar (re-work)

# File name: ZAVI2MPG - saved in the "nautilus-scripts" directory.
# Author: robz
# Version: 130511
# This is a re-re-work of a previous script for converting .avi television
# files to a "burnable to DVD" format. This time though conversion statistics
# visual output is supplied by the Zenity dialogue. Stats logfile also added.
# File locations in some variables are specific to my PC check "# Vars" first.
# The script is called by right clicking on the ".avi file" then clicking
# the appropriate choice in the nautilus-scripts menu.
# You'll need "FFmpeg" an FFmpeg notify-send icon and a ding-dong.wav sound.

# Vars.

display () # Calculate/collect progress bar info. & pipe to Zenity.
START=$(date +%s); FR_CNT=0; ETA=0; ELAPSED=0
while [ -e /proc/$PID ]; do                         # Is FFmpeg running?
    sleep 2
    VSTATS=$(awk '{gsub(/frame=/, "")}/./{line=$1-1} END{print line}' \
    "$RM"/vstats*)                                  # Parse vstats file.
    if [ $VSTATS -gt $FR_CNT ]; then                # Parsed sane or no?
        PERCENTAGE=$(( 100 * FR_CNT / TOT_FR ))     # Progbar calc.
        ELAPSED=$(( $(date +%s) - START )); echo $ELAPSED > /tmp/elapsed.value
        ETA=$(date -d @$(awk 'BEGIN{print int(('$ELAPSED' / '$FR_CNT') *\
        ('$TOT_FR' - '$FR_CNT'))}') -u +%H:%M:%S)   # ETA calc.
    echo "# Working on file: $COUNT of $OF - Length: $DUR - Frames: $TOT_FR\
    \nFilename: ${NFILE%.*}\nSaved to: $SAVEAS\
    \nTelevision standard is set to: $TFORM2\
    \nRe-coding with FFmpeg, method: $QUAL\
    \nFrame: $FR_CNT of $TOT_FR   Elapsed: $(date -d @$ELAPSED -u\
    +%H:%M:%S)   ET to finish: $ETA"                # Text for stats. output.
    echo $PERCENTAGE                                # Feed the progbar.
done | zenity\
    --window-icon $ICON\
    --title="AVI-2-MPG ${QUAL:10:13}"\
    --text="Initializing please wait..."\

SEL=$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS            # Right clicked selection.
trap "killall ffmpeg ZAVI2MPG; rm -f "$RM/ffmpeg2pass*" "$RM/vstats*"; exit" \
INT TERM EXIT                                       # Kill & clean if stopped.

# Select re-coded file destination.
SAVEAS=$(zenity --file-selection --directory\
    --window-icon $ICON --title ">>SELECT DESTINATION FOR RE-CODED FILE<<")
if [ "$?" = 1 ]; then
    exit $?

# Select quality of re-code.
QUAL=$(zenity --window-icon $ICON --title "AVI-2-MPG"\
    --width 340 --height 170 --list --text "Recode Mode"\
    --radiolist --column "Select " --column "FFmpeg re-code option"\
    FALSE 'One Pass, same Q big file, fast.'\
    TRUE 'Two Pass, same Q smaller file, slow.');
if [ $? = 1 ]; then
    exit $?
elif [ "${QUAL:0:8}" = "One Pass" ]; then
    QUAL="One Pass - Re-coding..."

SAVEIFS=$IFS                                        # Make "for loops" handle
IFS=$(echo -en "\n\b")                              # filenames with spaces.

for FILE in $SEL; do ((OF+=1)); done                    # Count right click
echo -e $(date +%c)" - Files processed: $OF\n" >> $LOG  # selected files, log.

# Loop through counted file selection and process in turn.
for FILE in $SEL; do ((COUNT+=1))
    NFILE=$(basename "$FILE")
    # Get duration and PAL/NTSC fps then calculate total frames.
    FPS=$(ffprobe "$FILE" 2>&1 | sed -n "s/.*, \(.*\) tbr.*/\1/p")
    DUR=$(ffprobe "$FILE" 2>&1 | sed -n "s/.* Duration: \([^,]*\), .*/\1/p")
    HRS=$(echo $DUR | cut -d":" -f1)
    MIN=$(echo $DUR | cut -d":" -f2)
    SEC=$(echo $DUR | cut -d":" -f3)
    TOT_FR=$(echo "($HRS*3600+$MIN*60+$SEC)*$FPS" | bc | cut -d"." -f1)
    if [ ! "$TOT_FR" -gt "0" ]; then zenity --error; exit; fi

    # TV format to FFmpeg friendly.
    TFORM=$(echo "$FPS" |\
        sed "s/25/pal-dvd/g; s/30/pal-dvd/g; s/29.98/ntsc-dvd/g;\
        s/29.97/ntsc-dvd/g; s/23.98/ntsc-dvd/g;")
    TFORM2=$(echo "$FPS" |\
        sed "s/25/PAL @ 25fps/g; s/30/PAL @ 30fps/g;\
        s/29.98/NTSC @ 29.98fps/g; s/29.97/NTSC @ 29.97fps/g;\
        s/23.98/NTSC-Film @ 23.98fps/g;")

    # Re-code with it.
    if [ "$QUAL" = "One Pass - Re-coding..." ]; then    # Same Q big file.
        nice -n 15 ffmpeg -i "$FILE"\
        -target $TFORM\
        -r $FPS\
        $SAVEAS/${NFILE%.*}.mpg &                       # Background FFmpeg.
        PID=$! && display                               # GUI stats. func.
        rm -f "$RM"/vstats*                             # Clean up tmp files.
        QUAL="Two Pass - Analysing..."                  # Analyse video only.
        nice -n 15 ffmpeg -i "$FILE"\
        -pass 1\
        -target $TFORM\
        -r $FPS\
        -b 2800k\
        /dev/null &                                     # Background FFmpeg.
        PID=$! && display                               # GUI stats. func.
        rm -f "$RM"/vstats*                             # Clean up.
        AN_TM=$(cat /tmp/elapsed.value)                 # Get analyse time.
        ((BATCH+=$AN_TM))                               # Batch time totaling.
        QUAL="Two Pass - Re-coding..."                  # Do smaller re-code.
        nice -n 15 ffmpeg -i "$FILE"\
        -pass 2\
        -target $TFORM\
        -r $FPS\
        -b 2800k\
        -acodec ac3\
        -ar 48000\
        -ab 128k\
        $SAVEAS/${NFILE%.*}.mpg &                       # Background FFmpeg.
        PID=$! && display                               # GUI stats. func.
        rm -f "$RM"/ffmpeg2pass* "$RM"/vstats*          # Clean up tmp files.
    # Statistics for logfile entry.
    ((BATCH+=$(cat /tmp/elapsed.value)))                # Batch time totaling.
    ELAPSED=$(cat /tmp/elapsed.value)                   # Per file time.
    echo -e $COUNT\. ${NFILE%.*}\
    "\nDuration: $DUR - Total frames: $TOT_FR\
    \nCoded to $TFORM2 standard using \"${QUAL:0:8}\" option." >> $LOG
    # Elapsed time for 1 pass, average for 2 passes, fps average for both.
    if [ "${QUAL:0:8}" = "Two Pass" ]; then
        ((ELAPSED+=$AN_TM)); AV_RATE=$(( TOT_FR * 2 / ELAPSED ))
        AV_RATE=$(( TOT_FR / ELAPSED ))
    echo -e "Re-coding time taken: $(date -d @$ELAPSED -u +%H:%M:%S)"\
    "at an average rate of $AV_RATE""fps.\n" >> $LOG

# Notify finished batch ding-dong gong and message, log it, all done bye ;¬)
TEXT="Total re-coding time: $(date -d @$BATCH -u +%H:%M:%S)."
echo -e "$TEXT\n\
__________________________________________________________________" >> $LOG
notify-send -i $ICON "ZAVI2MPG Re-code completed" "$TEXT"
/usr/bin/canberra-gtk-play --volume 2 -f $HOME/Audio/2tone.wav

No comments: