Skip to content

Instantly share code, notes, and snippets.

@brucewoodward
Created October 12, 2017 21:41

Revisions

  1. brucewoodward created this gist Oct 12, 2017.
    60 changes: 60 additions & 0 deletions vlc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/bin/bash

    VLCTMP=/tmp/vlc.out
    QUIET=false

    log_vlc() {
    echo $(date +%s) "$@" >> ~/.vlc
    }

    run_vlc() {
    open -a vlc "$@"
    }

    not_valid_file_name() {
    test -e "$1" && return 1 || return 0
    }

    size_check() {
    local count=$(tee "$VLCTMP" | wc -l)
    if ((count==0))
    then
    echo "Nothing found" 1>&2
    perl -e "kill -15, $$" # why is Perl being used for this?
    else
    cat "$VLCTMP"
    fi
    }

    run_selecta() {
    local wanted_file_extensions='\.(mkv|avi|mp4|mov|m4v|wmv)$'
    echo "`find . | grep -i "$1" | egrep -i "${wanted_file_extensions}" | size_check | selecta -a`"
    }

    die() {
    echo $1
    exit 1
    }

    rm -f "$VLCTMP"

    # -q as first arg to stop the logging of the file being watched.
    if [[ $# -gt 0 && "$1" = -q ]]
    then
    QUIET=true
    shift
    fi
    test $# -eq 0 && die "Missing arg(s)"

    # arg isn't a file so use selecta to get the user to pick a file
    # to display with vlc.
    if not_valid_file_name "$1"; then
    # Use find and grep to come up with a list of files for selecta.
    files_for_vlc="$( run_selecta "$1" )"
    test -z "$files_for_vlc" && exit 0 # no files selected.
    else # full filename has been given.
    files_for_vlc="$@"
    fi

    test "$QUIET" = false && log_vlc "$files_for_vlc"
    run_vlc "$files_for_vlc"