Skip to content

Instantly share code, notes, and snippets.

@debanjum
Last active February 3, 2016 00:56
Show Gist options
  • Save debanjum/9468178 to your computer and use it in GitHub Desktop.
Save debanjum/9468178 to your computer and use it in GitHub Desktop.
Opens movie at position at which searched dialog occurs
#!/bin/bash
#**************************************************************************#
# Dialog Seek.sh #
# by #
# Debanjum Singh Solanky #
# #
# Copyright (C) 2016 Debanjum Singh Solanky #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
#**************************************************************************#
#**************************************************************************#
# RUN #
# ------------------------------------------------------------------------ #
# Jump to Dialog Location in Movie using Subtitle Search #
# ./DialogSeek.sh "Dialog" Movie Subtitle(Optional) #
#**************************************************************************#
#**************************************************************************#
# DEPENDENCIES #
# ------------------------------------------------------------------------ #
# VLC[rc on], BASH, pcregrep, zenity[for gui] #
# VLC with remote control on & unix sock @ /tmp/vlc.sock #
#**************************************************************************#
# INITIALISING VARIABLES
VLC_RC_SOCKET="/tmp/vlc.sock" #VLC RC Unix Socket Path
# VALID ARGUMENTS
# Script called with subtitle path
[ $# -eq 3 ] && (
# Search & select dialog time
pcregrep -MB1 "^.*?(\n|.).*?(\n|.).*?$1.*?(\n|.).*?$" "$3" > vlcsubtemp
# Clean up selected dialog times
pcregrep ".*\>.*[0-9][0-9]:.*" vlcsubtemp >vlcsubtemp1
sed 's/,.*//' vlcsubtemp1 > start
sed 's/.*> //' vlcsubtemp1 > vlcsubtemp
sed 's/,.*//' vlcsubtemp > stop
# Storing dialog times from temporary file in array
IFS=$'\r\n' Dialog=($(cat start))
# Clean Up
rm vlcsubtemp vlcsubtemp1 start stop
# Convert time to seconds & jump to dialog time in VLC
i=0
ret=0
seek=$(echo ${Dialog[$i]} | tr ',' '.' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
# Start VLC @ First Searchterm Instance
vlc --quiet --fullscreen --start-time=$(date +'%s' -d "1970-01-01 ${Dialog[$i]}Z") "$2" --sub-file "$3" 2 &
sleep 1 # Delay for vlc to startup
# Main SearchTerm based Video Traversal Loop
while [ $i -gt -1 ] && [ $i -lt $(( ${#Dialog[@]}-1 )) ]
do
# Wait Till Enter to Jump to Next
read -p "Press [Enter] to jump to next occurence or [Ctrl+C] to exit"
i=$(( $i+1 ))
# Convert Dialog_to_seek to seconds & jump to the dialog time in video
seek=$(echo ${Dialog[$i]} | tr ',' '.' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
echo -n "seek $seek" | nc -U $VLC_RC_SOCKET
done
)
# Commandline w/o Subtitle Path [assumed default subtitle path = Moviepath.srt]
[ $# -eq 2 ] && (
# Extract subtitle filename
filename=$(basename "$2")
subpath="${2%.*}.srt"
# AutoDownload Subtitles if not present
[ ! -f "$subpath" ] && Subtitles.py -g cli -a "$2"
# Search & select dialog time
pcregrep -MB1 "^.*?(\n|.).*?(\n|.).*?$1.*?(\n|.).*?$" "$subpath" > vlcsubtemp
# Clean up selected dialog times
pcregrep ".*\>.*[0-9][0-9]:.*" vlcsubtemp >vlcsubtemp1
sed 's/,.*//' vlcsubtemp1 > start
sed 's/.*> //' vlcsubtemp1 > vlcsubtemp
sed 's/,.*//' vlcsubtemp > stop
# Storing dialog times from temporary file in array
IFS=$'\r\n' Dialog=($(cat start))
# Clean Up
rm vlcsubtemp vlcsubtemp1 start stop
# Convert time to seconds & jump to dialog time in VLC
i=0
ret=0
seek=$(echo ${Dialog[$i]} | tr ',' '.' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
# Start VLC @ First Searchterm Instance
vlc --quiet --fullscreen --start-time=$(date +'%s' -d "1970-01-01 ${Dialog[$i]}Z") "$2" --sub-file "$subpath" 2 &
sleep 1 # Delay for vlc to startup
# Main SearchTerm based Video Traversal Loop
while [ $i -gt -1 ] && [ $i -lt $(( ${#Dialog[@]}-1 )) ]
do
# Wait Till Enter to Jump to Next
read -p "Press [Enter] to jump to next occurence or [Ctrl+C] to exit"
i=$(( $i+1 ))
# Convert Dialog_to_seek to seconds & jump to the dialog time in video
seek=$(echo ${Dialog[$i]} | tr ',' '.' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
echo -n "seek $seek" | nc -U $VLC_RC_SOCKET
done
)
# GUI based
[ $# -eq 1 ] && (
# Extract video, subtitle filepath
filepath=`echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | tr -d '\n'`
subpath="${filepath%.*}.srt"
# AutoDownload Subtitles if not present
[ ! -f "$subpath" ] && Subtitles.py -g cli -a "$filepath"
# SearchTerm Input DialogBox
search=$(zenity --title="Dialog Seek $subpath" --text "Enter Dialog to Search:" --entry)
[ $? -gt 0 ] && exit 0
# Searching & selecting dialog time(s) in Movie
pcregrep -MB1 "^.*?(\n|.).*?(\n|.).*?$search.*?(\n|.).*?$" "$subpath" > vlcsubtemp
# Clean up selected dialog times
pcregrep ".*\>.*[0-9][0-9]:.*" vlcsubtemp > vlcsubtemp1
sed 's/,.*//' vlcsubtemp1 > start
sed 's/.*> //' vlcsubtemp1 > vlcsubtemp
sed 's/,.*//' vlcsubtemp > stop
# Storing dialog times from temporary file in array
IFS=$'\r\n' Dialog=($(cat start))
# Clean Up
rm vlcsubtemp vlcsubtemp1 start stop
# Convert time to seconds & jump to dialog time in VLC
i=0
ret=0
seek=$(echo ${Dialog[$i]} | tr ',' '.' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
# Start VLC @ First Searchterm Instance
vlc --quiet --fullscreen --start-time=$(date +'%s' -d "1970-01-01 ${Dialog[$i]}Z") $NAUTILUS_SCRIPT_SELECTED_URIS --sub-file "$subpath" 2 &
sleep 1 # Delay for vlc to startup
# Main SearchTerm based Video Traversal Loop
while [ $i -gt -1 ] && [ $i -lt $(( ${#Dialog[@]}-1 )) ]
do
# Select jump to next_instance/previous_instance/exit_script(if dialog box times out(=5s))
# Dialog box
zenity --question --title="Dialog Seek: \"$search\"" --text="Jump To Dialog:" --ok-label="Next" --cancel-label="Previous" --timeout=5
# Evaluating Choice
ret=$?
[ $ret -eq 5 ] && exit 0
[ $ret -eq 1 ] && i=$(( $i-1 ))
[ $ret -eq 0 ] && i=$(( $i+1 ))
# Convert Dialog_to_seek to seconds & jump to the dialog time in video
seek=$(echo ${Dialog[$i]} | tr ',' '.' | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
echo -n "seek $seek" | nc -U $VLC_RC_SOCKET
done
)
# INVALID ARGUMENTS
[[ $# -lt 1 ]] || [[ $# -gt 3]] && (
echo -e "Invalid Argument(s)\nCommand Format: $0 \"Dialog\" \"Movie\" <\"Subtitle\">\nExample 1: $0 \"Mr. Anderson\" \"The Matrix.mkv\"\nExample 2: $0 \"Mr. Anderson\" \"The Matrix.mkv\" \"The Matrix.srt\"")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment