#!/bin/bash

#find "$(pwd -P)" -type f -name "*.mp3" (gives absolute path)
#https://manpages.ubuntu.com/manpages/bionic/man1/ffplay-all.1.html

export TEXTDOMAIN=ffplay-gtk
export OUTPUT_CHARSET=UTF-8

version=1.6

currentpid=`echo $$`

kill -9 `pidof ffplay` 2>/dev/null
kill -9 `pidof ffplay-gtk-timer` 2>/dev/null
ps aux | grep FFPLAY_GTK | grep -v grep | tr -s ' ' | cut -f 2 -d ' ' | grep -v ^$currentpid$ | while read tokill; do
	kill -9 $tokill 2>/dev/null
done

who=`whoami`
tmpfile=/tmp/$who-ffplay-gtk
if [ -f "$tmpfile"/user-playlist ]; then	
	find $tmpfile/ -type f -not -name playlist -not -name user-playlist -delete
else	
	rm -r $tmpfile 2>/dev/null
fi
mkdir $tmpfile 2>/dev/null
touch $tmpfile/human #if file doesn't exist immediately when gui loads it will never load once it appears
touch $tmpfile/chapter-title #same as above
touch $tmpfile/file_height #same as above
touch $tmpfile/file_width #same as above
touch $tmpfile/win
touch $tmpfile/timecount

if [ "`which ffplay`" = "" ] || [ "`which ffmpeg`" = "" ]; then
	echo $(gettext 'you need to install ffmpeg and ffplay from the ffmpeg package')
	exit
fi

##find a suitable version of gtkdialog (needs 0.7.20 or greater) currently (as of puppy 4 and maybe before)
##the executable is named gtkdialog3. Exit with a warning incase gtkdialog isn't found.
GTKDIALOG=""
if [ "`which gtkdialog4`" != "" ]; then
	GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" != "" ]; then
	GTKDIALOG=gtkdialog3
elif [ "`which gtkdialog`" != "" ]; then
	GTKDIALOG=gtkdialog
elif [ "$GTKDIALOG" = "" ]; then
	echo $(gettext 'you need to install gtkdialog')
	exit 1
fi

audiosource=default
if [ -f $HOME/.ffplay-gtk ]; then
	audiosource=`cat $HOME/.ffplay-gtk | grep ^audiosource= | cut -f 2 -d '='`
fi
rm -f $HOME/.ffplay-gtk 2>/dev/null

ffsources=`ffmpeg -sources alsa 2>&1`
if [ "$audiosource" != "" ]; then
	echo "$audiosource" > $tmpfile/alsasources
fi
echo "$ffsources" | grep -A 999 ^'Auto-detected sources' | grep -v ^'Auto-detected sources' | awk '{$1=$1};1' | sed 's/^\*\ //' | cut -f 1 -d ' ' | cut -f 1 -d ':' | grep -v ^$audiosource$ | sort -u >> $tmpfile/alsasources

dibox=false

SOURCES=""
for ONESOURCE in `cat $tmpfile/alsasources`
do
	SOURCES="$SOURCES<item>$ONESOURCE</item>"
done

if ! [ -f "$1" ]; then
			export FFPLAY_GTK="
		<window title=\"ffplay-gtk $version\"icon-name=\"gtk-media-play\" resizable=\"true\">
				<frame $(gettext 'Select a media file supported by ffmpeg to play, a playlist of files with absolute paths, or a mounted DVD VIDEO_TS directory')>
				<hbox>
  			   <chooser>
    <height>500 resizable=\"true\"</height><width>850 resizable=\"true\"</width>
    <variable>file_entry</variable>
  </chooser>	
					</hbox>
				
					<hbox>
					<button>
						<label>$(gettext 'get video information')</label>
						<action>ffplay-gtk-timer ffmpeg \"\$file_entry\"</action>
						<action>REFRESH:file_width</action>
						<action>REFRESH:file_height</action>
					</button>
				<text><label>$(gettext 'width')</label></text>	
				<entry>
				<variable>file_width</variable>
				<input file>$tmpfile/file_width</input>
				</entry>		
				<text><label>$(gettext 'height')</label></text>	
				<entry>
				<variable>file_height</variable>
				<input file>$tmpfile/file_height</input>
				</entry>				
					
					</hbox>
					
					<hbox>
					
					<text><label>$(gettext 'audio output')</label></text>
					<comboboxtext>
					<variable>audiosource</variable>
					<visible>enabled</visible>
					$SOURCES
					</comboboxtext>
					
						<checkbox>
						<label>$(gettext 'disable subtitles')</label>
						<variable>subbox</variable>
						<default>false</default>
						</checkbox>
						
						<checkbox>
						<label>$(gettext 'de-interlace')</label>
						<variable>dibox</variable>
						<default>false</default>
						</checkbox>
						
					<button tooltip-text=\"$(gettext 'ok')\">
					<input file icon=\"ffplay-yes\"></input>
					<action type=\"exit\">Exit-now</action>
				</button>

					<button tooltip-text=\"$(gettext 'quit')\">
						<input file icon=\"ffplay-quit\"></input>
								<action type=\"exit\">KILL_THIS_SCRIPT</action>
					</button>
			
				</hbox>
				
				</frame>
			</window>
		"
	results="`$GTKDIALOG --program=FFPLAY_GTK`"
	
	if [ "`echo $results | grep KILL_THIS_SCRIPT`" != "" ]; then
		exit
	fi

	if [ "`echo $results | grep EXIT | grep abort`" != "" ]; then
		exit
	fi

	r2=`echo "$results" | sed 's/=\"/|\"/'`	
	filetoplay=`echo "$r2" | grep ^file_entry\| | cut -f 2 -d '|' | sed 's/\"//g'`
	audiosource=`echo "$r2" | grep ^audiosource\| | cut -f 2 -d '|' | sed 's/\"//g'`
	echo audiosource="$audiosource" > $HOME/.ffplay-gtk
	subbox=`echo "$r2" | grep ^subbox\| | cut -f 2 -d '|' | sed 's/\"//g'`
	dibox=`echo "$r2" | grep ^dibox\| | cut -f 2 -d '|' | sed 's/\"//g'`
	if [ "$dibox" = true ]; then
		di="-vf yadif"
	else
		di=""
	fi
	if [ "$subbox" = true ]; then
		subbox="-sn"
	else
		subbox=""
	fi
	file_width=`echo "$r2" | grep ^file_width\| | cut -f 2 -d '|' | sed 's/\"//g' | tr -c -d [0-9]`
	file_height=`echo "$r2" | grep ^file_height\| | cut -f 2 -d '|' | sed 's/\"//g' | tr -c -d [0-9]`
	rm -f $tmpfile/user-playlist 2>/dev/null
	rm -f $tmpfile/playlist 2>/dev/null

fi

if [ "$filetoplay" = "" ]; then
	filetoplay="$1"
fi

if [ -d "$filetoplay" ]; then
	if [ "`echo $filetoplay | grep VIDEO_TS`" != "" ]; then
		echo $(gettext 'playing DVD')
		dvd=yes
	else
		echo $(gettext 'VIDEO_TS directory not selected')
		exit
	fi
else
	if ! [ -f "$filetoplay" ]; then
		echo "$filetoplay" $(gettext 'is not a file')
		exit
	fi
fi

if [ "`file "$filetoplay" | grep text$`" != "" ]; then
	cat "$filetoplay" | while read line; do
		if [ -f "$line" ]; then
			echo "$line" >> $tmpfile/playlist
		fi
	done
	filetoplay=`cat $tmpfile/playlist | head -n 1`
	touch $tmpfile/user-playlist
else
	if [ -f "$tmpfile"/user-playlist ]; then
		playlistfile=`cat $tmpfile/playlist | grep "$filetoplay"`
		if ! [ -f "$playlistfile" ]; then
			rm -f $tmpfile/user-playlist 2>/dev/null
			rm -f $tmpfile/playlist 2>/dev/null
		fi
	fi
fi

width=400
height=150

file=`basename "$filetoplay"`

echo 00:00 > $tmpfile/human 2>/dev/null

if [ "$2" != "" ]; then
	timetostart="$2"
else
	timetostart=0
fi

if [ "$dvd" = yes ]; then
	dvdinfo=`vobcopy -i "$filetoplay" -I 2>&1`
	dvdname=`echo "$dvdinfo" | grep DVD-name | cut -f 2 -d ':' | sed 's/^\ //'`
	dvd_titles_numchapters=`echo "$dvdinfo" | grep ' chapter' | grep -v Most | grep -v angles | grep -v dvd | sed 's/\[Info\]\ Title\ //' | sed 's/\ has\ /|/' | cut -f 1 -d ' '`
	dvdnum=`echo "$dvd_titles_numchapters" | wc -l`
	dvd_main_title=`echo "$dvdinfo" | grep 'Most chapters' | sed 's/title\ /|/' | sed 's/\ with/|/' | cut -f 2 -d '|'`
	x=1
	while [ "$x" -le "$dvdnum" ]; do
		dvdstreams=`vobcopy -i "$filetoplay" -n $x -o - -v -l -f | ffmpeg -i - 2>&1`
		dvdall=`echo "$dvdstreams" | grep 'Stream #' | grep -v dvd_nav_packet | sed 's/]:\ /|/' | sed 's/Stream\ #//' | sed 's/\[/|/' | sed -e 's/^[ \t]*//'`
		echo "$dvdall" | grep Video: | cut -f 1 -d '|' | head -n 1 > $tmpfile/dvdvideo-$x
		echo "$dvdall" | grep Audio: | sed 's/Audio:\ //' > $tmpfile/dvdaudio-$x
		echo "$dvdall" | grep Subtitle: | cut -f 1 -d '|' > $tmpfile/dvdsub-$x
		x=`expr $x + 1`
	done

	if [ "$dvdnum" -gt 1 ]; then
		export RIPGUI="
		<window title=\"ffplay-gtk-$version \"icon-name=\"gtk-cdrom\">
		<vbox scrollable=\"true\" width=\"320\" height=\"720\" resizable=\"true\">

		<frame $(gettext 'DVD name')>
		<entry editable=\"false\">
		<variable>dvdname</variable>
		<input>echo $dvdname</input>
		</entry>
		</frame>
			"
		x=1
		while [ "$x" -le "$dvdnum" ]; do
			c1=`echo "$dvd_titles_numchapters" | sed -n "$x"p | cut -f 1 -d '|'`
			c2=`echo "$dvd_titles_numchapters" | sed -n "$x"p | cut -f 2 -d '|'`
			if [ "$x" = "$dvd_main_title" ]; then
				y=true
			else
				y=false
			fi
			RIPGUI="$RIPGUI
			<radiobutton>
			<label>$(gettext 'Play: Title') $c1 $(gettext 'contains') $c2 $(gettext 'chapters')</label>
			<variable>rb-$x</variable>
			<default>$y</default>
			</radiobutton>
			"
			x=`expr $x + 1`
		done
		RIPGUI="$RIPGUI
		<hbox>
		<button tooltip-text=\"$(gettext 'Play')\">
		<input file icon=\"ffplay-yes\"></input>
        <action type=\"exit\">EXIT_NEXT</action>
		</button>
		<button tooltip-text=\"$(gettext 'quit')\">
		<input file icon=\"ffplay-quit\"></input>
        <action type=\"exit\">EXIT_NOW</action>
		</button>
		</hbox>
		</vbox>
		</window>
		"
		results=`$GTKDIALOG --program=RIPGUI --center`
		r2=`echo "$results" | sed 's/=\"/|\"/'`
		if [ "`echo $r2 | grep EXIT | grep abort`" != "" ] || [ "`echo $results | grep EXIT_NOW`" != "" ]; then
			echo $(gettext 'abort')
			exit
		fi
		dvd_title_to_play=`echo "$r2" | grep ^rb | grep true | cut -f 2 -d '-' | cut -f 1 -d '|'`
	else
		dvd_title_to_play="$dvd_main_title"
	fi
	dvdaudiostreamsnum=`cat $tmpfile/dvdaudio-$dvd_title_to_play | wc -l`
	dvdsubstreamsnum=`cat $tmpfile/dvdsub-$dvd_title_to_play | wc -l`
	

	export RIPGUI="
	<window title=\"ffplay-gtk-$version \"icon-name=\"gtk-cdrom\">
	<vbox scrollable=\"true\" width=\"500\" height=\"720\" resizable=\"true\">
	<frame $(gettext 'Selected Title')>
	<entry editable=\"false\">
	<variable>dvdtitle</variable>
	<input>echo $dvdname - $dvd_title_to_play</input>
	</entry>
	</frame>
	<frame $(gettext 'Select Audio Stream to play')>
	"
	x=1
	while [ "$x" -le "$dvdaudiostreamsnum" ]; do
		c1=`cat $tmpfile/dvdaudio-$dvd_title_to_play | sed -n "$x"p | cut -f 1 -d '|'`
		c2=`cat $tmpfile/dvdaudio-$dvd_title_to_play | sed -n "$x"p | cut -f 3 -d '|'`
		if [ "$x" = 1 ]; then
			y=true
		else
			y=false
		fi
		RIPGUI="$RIPGUI
		<radiobutton>
		<label>$(gettext 'Play Audio Stream'): $c1 - $c2</label>
		<variable>rb1-$x</variable>
		<default>$y</default>
		</radiobutton>
		"
		x=`expr $x + 1`
	done
	RIPGUI="$RIPGUI
	</frame>
	<frame $(gettext 'Select Subtitle to play')>
	<radiobutton>
	<label>$(gettext 'No Subtitles')</label>
	<variable>rb2-0</variable>
	<default>true</default>
	</radiobutton>
	"
	x=1
	while [ "$x" -le "$dvdsubstreamsnum" ]; do
		c1=`cat $tmpfile/dvdsub-$dvd_title_to_play | sed -n "$x"p`
		RIPGUI="$RIPGUI
		<radiobutton>
		<label>$(gettext 'Play Subtitle Stream'): $c1</label>
		<variable>rb2-$x</variable>
		<default>false</default>
		</radiobutton>
		"
		x=`expr $x + 1`
	done
	RIPGUI="$RIPGUI
	</frame>	
	<hbox>
	<checkbox>
	<label>$(gettext 'De-Interlace Video')</label>
	<variable>cbox</variable>
	<default>$dibox</default>
	</checkbox>
	<button tooltip-text=\"$(gettext 'Play')\">
	<input file icon=\"ffplay-yes\"></input>
    <action type=\"exit\">EXIT_NEXT</action>
    </button>
	<button tooltip-text=\"$(gettext 'quit')\">
	<input file icon=\"ffplay-quit\"></input>
    <action type=\"exit\">EXIT_NOW</action>
    </button>
    </hbox>
    </vbox>
    </window>
    "
	results=`$GTKDIALOG --program=RIPGUI --center`
	r2=`echo "$results" | sed 's/=\"/|\"/'`
	if [ "`echo $r2 | grep EXIT | grep abort`" != "" ] || [ "`echo $results | grep EXIT_NOW`" != "" ]; then
		echo $(gettext 'abort')
		exit
	fi
	cbox=`echo "$results" | grep cbox | grep =\"true\"`
	if [ "$cbox" != "" ]; then
		di="-vf yadif"
	fi
	dvd_audio=`echo "$results" | grep rb1 | grep =\"true\" | sed 's/=/-/' | cut -f 2 -d '-'`
	dvd_sub=`echo "$results" | grep rb2 | grep =\"true\" | sed 's/=/-/' | cut -f 2 -d '-'`
	dvd_video_stream=`cat $tmpfile/dvdvideo-$dvd_title_to_play`
	dvd_audio_stream=`cat $tmpfile/dvdaudio-$dvd_title_to_play | sed -n "$dvd_audio"p | cut -f 1 -d '|'`
	dvd_sub_stream=`cat $tmpfile/dvdsub-$dvd_title_to_play | sed -n "$dvd_sub"p`
	ffmpegcheck=`ffmpeg 2>&1`
	if [ "`echo $ffmpegcheck | grep 'enable-libx265'`" != "" ]; then
		vcodec=libx265 #video codec to use 
	else
		vcodec=libx264
	fi
	if [ "$dvd_sub_stream" = "" ]; then
		vobcopy -i "$filetoplay" -n $dvd_title_to_play -o - -v -l -f | ffmpeg -i - -map $dvd_audio_stream -codec:a copy -map $dvd_video_stream -codec:v $vcodec $di -f matroska - | SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -i - 2> $tmpfile/timecount &
	else
		vobcopy -i "$filetoplay" -n $dvd_title_to_play -o - -v -l -f | ffmpeg -i - -map $dvd_sub_stream -codec:s copy -map $dvd_audio_stream -codec:a copy -map $dvd_video_stream -codec:v $vcodec $di -f matroska - | SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -i - 2> $tmpfile/timecount &
	fi
	ffplay_pid=$!
	sleep 0.1
	if [ "`which xdotool`" != "" ]; then
		win=`xdotool search --all --name --pid $ffplay_pid ffplay 2>/dev/null`
		if [ "$win" = "" ]; then
			sleep 0.2
			win=`xdotool search --pid $ffplay_pid 2>/dev/null`
		fi
	fi
	ffplay-gtk-timer 0 925000 &
	fgt=$!
	export FFPLAY_GTK='
	<window title="ffplay-gtk '$version' "icon-name="gtk-media-play" resizable="true" scrollable="true">
	<hbox>
	<vbox>
	<hbox>
	<entry editable="false" file-monitor="true" auto-refresh="true">
	<variable>timeentry</variable>
	<input file>'$tmpfile'/human</input>
	<action type="refresh">timeentry</action>
	</entry>				
	'
	if [ "`which xdotool`" != "" ]; then
		FFPLAY_GTK=$FFPLAY_GTK'			
		<button tooltip-text="'$(gettext 'play / pause')'">
		<input file icon="ffplay-pause-play"></input>
		<action>xdotool key --clearmodifiers --window '$win' p</action>
		</button>			
		<button tooltip-text="'$(gettext 'full screen toggle, if full screen goes over your dock bar hit the f key to get back to the gui')'">
		<input file icon="ffplay-fullscreen"></input>
		<action>xdotool key --clearmodifiers --window '$win' f</action>
		</button>
		'
	fi
	FFPLAY_GTK=$FFPLAY_GTK'
	<button tooltip-text="'$(gettext 'quit')'">
	<input file icon="ffplay-quit"></input>
	<action>kill -9 `pidof ffplay` 2>/dev/null</action>
	<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
	<action type="exit">EXIT_NOW</action>
	</button>			
	</hbox>
	</vbox>
	</hbox>
	</window>
	'
	results="`$GTKDIALOG --program=FFPLAY_GTK  --geometry +650+780`" 
	kill -9 `pidof ffplay` 2>/dev/null
	exit
fi

ff=`ffmpeg -i "$filetoplay" 2>&1`
meta=`echo "$ff" | grep -A 99 'Metadata:' | grep -v 'Metadata:' | grep -v 'At least one output file must be specified' | grep -v 'Side data:' | grep -v "Cover (front)" | grep 'Chapter #' -A 2 | sed 's/,\ end/|/' | sed 's/start\ /|/' | cut -f 2 -d '|' | awk '{$1=$1};1' | sed 's/title\ :\ //' | grep -v ^'Stream #'`
if  [ "$meta" != "" ]; then
	echo "$meta" | while read line; do
		d1=`date -u -d @"$line" +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
		if [ "$d1" != "" ]; then
			t1=`echo "$line" | cut -f 1 -d '.'`
			title=`echo "$meta" | grep "$line" -A 1 | grep -v "$line" | head -n 1 | sed 's/\ /_/g'`
			if [ "`echo $t1 | grep -i [A-Z]`" = "" ]; then
				echo "$d1"\|"$t1"\|"$title" >> $tmpfile/specifics
			fi
		fi
	done
fi

meta2=`echo "$ff" | grep -A 99 'Input #' | grep -v 'Metadata:' | grep -v 'Input #' | grep -v 'At least one output file must be specified' | grep -v 'Side data:' | grep -v "Cover (front)" | awk '{$1=$1};1'`
if [ "$file_width" = "" ] || [ "$file_height" = "" ]; then
	#size=`echo "$meta2" | grep 'Stream #' | grep Video: | grep -v mjpeg | grep -v ' png,' | sed 's/Stream\ #[0-9]:[0-9]:\ Video:\ //' | sed 's/\ \[/|/' | cut -f 1 -d '|' | sed 's:.*,::' | tr -c -d [0-9]`
	size=`ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 "$filetoplay"`
	file_height=`echo $size | cut -f 2 -d 'x'`
	file_width=`echo $size | cut -f 1 -d 'x'`
	echo $file_width > $tmpfile/file_width
	echo $file_height > $tmpfile/file_height
fi
audiostreams=`echo "$meta2" | grep 'Stream #' | grep 'Audio:' | sed 's/Stream\ #//' | sed 's/:\ Audio:\ /|/'`
videostreams=`echo "$meta2" | grep 'Stream #' | grep 'Video:' | sed 's/Stream\ #//' | sed 's/:\ Video:\ /|/'`
subtitlestreams=`echo "$meta2" | grep 'Stream #' | grep 'Subtitle:' | sed 's/Stream\ #//' | sed 's/:\ Subtitle:\ /|/'`
astreamnum=`echo "$audiostreams" | wc -l`
vstreamnum=`echo "$videostreams" | wc -l`
sstreamnum=`echo "$subtitlestreams" | wc -l`
if [ "$subtitlestreams" = "" ]; then
	sstreamnum=0
fi
if [ "$audiostreams" = "" ]; then
	astreamnum=0
fi
if [ "$videostreams" = "" ]; then
	vstreamnum=0
fi
if [ "$astreamnum" -gt 1 ] || [ "$vstreamnum" -gt 1 ] || [ "$sstreamnum" -gt 1 ]; then
	export RIPGUI="
	<window title=\"ffplay-gtk-$version $(gettext 'stream selections')\"icon-name=\"gtk-media-play \"resizable=\"true\">
	<vbox scrollable=\"true\" width=\"950\" height=\"600\" resizable=\"true\">
	
	<frame $(gettext 'choose the video stream you want to use')>
	"
	x=1
	while [ "$x" -le "$vstreamnum" ]; do
		v1=`echo "$videostreams" | sed -n "$x"p | cut -f 1 -d '|'`
		v2=`echo "$videostreams" | sed -n "$x"p | cut -f 2 -d '|'`
		if [ "$x" = 1 ]; then
			y=true
		else
			y=false
		fi
		RIPGUI="$RIPGUI
		<radiobutton>
		<label>$v1 $v2</label>
		<variable>video-$v1</variable>
		<default>$y</default>
		</radiobutton>
		"
		x=`expr $x + 1`
	done
	if [ "$vstreamnum" = 0 ]; then
		RIPGUI="$RIPGUI
		<radiobutton>
		<label>$(gettext 'no video streams found')</label>
		<variable>novideo</variable>
		<default>true</default>
		</radiobutton>
		"
	fi
	RIPGUI="$RIPGUI
	</frame>
	
	<frame $(gettext 'choose the audio stream you want to use')>
	"
	x=1
	while [ "$x" -le "$astreamnum" ]; do
		a1=`echo "$audiostreams" | sed -n "$x"p | cut -f 1 -d '|'`
		a2=`echo "$audiostreams" | sed -n "$x"p | cut -f 2 -d '|'`
		if [ "$x" = 1 ]; then
			y=true
		else
			y=false
		fi
		RIPGUI="$RIPGUI
		<radiobutton>
		<label>$a1 $a2</label>
		<variable>audio-$a1</variable>
		<default>$y</default>
		</radiobutton>
		"
		x=`expr $x + 1`
	done
	if [ "$astreamnum" = 0 ]; then
		RIPGUI="$RIPGUI
		<radiobutton>
		<label>$(gettext 'no audio streams found')</label>
		<variable>noaudio</variable>
		<default>true</default>
		</radiobutton>
		"
	fi
	RIPGUI="$RIPGUI
	</frame>	
	
	<frame $(gettext 'choose the subtitle stream you want to use')>
	"
	x=0
	while [ "$x" -le "$sstreamnum" ]; do
		if [ "$x" != 0 ]; then
			s1=`echo "$subtitlestreams" | sed -n "$x"p | cut -f 1 -d '|'`
			s2=`echo "$subtitlestreams" | sed -n "$x"p | cut -f 2 -d '|'`
		fi
		if [ "$x" = 0 ]; then
			y=true
			s1=0
			s2="no subtitles"
		else
			y=false
		fi
		RIPGUI="$RIPGUI
		<radiobutton>
		<label>$s1 $s2</label>
		<variable>sub-$s1</variable>
		<default>$y</default>
		</radiobutton>
		"
		x=`expr $x + 1`
	done
	RIPGUI="$RIPGUI
	</frame>	
	<hbox>
	<button>
		<label>$(gettext 'Play')</label>
		<action type=\"exit\">EXIT_NEXT</action>
	</button>
	<button tooltip-text='$(gettext 'quit')'>
	<input file icon=\"ffplay-quit\"></input>
		<action type=\"exit\">EXIT_NOW</action>
	</button>
	</hbox>
	</vbox>
	</window>
	"
	results=`$GTKDIALOG --program=RIPGUI --center`
	r2=`echo "$results" | sed 's/=\"/|\"/'`
	if [ "`echo $r2 | grep EXIT | grep abort`" != "" ] || [ "`echo $results | grep EXIT_NOW`" != "" ]; then
		echo $(gettext 'abort')
		exit
	fi
	nosubs=`echo "$r2" | grep 'sub-0|\"true\"'`
	if [ "$nosubs" = "" ]; then
		substream=`echo "$r2" | grep '|\"true\"' | grep ^sub | cut -f 1 -d '|' | cut -f 2 -d ':'`
		subbox=""
		ss="-sst $substream"
	else
		ss=""
		subbox="-sn"
	fi
	audiostream=`echo "$r2" | grep '|\"true\"' | grep ^audio | cut -f 1 -d '|' | cut -f 2 -d ':'`
	as="-ast $audiostream"
	videostream=`echo "$r2" | grep '|\"true\"' | grep ^video | cut -f 1 -d '|' | cut -f 2 -d ':'`
	if [ "$videostream" != "" ]; then
		vs="-vst $videostream"
	fi
fi

##known audio types to skip checking for video
ext=${filetoplay##*.}
array=(aac ac3 aiff ape atrac au flac m4a mka mkw mp2 mp3 mpc ogg opus ra sbc shn tta wav wma wv)
for item in ${array[*]}
do
	if [ "`echo $item | grep -ix $ext`" != "" ]; then
		knownaudio="$item"
	fi
done

if [ "$knownaudio" = "" ]; then
	videoinfo=`echo "$ff" | grep 'Stream #' | grep 'Video:' | grep -v mjpeg | grep -v 'Video: png,' | sed 's/Stream\ #0:0:\ Video:\ //'`
fi
if [ "$videoinfo" != "" ]; then
	if [ "`which xdotool`" != "" ]; then
		xyear=`xdotool version | cut -f 2 -d '.' | tr -d -c 0-9 | head -c 4`
		if [ "$xyear" -lt 2019 ]; then
			echo $(gettext 'your version of xdotool is from') "$xyear" $(gettext 'and may be buggy')
			echo $(gettext 'you may want to think about upgrading it')
		fi
	fi
	echo -x $file_width -y $file_height $subbox $ss $as $vs $di > $tmpfile/streamstoplay
	SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$timetostart" "$filetoplay" -x $file_width -y $file_height $subbox $ss $as $vs $di 2> $tmpfile/timecount &
	touch $tmpfile/video
else
	SDL_AUDIODRIVER="alsa" AUDIODEV="$audiosource" ffplay -ss "$timetostart" "$filetoplay" -nodisp 2> $tmpfile/timecount &
fi
ffplay_pid=$!
sleep 0.1

if [ "$videoinfo" != "" ]; then
if [ "`which xdotool`" != "" ]; then
	win=`xdotool search --all --name --pid $ffplay_pid ffplay | tail -n 1`
	if [ "$win" = "" ]; then
		sleep 0.2
		win=`xdotool search --pid $ffplay_pid | tail -n 1 2>/dev/null`
	fi
	if [ "$win" = "" ]; then
		win=`xdotool search --name "$filetoplay" | tail -n 1 2>/dev/null`
	fi
	win2=`xdotool search --name "$filetoplay" | grep -v $win`
	echo $win > $tmpfile/win
	if [ "$win2" != "" ]; then
		echo $win2 >> $tmpfile/win
	fi
fi
fi

seconds=`ffprobe -i "$filetoplay" -show_entries format=duration -v quiet -of csv="p=0"`
nofraction=`echo $seconds | cut -f 1 -d '.'`
echo "$nofraction" > $tmpfile/nofraction

if ! [ -f $tmpfile/specifics ] || [ "`cat $tmpfile/specifics | wc -l | cut -f 1 -d ' '`" = 1 ]; then
	t1=`echo "scale=2;$seconds/10" | bc -l`
	d1=`date -u -d @$t1 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t2=`echo "scale=2;$t1*2" | bc -l`
	d2=`date -u -d @$t2 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t3=`echo "scale=2;$t1*3" | bc -l`
	d3=`date -u -d @$t3 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t4=`echo "scale=2;$t1*4" | bc -l`
	d4=`date -u -d @$t4 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t5=`echo "scale=2;$t1*5" | bc -l`
	d5=`date -u -d @$t5 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`	
	t6=`echo "scale=2;$t1*6" | bc -l`
	d6=`date -u -d @$t6 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t7=`echo "scale=2;$t1*7" | bc -l`
	d7=`date -u -d @$t7 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t8=`echo "scale=2;$t1*8" | bc -l`
	d8=`date -u -d @$t8 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t9=`echo "scale=2;$t1*9" | bc -l`
	d9=`date -u -d @$t9 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t10=`echo "scale=2;$t1*10" | bc -l`
	d10=`date -u -d @$t10 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t11=`echo "scale=2;$t1*.5" | bc -l`
	d11=`date -u -d @$t11 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t12=`echo "scale=2;$t1*1.5" | bc -l`
	d12=`date -u -d @$t12 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t13=`echo "scale=2;$t1*2.5" | bc -l`
	d13=`date -u -d @$t13 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t14=`echo "scale=2;$t1*3.5" | bc -l`
	d14=`date -u -d @$t14 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t15=`echo "scale=2;$t1*4.5" | bc -l`
	d15=`date -u -d @$t15 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t16=`echo "scale=2;$t1*5.5" | bc -l`
	d16=`date -u -d @$t16 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t17=`echo "scale=2;$t1*6.5" | bc -l`
	d17=`date -u -d @$t17 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t18=`echo "scale=2;$t1*7.5" | bc -l`
	d18=`date -u -d @$t18 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t19=`echo "scale=2;$t1*8.5" | bc -l`
	d19=`date -u -d @$t19 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`
	t20=`echo "scale=2;$t1*9.5" | bc -l`
	d20=`date -u -d @$t20 +"%T" 2>/dev/null | sed 's/^0*//' | sed 's/^:*//'`

	echo "00:00|0" > $tmpfile/specifics
	echo "$d11"\|"$t11" >> $tmpfile/specifics
	echo "$d1"\|"$t1" >> $tmpfile/specifics
	echo "$d12"\|"$t12" >> $tmpfile/specifics
	echo "$d2"\|"$t2" >> $tmpfile/specifics
	echo "$d13"\|"$t13" >> $tmpfile/specifics
	echo "$d3"\|"$t3" >> $tmpfile/specifics
	echo "$d14"\|"$t14" >> $tmpfile/specifics
	echo "$d4"\|"$t4" >> $tmpfile/specifics
	echo "$d15"\|"$t15" >> $tmpfile/specifics
	echo "$d5"\|"$t5" >> $tmpfile/specifics
	echo "$d16"\|"$t16" >> $tmpfile/specifics
	echo "$d6"\|"$t6" >> $tmpfile/specifics
	echo "$d17"\|"$t17" >> $tmpfile/specifics
	echo "$d7"\|"$t7" >> $tmpfile/specifics
	echo "$d18"\|"$t18" >> $tmpfile/specifics
	echo "$d8"\|"$t8" >> $tmpfile/specifics
	echo "$d19"\|"$t19" >> $tmpfile/specifics
	echo "$d9"\|"$t9" >> $tmpfile/specifics
	echo "$d20"\|"$t20" >> $tmpfile/specifics
	echo "$d10"\|"$t10" >> $tmpfile/specifics

fi

total=`date -u -d @$seconds +"%T" 2>/dev/null`
echo 0 > $tmpfile/newwidth
sleep 0.2

echo even > $tmpfile/pause

#meta=`echo "$ff" | grep -B 30 'Duration:' | grep -A 30 'Metadata:' | grep -v 'Metadata:' | grep -v 'At least one output file must be specified' | grep -v 'Side data:' | grep -v "Cover (front)" | awk '{$1=$1};1'`
meta=`echo "$ff" | grep -A 99 'Metadata:' | grep -v 'Metadata:' | grep -v 'At least one output file must be specified' | grep -v 'Side data:' | grep -v "Cover (front)" | awk '{$1=$1};1'`
tracktitle=`echo "$meta" | grep -i ^title | head -n 1 | sed 's/:\ /|/' | cut -f 2 -d '|'`
artist=`echo "$meta" | grep -i ^artist | head -n 1 | sed 's/:\ /|/' | cut -f 2 -d '|'`
if [ "$tracktitle" = "" ]; then
	tracktitle="$(gettext 'Title Not Found')"
fi
if [ "$artist" = "" ]; then
	artist="$(gettext 'Artist Not Found')"
fi

chapter=`cat $tmpfile/specifics | cut -f 3 -d '|' | head -n 1`
if [ "$chapter" != "" ]; then
	meta=`echo "$ff" | grep -B 30 'Duration:' | grep -A 30 'Metadata:' | grep -v 'Metadata:' | grep -v 'At least one output file must be specified' | grep -v 'Side data:' | grep -v "Cover (front)" | awk '{$1=$1};1'`
	lines=`wc -l $tmpfile/specifics | cut -f 1 -d ' '`
	num=1
	cat $tmpfile/specifics | cut -f 3 -d '|' | while read chnew; do
		chnew2=`echo "$chnew" | sed 's/_/\ /g'`
		start=`cat $tmpfile/specifics | grep "|$chnew$" | head -n 1 | cut -f 2 -d '|'`
		end1=`cat $tmpfile/specifics | grep "|$start|" -A1 | tail -n 1 | cut -f 2 -d '|'`
		end=`expr $end1 - 1`
		if [ "$num" = "$lines" ]; then
			end=$nofraction
		fi
		if [ "`cat $tmpfile/chapter 2>/dev/null | grep ^$chnew2$`" != "" ]; then
			y=2
			z=1
			while [ "$z" = 1 ]; do
				chnew2="$chnew2 $y"
				if [ "`cat $tmpfile/chapter 2>/dev/null | grep ^$chnew2$`" = "" ]; then
					z=2
				fi
				y=`expr $y + 1`
			done
		fi
		echo "$chnew2"\|$start\|$end >> $tmpfile/chapter
		num=`expr $num + 1`
	done
fi

ffplay-gtk-timer $timetostart $nofraction &
fgt=$!

dir=`dirname "$filetoplay"`
cd "$dir"

if [ "$videoinfo" = "" ]; then
	ffmpeg -y -i "$file" -nostats -loglevel 0 -map 0:v -vf scale=150:150 $tmpfile/cover.jpg < /dev/null
	if ! [ -f $tmpfile/cover.jpg ]; then
		jpg=`find . -iname '*.jpg' | head -n 1`
		if [ "$jpg" != "" ]; then
			cp "$jpg" $tmpfile/cover.jpg
		else
			jpg=`find . -iname '*.jpeg' | head -n 1`
			if [ "$jpg" != "" ]; then
				cp "$jpg" $tmpfile/cover.jpg
			else
				jpg=`find . -iname '*.png' | head -n 1`
				if [ "$jpg" != "" ]; then
					ffmpeg -i "$jpg" $tmpfile/cover.jpg
				fi
			fi
		fi
	fi
fi

echo "$file" > $tmpfile/currentfile

ext="${file##*.}"
if ! [ -f "$tmpfile"/playlist ]; then
	find . -maxdepth 1 -type f -name "*.$ext" | sort -V | sed 's/^.\///' > $tmpfile/playlist
fi	
pnumtotal=`cat $tmpfile/playlist | wc -l | tr -s ' ' | cut -f 1 -d ' '`
pnum=1
while [ "$pnum" -le "$pnumtotal" ]
do
	newplaylist=`cat $tmpfile/playlist | sed -n "$pnum"p`
	playlist="$playlist<item>$newplaylist</item>"
	pnum=`expr $pnum + 1`
done
	
previous=`cat $tmpfile/playlist | grep "$file" -B 1 | head -n 1`
next=`cat $tmpfile/playlist | grep "$file" -A 1 | tail -n 1`

specifics=""
	for specificlist in `cat $tmpfile/specifics | sed 's/|.*|/|/'`
	do
		specifics="$specifics<item>$specificlist</item>"
	done

filetoplaynew=`echo "$filetoplay" | sed 's/)/\\\)/g' | sed 's/(/\\\(/g'`
	
export FFPLAY_GTK='

	<window title="ffplay-gtk '$version' '$file' "icon-name="gtk-media-play" resizable="true" scrollable="true">
	
	<hbox homogeneous="false" space-expand="false" space-fill="false">
	'
	if [ -f $tmpfile/cover.jpg ]; then
		FFPLAY_GTK=$FFPLAY_GTK'
		<pixmap file-monitor="true" auto-refresh="true"><input file>'$tmpfile'/cover.jpg</input><height>150</height><width>150</width></pixmap>
		'
	fi
	FFPLAY_GTK=$FFPLAY_GTK'
	<vbox>

				<hbox homogeneous="false" space-expand="false" space-fill="false">
				<text wrap="false" width-chars="40" use-markup="true"><label>'$artist' - '$tracktitle'</label></text>					
					'
					if [ "$chapter" != "" ]; then
						FFPLAY_GTK=$FFPLAY_GTK'
					<entry editable="false" width-chars="26" file-monitor="true" auto-refresh="true">
						<variable>chapter</variable>
						<input file>'$tmpfile'/chapter-title</input>
						<action type="refresh">chapter</action>
					</entry>
					'
					fi
					
					FFPLAY_GTK=$FFPLAY_GTK'
					
				</hbox>
				
				<hbox homogeneous="false" space-expand="false" space-fill="false">
				'
				if [ "$videoinfo" != "" ]; then
				if [ "`which xdotool`" != "" ]; then
					FFPLAY_GTK=$FFPLAY_GTK'
					<comboboxtext file-monitor="true" auto-refresh="true" tooltip-text="'$(gettext 'Select xdotool process to control, the top process is the default. Every time you click the button to the right the processes will refresh. Sometimes the incorrect process is selected and the controls will not work so this is a workaround for now. If no process number is listed then the file probably needs to be reloaded before the controls will work. You can reload the file from the options menu.')'">
						<variable>process</variable>
						<input file>'$tmpfile'/win</input>
					</comboboxtext>
					<button tooltip-text="'$(gettext 'click button to refresh xdotool processes if controls are not working')'">
					<input file>/usr/share/pixmaps/ffplay-yes.png</input>
						<action>echo $process > '$tmpfile'/win</action>
						<action>xdotool search --name '\"$filetoplaynew\"' | grep -v $process >> '$tmpfile'/win</action>
					</button>
					'
				fi
				fi
				FFPLAY_GTK=$FFPLAY_GTK'
				
					<button tooltip-text="'$(gettext 'go forward 10 seconds')'">
					<input file icon="ffplay-forward"></input>
					<action>ffplay-gtk-timer pausecheck $process</action>
					'
					if [ "$videoinfo" != "" ]; then
						if [ "`which xdotool`" != "" ]; then
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>xdotool key --clearmodifiers --window $process Right</action>
							'
						else
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>kill -9 `pidof ffplay` 2>/dev/null</action>
							<action>ffplay-gtk-timer audio 10 '\"$file\"' video</action>
							'
						fi
					 else
						FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>ffplay-gtk-timer audio 10 '\"$file\"'</action>
						'
					fi
					FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					   <action>ffplay-gtk-timer timer &</action>
					   <action>sleep 0.2</action>
						</button>
				
					<button tooltip-text="'$(gettext 'go forward 1 minute')'">
					<input file icon="ffplay-forward"></input>
					<action>ffplay-gtk-timer pausecheck $process</action>
					'
					if [ "$videoinfo" != "" ]; then
						if [ "`which xdotool`" != "" ]; then
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>xdotool key --clearmodifiers --window $process Up</action>
							'
						else	
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>kill -9 `pidof ffplay` 2>/dev/null</action>
							<action>ffplay-gtk-timer audio 60 '\"$file\"' video</action>
							'
						fi
					 else
						FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>ffplay-gtk-timer audio 60 '\"$file\"'</action>
						'
					fi
					
					FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					   <action>ffplay-gtk-timer timer &</action>
					   <action>sleep 0.2</action>
						</button>
						
						<button tooltip-text="'$(gettext 'pause / play')'">
					<input file icon="ffplay-pause-play"></input>
					'
					if [ "$videoinfo" != "" ]; then
						if [ "`which xdotool`" != "" ]; then
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>xdotool key --clearmodifiers --window $process p</action>
							'
						else
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>ffplay-gtk-timer check '\"$file\"' video</action>
							'
						fi
					 else
						FFPLAY_GTK=$FFPLAY_GTK'
						<action>ffplay-gtk-timer check '\"$file\"'</action>
						'
						fi
						FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					 <action>ffplay-gtk-timer even</action>
					 	<action>ffplay-gtk-timer timer nofraction '$fgt' play &</action>
					   <action>sleep 0.2</action>
					    <action type="refresh">timeentry</action>
						</button>
						
					<button tooltip-text="'$(gettext 'go backward 1 minute')'">
					<input file icon="ffplay-backward"></input>
					<action>ffplay-gtk-timer pausecheck $process</action>
					'
					if [ "$videoinfo" != "" ]; then
						if [ "`which xdotool`" != "" ]; then
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>xdotool key --clearmodifiers --window $process Down</action>
							'
						else	
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>kill -9 `pidof ffplay` 2>/dev/null</action>
							<action>ffplay-gtk-timer audio -60 '\"$file\"' video</action>
							'
						fi	
					 else
						FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>ffplay-gtk-timer audio -60 '\"$file\"'</action>
						'
					fi
					
					FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					   <action>ffplay-gtk-timer timer '$nofraction' &</action>
					   <action>sleep 0.2</action>
						</button>
						
					<button tooltip-text="'$(gettext 'go backward 10 seconds')'">
					<input file icon="ffplay-backward"></input>
					<action>ffplay-gtk-timer pausecheck $process</action>
					'
					if [ "$videoinfo" != "" ]; then
						if [ "`which xdotool`" != "" ]; then
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>xdotool key --clearmodifiers --window $process Left</action>
							'
						else	
							FFPLAY_GTK=$FFPLAY_GTK'
							<action>kill -9 `pidof ffplay` 2>/dev/null</action>
							<action>ffplay-gtk-timer audio -10 '\"$file\"' video</action>
							'
						fi	
					 else
						FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>ffplay-gtk-timer audio -10 '\"$file\"'</action>
						'
					fi
					
					FFPLAY_GTK=$FFPLAY_GTK'
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					   <action>ffplay-gtk-timer timer &</action>
					   <action>sleep 0.2</action>
					   </button>
						
					<entry editable="false" width-chars="8" file-monitor="true" auto-refresh="true">
						<variable>timeentry</variable>
						<input file>'$tmpfile'/human</input>
						<action type="refresh">timeentry</action>
					</entry>
					<text use-markup="true"><label>'$total'</label></text>

				</hbox>
				
			<hbox homogeneous="false" space-expand="false" space-fill="false">	
			'
			if [ "$chapter" = "" ]; then	
				FFPLAY_GTK=$FFPLAY_GTK'
				<vbox width-request="120">
				'
			else
				FFPLAY_GTK=$FFPLAY_GTK'
				<vbox width-request="230">
				'
			fi
			FFPLAY_GTK=$FFPLAY_GTK'
				<combobox tooltip-text="'$(gettext 'Select specific time or chapter and click the button to the right')'">
					<variable>specific</variable>
					'$specifics'
				</combobox>
				</vbox>
				
				<button tooltip-text="'$(gettext 'select a time or chapter to the left and click the button')'">
					<input file>/usr/share/pixmaps/ffplay-yes.png</input>
					<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					   	<action>ffplay-gtk-timer specific '\"$file\"' $specific '$nofraction' '\"$videoinfo\"'</action>
				</button>
				
				<vbox width-request="230">
				<combobox tooltip-text="'$(gettext 'Select file to play and click the button to the right')'">
					<variable>play</variable>
					'$playlist'
				</combobox>
				</vbox>
				
				<button tooltip-text="'$(gettext 'select a file to play and click the button')'">
					<input file>/usr/share/pixmaps/ffplay-yes.png</input>
					<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					   	<action>ffplay-gtk "$play" &</action>
				</button>
	</hbox>
	
					<hbox>

		'
		if [ "$chapter" = "" ]; then	
			FFPLAY_GTK=$FFPLAY_GTK'
			<hscale width-request="225" range-value="0" tooltip-text="'$(gettext 'select percentage of file to move to')'">
			'
		else
			FFPLAY_GTK=$FFPLAY_GTK'
			<hscale width-request="300" range-value="0" tooltip-text="'$(gettext 'select percentage of file to move to')'">
			'
		fi
			FFPLAY_GTK=$FFPLAY_GTK'
			<variable>percent</variable>
			 <action>kill -9 `pidof ffplay` 2>/dev/null</action>
			<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
			'
			if [ "$videoinfo" = "" ]; then
			FFPLAY_GTK=$FFPLAY_GTK'
			<action>ffplay-gtk-timer percent $percent '$nofraction' '\"$file\"' &</action>
			'
			else
			FFPLAY_GTK=$FFPLAY_GTK'
			<action>ffplay-gtk-timer percent $percent '$nofraction' '\"$file\"' video &</action>
			'
			fi
			FFPLAY_GTK=$FFPLAY_GTK'
		</hscale>
					
					
						<menubar>
					<menu>
						'
						if [ "$videoinfo" != "" ]; then
							if [ "`which xdotool`" != "" ]; then
								FFPLAY_GTK=$FFPLAY_GTK'
								<menuitem icon-name="ffplay-forward">
								<label>'$(gettext 'go forward 10 minutes or next chapter')'</label>
								<action>ffplay-gtk-timer pausecheck $process</action>
								<action>xdotool key --clearmodifiers --window $process Page_Up</action>
								<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
								<action>ffplay-gtk-timer timer &</action>
								<action>sleep 0.2</action>
								</menuitem>
								<menuitem icon-name="ffplay-backward">
								<label>'$(gettext 'go backward 10 minutes or previous chapter')'</label>
								<action>ffplay-gtk-timer pausecheck $process</action>
								<action>xdotool key --clearmodifiers --window $process Page_Down</action>
								<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
								<action>ffplay-gtk-timer timer &</action>
								<action>sleep 0.2</action>
								</menuitem>
								'
							fi
						fi
						FFPLAY_GTK=$FFPLAY_GTK'
						'
						if [ "$videoinfo" != "" ]; then
							if [ "`which xdotool`" != "" ]; then
								FFPLAY_GTK=$FFPLAY_GTK'
								<menuitem>
								<label>'$(gettext 'cycle the audio channel')'</label>
								<action>ffplay-gtk-timer pausecheck $process</action>
								<action>xdotool key --clearmodifiers --window $process a</action>
								<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
								<action>ffplay-gtk-timer timer &</action>
								<action>sleep 0.2</action>
								</menuitem>
								<menuitem>
								<label>'$(gettext 'cycle the video channel')'</label>
								<action>ffplay-gtk-timer pausecheck $process</action>
								<action>xdotool key --clearmodifiers --window $process v</action>
								<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
								<action>ffplay-gtk-timer timer &</action>
								<action>sleep 0.2</action>
								</menuitem>
								<menuitem>
								<label>'$(gettext 'cycle the subtitle channel')'</label>
								<action>ffplay-gtk-timer pausecheck $process</action>
								<action>xdotool key --clearmodifiers --window $process t</action>
								<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
								<action>ffplay-gtk-timer timer &</action>
								<action>sleep 0.2</action>
								</menuitem>
								'
							fi
						fi
						FFPLAY_GTK=$FFPLAY_GTK'
						<menuitem icon-name="ffplay-add">
					<label>'$(gettext 'new file')'</label>
					<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					 <action>ffplay-gtk &</action>
						</menuitem>
						
						<menuitem>
					<label>'$(gettext 'reload file')'</label>
					<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					 <action>ffplay-gtk '\"$file\"' &</action>
						</menuitem>
						
					'
					if [ "`which fftag`" != "" ]; then
						FFPLAY_GTK=$FFPLAY_GTK'
						
						<menuitem icon-name="ffplay-edit">
					<label>'$(gettext 'edit metadata in fftag')'</label>
					<action>fftag '\"$file\"' &</action>
						</menuitem>
						
					'
					fi
					FFPLAY_GTK=$FFPLAY_GTK'
					
					'
					if [ "$videoinfo" != "" ]; then
					if [ "`which pdvdrip`" != "" ]; then
						FFPLAY_GTK=$FFPLAY_GTK'
						
						<menuitem icon-name="gtk-convert">
					<label>'$(gettext 'convert video in pdvdrip')'</label>
					<action>pdvdrip '\"$file\"' &</action>
						</menuitem>
						
					'
					fi
					fi
					FFPLAY_GTK=$FFPLAY_GTK'
						<menuitem>
					<label>'$(gettext 'file attributes')'</label>
					<action>/usr/share/ffplay-gtk/attributes '\"$file\"' &</action>
						</menuitem>
						
						<label>[ '$(gettext 'options')' ]</label>
					</menu>
				</menubar>
					'
					if [ "$videoinfo" != "" ]; then
					if [ "`which xdotool`" != "" ]; then
						FFPLAY_GTK=$FFPLAY_GTK'				
								<button tooltip-text="'$(gettext 'full screen toggle, if full screen goes over your dock bar hit the f key to get back to the gui')'">
					<input file icon="ffplay-fullscreen"></input>
					 <action>xdotool key --clearmodifiers --window $process f</action>
				 </button>
				 '
				 fi
				 fi
					FFPLAY_GTK=$FFPLAY_GTK'
					
					<button tooltip-text="'$(gettext 'play previous file')' '$previous'">
					<input file icon="ffplay-previous"></input>
					<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
						<action type="exit">EXIT_PREVIOUS</action>
				 </button>
				 
				 <button tooltip-text="'$(gettext 'play next file')' '$next'">
					<input file icon="ffplay-next"></input>
					<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
						<action type="exit">EXIT_NEXT</action>
				 </button>
				
				<button tooltip-text="'$(gettext 'quit')'">
					<input file icon="ffplay-quit"></input>
						<action>kill -9 `pidof ffplay` 2>/dev/null</action>
						<action>kill -9 `pidof ffplay-gtk-timer` 2>/dev/null</action>
					 <action type="exit">EXIT_NOW</action>
				</button>
				
				</hbox>
				


</vbox>
</hbox>
	
	</window>
		'
results="`$GTKDIALOG --program=FFPLAY_GTK  --geometry +650+780`" 
r2=`echo "$results" | sed 's/=\"/|\"/'`
if [ "`echo $r2 | grep EXIT | grep abort`" != "" ] || [ "`echo $results | grep EXIT_NOW`" != "" ]; then
	echo $(gettext 'abort')
	exit
fi

if [ "`echo $r2 | grep EXIT_NEXT`" != "" ]; then
	ffplay-gtk "$next" &
	exit
fi

if [ "`echo $r2 | grep EXIT_PREVIOUS`" != "" ]; then
	ffplay-gtk "$previous" &
	exit
fi


