All pastes #284667 Raw Edit

Unnamed

public text v1 · immutable
#284667 ·published 2006-12-19 05:40 UTC
rendered paste body
#!/bin/bash

###### Functions ######

function running_total ()
{
    awk -v offset=$offset '{
    for (i=1; i<=NF; i++) if (s=s+$i) printf("%s ",s);printf ("\n")}'
}

function awk_total()
{
    awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}'
}
function abspath()
{
    DIR=$(dirname "$1")
    BASE=$(basename "$1")
    ABS_PATH="`cd \"$DIR\" && pwd || echo \"$DIR\"`/$BASE"
    echo "$ABS_PATH"
}
function get_listargs()
{
    unset x ARGS_ARRAY
    # Hackish list-parsing
    while test $# -gt 0 && test x"${1:0:1}" != x"-"; do
        ARGS_ARRAY[x++]="$1"
        shift
    done
    # Do not skip past the next argument
    if test $# -gt 0 && test x"${1:0:1}" = x"-";then
        DO_SHIFT=false
    fi
}

function vid_length()
{
    mencoder "$1" -quiet -ovc copy -oac pcm -o /dev/null 2>&1 | awk '/Video stream/ {print $10}'
}

###### End Functions #######

###### get options #########

while test $# -gt 0; do
    DO_SHIFT=:
    case "$1" in
        "-files" )
            shift
            get_listargs "$@"
            for f in  ${!ARGS_ARRAY[@]}; do
                FILES[f]="$(abspath "${ARGS_ARRAY[f]}")"
            done
            ;;
        "-chapters" )
            shift
            CHAPTERS=$1
            ;;
    esac
    $DO_SHIFT && shift
done
###### End options ######

###### Execution begins #######


if test ${#FILES[@]} -eq 0 || test ${#CHAPTERS[@]} -eq 0; then
    echo "usage: get_chapters -files FILES -chapters NUMBER_OF_CHAPTERS"
    exit 1
fi

echo "Running mencoder to get video lengths"
echo

for a in ${!FILES[@]}; do
    vid_lens=( ${vid_lens[@]} $(vid_length "${FILES[a]}") )
done
VID_LENS=( ${vid_lens[@]} )
total_length=$( awk_total <<< "${VID_LENS[@]}" )   
echo "Video lengths are ${VID_LENS[@]}"
echo "total_length is $total_length"
chapter_length=$( bc <<< "scale=3; $total_length / $CHAPTERS" )
echo "chapter_length is $chapter_length"
echo
remainder=0
unset x
for vid in ${!VID_LENS[@]}; do
    adjusted_length=$( bc <<< "scale=3; ${VID_LENS[vid]} + $remainder" )
    num_chapters=$( bc <<< "$adjusted_length / $chapter_length" )
    if (( num_chapters > 0)); then
        chapter_length_array=( $( for ((t=0; t<num_chapters; t++)); do
        echo $chapter_length; done) )
        chapter_length_array[0]=$( bc <<< "scale=3; $chapter_length - $remainder" )
        chapter_in_secs=$( running_total <<< ${chapter_length_array[@]} )
        remainder=$( bc <<< "scale=2;  $adjusted_length % $chapter_length" )
        chapters[x++]=$chapter_in_secs
    else
        chapters[x++]=""
        remainder=$adjusted_length
    fi
    unset chapter_length_array
    echo "remainder is $remainder"
done

chapters[0]="0 ${chapters[0]}"

echo
for i in ${!VID_LENS[@]}; do
    echo "video number # $i chapters: ${chapters[i]}"
done