Playing random videos on one's system; minor improvement to include DVDs

In my .bashrc I have a function which I use to play random videos (not written below, just fyi), and another one for all media files, e.g.:

createmediafiles() {
find ~+ -type f -iregex '.*.(mp3|wav|ogg|flac|mp4|mov|avi)' > ~/mediafiles.txt
find ~+ -type d -name VIDEO_TS >> ~+/mediafiles.txt
}

playmediafiles() {
    while true; do
        while read -r line; do
            shuf -n 1 |
            tee -a ~/played-log.txt |
            xargs -d "n" mpv
        done < ~/mediafiles.txt
    done
}

I want to add the VIDEO_TS line to createmediafiles, but in the playmediafiles function I want to add an IF statement, such that if the line shuffled to in mediafiles.txt is a VIDEO_TS then rather than using mpv to play a file, it will do:

tee -a ~/played-log.txt | xargs -d "n" vlc --fullscreen

Piping in mediafiles.txt read the same way as it does with mpv.

How do I throw that in an IF statement with the condition being that the line ends in VIDEO_TS?

Asked By: 1toneboy

||

I think you might be trying to do something like this:

playmediafiles() {
    local line cmd

    while true; do
        line=$(shuf -n 1 ~/mediafiles.txt)
        case $line in
            *VIDEO_TS* ) cmd=( 'echo' 'vlc' '--fullscreen' ) ;;
            * )          cmd=( 'echo' 'mpv' ) ;;
        esac
        cmd+=( "$line" )
        printf '%sn' "$line" >> ~/played-log.txt
        "${cmd[@]}"
    done
}

or:

playmediafiles() {
    local line cmd

    while true; do
        while IFS= read -r line; do
            case $line in
                *VIDEO_TS* ) cmd=( 'echo' 'vlc' '--fullscreen' ) ;;
                * )          cmd=( 'echo' 'mpv' ) ;;
            esac
            cmd+=( "$line" )
            printf '%sn' "$line" >> ~/played-log.txt
            "${cmd[@]}"
        done < <(shuf ~/mediafiles.txt)
    done
}

The first script loops forever calling shuf to select 1 line at a time at random from mediafiles.txt and store that line in the variable line.

The second script loops forever calling shuf to print the entire contents of mediafiles.txt in a random order, then has a second loop to read 1 line at a time from the shuf output and store that line in the variable line.

Both then test $line to see if it contains VIDEO_TS or not and populate the cmd array with the appropriate command plus arguments using cmd=( 'echo' 'vlc' '--fullscreen' ) or cmd=( 'echo' 'mpv' ). They then add the contents of line to the end of the array using cmd+=( "$line" ) and then print the contents of the array to played-log.txt and then execute the command stored in the array with "${cmd[@]}".

The only difference between the 2 is the first spawns a subshell to call shuf once per file to be played while the 2nd spawns a subshell to call shuf once for the whole of the input file and then read once per line to be played. The second one will probably run a bit faster but I doubt if you’ll notice it or care since you’re playing media between each line of input.

Remove the 'echo's when done testing to make sure it does what you want.

Answered By: Ed Morton
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.