#!/bin/bash

# Copyright (C) 2007 PÃ¢ris Quentin
#                    Cassarin-Grand Arthur

# 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 2 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.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

TITLE="Capture Plugin"
CAP_FOLDER="$HOME/POL_Capture"
unset PULSE_CHECK
unset PULSE_CONFIG
unset FFMPEG_VERSION

# Checking ffmpeg version
FFMPEG_VERSION=`ffmpeg -version 2>&1 | head -n 1 | awk '{ print $3 }' | cut -d "-" -f2`
if [ "$FFMPEG_VERSION" == "7" ]; then
	POL_SetupWindow_message "$(eval_gettext 'Your ffmpeg version is too old\nplease update to version 0.10 or newer')"
	exit 0
fi

make_logo ()
{
	cd $CAP_FOLDER
	if [ ! -e "logo.mp4" ]; then
		cp "$POL_USER_ROOT/plugins/Capture/logo/POL-0000.png" .
		# IMG 0 to 9
		for x in {1..9}
		do
	   	cp POL-0000.png POL-000$x.png
		done
		# IMG 10 to 74
		for x in {10..74}
		do
		cp POL-0000.png POL-00$x.png
		done
		POL_SetupWindow_wait "$(eval_gettext 'Please wait while the PlayOnLinux logo is created...')" "$TITLE"
		ffmpeg -r 25 -an -i POL-%04d.png logo.mp4
		
		rm *.png
	fi
	if [ ! -e "logo.wav" ]; then
		cp "$POL_USER_ROOT/plugins/Capture/logo/logo.wav" .
	fi
}

# Capture for OpenGL applications
assign_cap_glc ()
{	
	if [ ! -e "$CAP_FOLDER" ]; then
	mkdir -p "$CAP_FOLDER"
	fi
	cd "$POL_USER_ROOT/tmp/"
	unset GAME

	POL_SetupWindow_shortcuts_list "$(eval_gettext 'Which application do you want to apply the modification to?')" "$TITLE"
	GAME="$APP_ANSWER"
	if [ "$GAME" != "" ]; then
		PULSE_CHECK=`which pulseaudio`
		[ "$PULSE_CHECK" == "" ] || POL_SetupWindow_message "$(eval_gettext 'To enable sound with OpenGL capture, pulseaudio MUST be disabled.\nIt will be reload after capture.\nIf you do not aggre, just close this plugin now.')" "$TITLE"
		GAME_CAP="`echo "$GAME"`-Capture"
		COUNT_LINE=`wc -l "$POL_USER_ROOT/shortcuts/$GAME" | awk '{ print($1) }'`
		COUNT_LINE1=`echo $COUNT_LINE - 1 | bc`
		rm -rf "$POL_USER_ROOT/tmp/$GAME_CAP"
		touch "$POL_USER_ROOT/tmp/$GAME_CAP"
		cat "$POL_USER_ROOT/shortcuts/$GAME" | head -n $COUNT_LINE1 > "$POL_USER_ROOT/tmp/$GAME_CAP"
		EXE_CAP=`cat "$POL_USER_ROOT/shortcuts/$GAME" | tail -n 1`
		echo "POL_Wine_AutoSetVersionEnv" >> "$POL_USER_ROOT/tmp/$GAME_CAP"
		EXE_CAP=`echo $EXE_CAP | sed -e "s/POL_Wine/wine/g"`
		echo "glc-capture -b back --out=\"$CAP_FOLDER/$GAME_CAP.pid\" $EXE_CAP" >> "$POL_USER_ROOT/tmp/$GAME_CAP"
		echo "CAP_TIME=\`date +%D-%T | tr \"/\" \"-\"\ | tr \":\" \"-\"\`" >> "$POL_USER_ROOT/tmp/$GAME_CAP"
		echo "GAME_CAP2=\"\`echo \"$GAME_CAP\"-\"\$CAP_TIME\"\`\"" >> "$POL_USER_ROOT/tmp/$GAME_CAP"
		echo "mv \"$CAP_FOLDER/$GAME_CAP.pid\" \"$CAP_FOLDER/\"\"\$GAME_CAP2\"\".pid\"" >> "$POL_USER_ROOT/tmp/$GAME_CAP"
		POL_SetupWindow_message "$(eval_gettext 'Capture will begin...')" "$TITLE"
		POL_SetupWindow_wait "$(eval_gettext 'Capture in progress...')" "$TITLE"
		# Killing pulseaudio if found and disable auto-reload feature
		if [ -e "$HOME/.pulse/client.conf" ]; then
			mv "$HOME/.pulse/client.conf" "$HOME/.pulse/client.conf.save"
			PULSE_CONFIG="1"
		else
			PULSE_CONFIG="0"
		fi
		[ "$PULSE_CHECK" == "" ] || echo "autospawn = no" > "$HOME/.pulse/client.conf"
		[ "$PULSE_CHECK" == "" ] || pulseaudio --kill
		bash "$GAME_CAP"
		# Restarting pulseaudio after capture if needed and re-enable auto-reload feature
		rm "$HOME/.pulse/client.conf"
		if [ "$PULSE_CONFIG" == "1" ]; then
			mv "$HOME/.pulse/client.conf.save" "$HOME/.pulse/client.conf"
			unset PULSE_CONFIG
		fi
		[ "$PULSE_CHECK" == "" ] || pulseaudio --start
	else
		menu_capture
	fi
	menu_capture_glc
}

play_cap_glc ()
{
	cd "$CAP_FOLDER"
	POL_SetupWindow_browse "$(eval_gettext 'Please choose a PID capture file to play')" "$TITLE" ""
	CAP_FILE="$APP_ANSWER"
	if [ -z "$CAP_FILE" ];then
		POL_SetupWindow_message "$(eval_gettext 'You must choose a capture to read')" "$TITLE"
		menu_capture_glc
	else
		POL_SetupWindow_wait "$(eval_gettext 'Reading in progress...')" "$TITLE"
		glc-play "$CAP_FILE"
	fi
	menu_capture
}

convert_cap_glc ()
{
	cd $CAP_FOLDER
	POL_SetupWindow_browse "$(eval_gettext 'Please choose a capture file to convert')" "$TITLE" ""
	CAP_FILE="$APP_ANSWER"
	if [ -z "$CAP_FILE" ];then
		POL_SetupWindow_message "$(eval_gettext 'You must choose a capture to convert')" "$TITLE"
		menu_capture_glc
	else
		POL_SetupWindow_menu_list "$(eval_gettext 'Add the PlayOnLinux logo to your video?')" "Logo" "$(eval_gettext 'Yes')-$(eval_gettext 'No')" "-"
		if [ "$APP_ANSWER" == "$(eval_gettext 'Yes')" ]; then
			ADD_LOGO="1"
			make_logo
		else
			ADD_LOGO="0"
		fi	
		CAP_VID=`echo "$CAP_FILE" | cut -d . -f 1`
		POL_SetupWindow_message "$(eval_gettext 'Warning: For correct video encoding,\nresolution MUST be a multiple of 16')" "$TITLE"
		POL_SetupWindow_menu_list "$(eval_gettext 'Choose the video format for conversion')" "Menu" "x264 (Hi8)/Vorbis-x264 (Hi10)/Vorbis-XviD/MP3-Theora/Vorbis-VP8/Vorbis" "-"
		VIDEO_ITEM="$APP_ANSWER"
		if [ "$VIDEO_ITEM" == "x264 (Hi8)/Vorbis" ]; then
			POL_SetupWindow_message "$(eval_gettext 'Ubuntu users must add the Jon Severinsson PPA\nfor this transcoding to work properly.')" "$TITLE"
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -r 1 -f image2 video.png
			VIDEO_WIDTH=`exiftool video.png | grep "Image Width" | awk '{ print $4 }'`
			VIDEO_HEIGHT=`exiftool video.png | grep "Image Height" | awk '{ print $4 }'`
			WIDTH_16M=`echo $VIDEO_WIDTH/16 | bc`
			HEIGHT_16M=`echo $VIDEO_HEIGHT/16 | bc`
			VIDEO_WIDTH=`echo $WIDTH_16M*16 | bc`
			VIDEO_HEIGHT=`echo $HEIGHT_16M*16 | bc`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options for pass 1 \n (leave defaults if you do not know what to do)')" "$TITLE" "-vprofile high -level 4.1 -pass 1 -refs 16 -subq 10 -partitions +all-partp4x4 -me_method umh -me_range 24 -trellis 2 -b:v 1024k -bt 768k -s "$VIDEO_WIDTH"x"$VIDEO_HEIGHT" -r 25 -threads 2"
			VIDEO_OPTION1="$APP_ANSWER"
			VIDEO_WIDTH=`echo $VIDEO_OPTION1 | awk '{ print $24 }' | awk -F "x" '{ print $1 }'`
			VIDEO_HEIGHT=`echo $VIDEO_OPTION1 | awk '{ print $24 }' | awk -F "x" '{ print $2 }'`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options for pass 2 \n (leave defaults if you do not know what to do)')" "$TITLE" "-vprofile high -level 4.1 -pass 2 -refs 16 -subq 10 -partitions +all-partp4x4 -me_method umh -me_range 24 -trellis 2 -b:v 1024k -bt 768k -s "$VIDEO_WIDTH"x"$VIDEO_HEIGHT" -r 25 -threads 2"
			VIDEO_OPTION2="$APP_ANSWER"
			POL_SetupWindow_wait "$(eval_gettext 'Please wait while your video is being encoded...')" "$TITLE" 
			glc-play "$CAP_FILE" -o - -a 1 | ffmpeg -i - -acodec libvorbis -strict -2 -ac 2 -ab 128k -ar 44100 audio.ogg
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -vcodec libx264 -an $VIDEO_OPTION1 -fastfirstpass 1 -pix_fmt yuv420p video_temp.mkv
			if [ "$ADD_LOGO" == "1" ]; then
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -i audio.ogg -acodec copy -vcodec libx264 $VIDEO_OPTION2 -pix_fmt yuv420p video.mkv
				ffmpeg -i logo.wav -acodec libvorbis -strict -2 -ac 2 -ab 128k -ar 44100 logo.ogg
				ffmpeg -i logo.mp4 -vcodec libx264 -an $VIDEO_OPTION1 -fastfirstpass 1 -pix_fmt yuv420p logo_temp.mkv
				ffmpeg -i logo.mp4 -i logo.ogg -acodec copy -vcodec libx264 $VIDEO_OPTION2 -pix_fmt yuv420p logo.mkv
				# Fix incorrect width for logo
				mkvmerge --display-dimensions 1:"$VIDEO_WIDTH"x"$VIDEO_HEIGHT" logo.mkv -o logo2.mkv
				# Merge logo and capture
				mkvmerge -o "$CAP_VID".mkv logo2.mkv + video.mkv
				rm logo.ogg
				rm logo_temp.mkv
				rm logo.mkv
				rm logo2.mkv
				rm video_temp.mkv
			else
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -i audio.ogg -acodec copy -vcodec libx264 $VIDEO_OPTION2 -pix_fmt yuv420p "$CAP_VID".mkv
			fi
			rm video.png
			rm audio.ogg
			rm video.mkv
			rm *.log
			rm *.mbtree
		fi
		if [ "$VIDEO_ITEM" == "x264 (Hi10)/Vorbis" ]; then
			POL_SetupWindow_message "$(eval_gettext 'You MUST have a Hi10 compiled libx264\nfor this transcoding to work properly.\nDefault libx264 builds are Hi8 only.')" "$TITLE"
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -r 1 -f image2 video.png
			VIDEO_WIDTH=`exiftool video.png | grep "Image Width" | awk '{ print $4 }'`
			VIDEO_HEIGHT=`exiftool video.png | grep "Image Height" | awk '{ print $4 }'`
			WIDTH_16M=`echo $VIDEO_WIDTH/16 | bc`
			HEIGHT_16M=`echo $VIDEO_HEIGHT/16 | bc`
			VIDEO_WIDTH=`echo $WIDTH_16M*16 | bc`
			VIDEO_HEIGHT=`echo $HEIGHT_16M*16 | bc`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options for pass 1 \n (leave defaults if you do not know what to do)')" "$TITLE" "-vprofile high10 -level 5.1 -pass 1 -refs 16 -subq 10 -partitions +all-partp4x4 -me_method umh -me_range 24 -trellis 2 -b:v 1024k -bt 768k -s "$VIDEO_WIDTH"x"$VIDEO_HEIGHT" -r 25 -threads 2"
			VIDEO_OPTION1="$APP_ANSWER"
			VIDEO_WIDTH=`echo $VIDEO_OPTION1 | awk '{ print $24 }' | awk -F "x" '{ print $1 }'`
			VIDEO_HEIGHT=`echo $VIDEO_OPTION1 | awk '{ print $24 }' | awk -F "x" '{ print $2 }'`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options for pass 2 \n (leave defaults if you do not know what to do)')" "$TITLE" "-vprofile high10 -level 5.1 -pass 2 -refs 16 -subq 10 -partitions +all-partp4x4 -me_method umh -me_range 24 -trellis 2 -b:v 1024k -bt 768k -s "$VIDEO_WIDTH"x"$VIDEO_HEIGHT" -r 25 -threads 2"
			VIDEO_OPTION2="$APP_ANSWER"
			POL_SetupWindow_wait "$(eval_gettext 'Please wait while your video is being encoded...')" "$TITLE" 
			glc-play "$CAP_FILE" -o - -a 1 | ffmpeg -i - -acodec libvorbis -strict -2 -ac 2 -ab 128k -ar 44100 audio.ogg
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -vcodec libx264 -an $VIDEO_OPTION1 -fastfirstpass 1 -pix_fmt yuv420p video_temp.mkv
			if [ "$ADD_LOGO" == "1" ]; then
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -i audio.ogg -acodec copy -vcodec libx264 $VIDEO_OPTION2 -pix_fmt yuv420p video.mkv
				ffmpeg -i logo.wav -acodec libvorbis -strict -2 -ac 2 -ab 128k -ar 44100 logo.ogg
				ffmpeg -i logo.mp4 -vcodec libx264 -an $VIDEO_OPTION1 -fastfirstpass 1 -pix_fmt yuv420p logo_temp.mkv
				ffmpeg -i logo.mp4 -i logo.ogg -acodec copy -vcodec libx264 $VIDEO_OPTION2 -pix_fmt yuv420p logo.mkv
				# Fix incorrect width for logo
				mkvmerge --display-dimensions 1:"$VIDEO_WIDTH"x"$VIDEO_HEIGHT" logo.mkv -o logo2.mkv
				# Merge logo and capture
				mkvmerge -o "$CAP_VID".mkv logo2.mkv + video.mkv
				rm logo.ogg
				rm logo_temp.mkv
				rm logo.mkv
				rm logo2.mkv
				rm video_temp.mkv
			else
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -i audio.ogg -acodec copy -vcodec libx264 $VIDEO_OPTION2 -pix_fmt yuv420p "$CAP_VID".mkv
			fi
			rm video.png
			rm audio.ogg
			rm video.mkv
			rm *.log
			rm *.mbtree
		fi
		if [ "$VIDEO_ITEM" == "XviD/MP3" ]; then
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -r 1 -f image2 video.png
			VIDEO_WIDTH=`exiftool video.png | grep "Image Width" | awk '{ print $4 }'`
			VIDEO_HEIGHT=`exiftool video.png | grep "Image Height" | awk '{ print $4 }'`
			WIDTH_16M=`echo $VIDEO_WIDTH/16 | bc`
			HEIGHT_16M=`echo $VIDEO_HEIGHT/16 | bc`
			VIDEO_WIDTH=`echo $WIDTH_16M*16 | bc`
			VIDEO_HEIGHT=`echo $HEIGHT_16M*16 | bc`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options for pass 1 \n (leave defaults if you do not know what to do)')" "$TITLE" "-pass 1 -b:v 1024k -bt 768k -s "$VIDEO_WIDTH"x"$VIDEO_HEIGHT" -r 25 -threads 2"
			VIDEO_OPTION1="$APP_ANSWER"
			VIDEO_WIDTH=`echo $VIDEO_OPTION1 | awk '{ print $8 }' | awk -F "x" '{ print $1 }'`
			VIDEO_HEIGHT=`echo $VIDEO_OPTION1 | awk '{ print $8 }' | awk -F "x" '{ print $2 }'`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options for pass 2 \n (leave defaults if you do not know what to do)')" "$TITLE" "-pass 2 -b:v 1024k -bt 768k -s "$VIDEO_WIDTH"x"$VIDEO_HEIGHT" -r 25 -threads 2"
			VIDEO_OPTION2="$APP_ANSWER"
			POL_SetupWindow_wait "$(eval_gettext 'Please wait while your video is being encoded...')" "$TITLE"
			glc-play "$CAP_FILE" -o - -a 1 | ffmpeg -i - -acodec libmp3lame -ac 2 -ab 128k -ar 44100 audio.mp3
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -vcodec mpeg4 -vtag xvid -an $VIDEO_OPTION1 video_temp.avi
			if [ "$ADD_LOGO" == "1" ]; then
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -i audio.mp3 -acodec copy -vcodec mpeg4 -vtag xvid $VIDEO_OPTION2 video.avi
				ffmpeg -i logo.wav -acodec libmp3lame -ac 2 -ab 128k -ar 44100 logo.mp3
				ffmpeg -i logo.mp4 -vcodec mpeg4 -vtag xvid $VIDEO_OPTION1 logo_temp.avi
				ffmpeg -i logo.mp4 -i logo.mp3 -acodec copy -vcodec mpeg4 -vtag xvid $VIDEO_OPTION2 logo.avi
				avimerge -o "$CAP_VID".avi -i logo.avi video.avi
				rm logo.mp3
				rm logo_temp.avi
				rm logo.avi
				rm video_temp.avi
			else
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -i audio.mp3 -acodec copy -vcodec mpeg4 -vtag xvid $VIDEO_OPTION2 "$CAP_VID".avi
			fi
			rm video.png
			rm audio.mp3
			rm video.avi
			rm *.log
		fi
		if [ "$VIDEO_ITEM" == "Theora/Vorbis" ]; then
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -r 1 -f image2 video.png
			VIDEO_WIDTH=`exiftool video.png | grep "Image Width" | awk '{ print $4 }'`
			VIDEO_HEIGHT=`exiftool video.png | grep "Image Height" | awk '{ print $4 }'`
			WIDTH_16M=`echo $VIDEO_WIDTH/16 | bc`
			HEIGHT_16M=`echo $VIDEO_HEIGHT/16 | bc`
			VIDEO_WIDTH=`echo $WIDTH_16M*16 | bc`
			VIDEO_HEIGHT=`echo $HEIGHT_16M*16 | bc`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options \n (leave defaults if you do not know what to do)')" "$TITLE" "-v 7 -x $VIDEO_WIDTH -y $VIDEO_HEIGHT --optimize -V 1024 --pp ac -F 25"
			VIDEO_OPTION="$APP_ANSWER"
			POL_SetupWindow_wait "$(eval_gettext 'Please wait while your video is being encoded...')" "$TITLE"
			glc-play "$CAP_FILE" -o - -a 1 | ffmpeg2theora - -c 2 -A 192 -H 44100 -F 25 -o audio.ogg
			if [ "$ADD_LOGO" == "1" ]; then
				ffmpeg2theora logo.wav -c 2 -A 192 -H 44100 -F 25 -o logo.ogg
				ffmpeg2theora logo.mp4 --noaudio $VIDEO_OPTION -o logo_temp.ogv
				oggz-merge -o logo.ogv logo_temp.ogv logo.ogg
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg2theora - --noaudio $VIDEO_OPTION -o video_temp.ogv
				oggz-merge -o video.ogv video_temp.ogv audio.ogg
				oggCat "$CAP_VID".ogv logo.ogv video.ogv
				rm logo.ogg
				rm logo_temp.ogv
				rm logo.ogv
				rm video_temp.ogv
			else
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg2theora - $VIDEO_OPTION -o video.ogv
				oggz-merge -o "$CAP_VID".ogv video.ogv audio.ogg
			fi
			rm video.png
			rm audio.ogg
			rm video.ogv
		fi
		if [ "$VIDEO_ITEM" == "VP8/Vorbis" ]; then
			POL_SetupWindow_message "$(eval_gettext 'You MUST have VP8 codec (libvpx)\nfor this transcoding to work properly.')" "$TITLE"
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -r 1 -f image2 video.png
			VIDEO_WIDTH=`exiftool video.png | grep "Image Width" | awk '{ print $4 }'`
			VIDEO_HEIGHT=`exiftool video.png | grep "Image Height" | awk '{ print $4 }'`
			WIDTH_16M=`echo $VIDEO_WIDTH/16 | bc`
			HEIGHT_16M=`echo $VIDEO_HEIGHT/16 | bc`
			VIDEO_WIDTH=`echo $WIDTH_16M*16 | bc`
			VIDEO_HEIGHT=`echo $HEIGHT_16M*16 | bc`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options for pass 1 \n (leave defaults if you do not know what to do)')" "$TITLE" "-quality best -level 116 -pass 1 -rc_lookahead 16 -keyint_min 0 -g 120 -skip_threshold 0 -speed 0 -cpu-used 0 -b:v 1024k -bt 768k -s "$VIDEO_WIDTH"x"$VIDEO_HEIGHT" -r 25 -threads 2"
			VIDEO_OPTION1="$APP_ANSWER"
			VIDEO_WIDTH=`echo $VIDEO_OPTION1 | awk '{ print $24 }' | awk -F "x" '{ print $1 }'`
			VIDEO_HEIGHT=`echo $VIDEO_OPTION1 | awk '{ print $24 }' | awk -F "x" '{ print $2 }'`
			POL_SetupWindow_textbox "$(eval_gettext 'Define your encoding options for pass 2 \n (leave defaults if you do not know what to do)')" "$TITLE" "-quality best -level 116 -pass 2 -rc_lookahead 16 -keyint_min 0 -g 120 -skip_threshold 0 -speed 0 -cpu-used 0 -b:v 1024k -bt 768k -s "$VIDEO_WIDTH"x"$VIDEO_HEIGHT" -r 25 -threads 2"
			VIDEO_OPTION2="$APP_ANSWER"
			POL_SetupWindow_wait "$(eval_gettext 'Please wait while your video is being encoded...')" "$TITLE" 
			glc-play "$CAP_FILE" -o - -a 1 | ffmpeg -i - -acodec libvorbis -strict -2 -ac 2 -ab 128k -ar 44100 audio.ogg
			glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -vcodec libvpx -an $VIDEO_OPTION1 video_temp.webm
			if [ "$ADD_LOGO" == "1" ]; then
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -i audio.ogg -acodec copy -vcodec libvpx $VIDEO_OPTION2 video.webm
				ffmpeg -i logo.wav -acodec libvorbis -strict -2 -ac 2 -ab 128k -ar 44100 logo.ogg
				ffmpeg -i logo.mp4 -vcodec libvpx -an $VIDEO_OPTION1 logo_temp.webm
				ffmpeg -i logo.mp4 -i logo.ogg -acodec copy -vcodec libvpx $VIDEO_OPTION2 logo.webm
				# Fix incorrect width for logo
				mkvmerge --webm --display-dimensions 1:"$VIDEO_WIDTH"x"$VIDEO_HEIGHT" logo.webm -o logo2.webm
				# Merge logo and capture
				mkvmerge --webm -o "$CAP_VID".webm logo2.webm + video.webm
				rm logo.ogg
				rm logo_temp.webm
				rm logo.webm
				rm logo2.webm
				rm video_temp.webm
			else
				glc-play "$CAP_FILE" -o - -y 1 | ffmpeg -i - -i audio.ogg -acodec copy -vcodec libx264 $VIDEO_OPTION2 "$CAP_VID".webm
			fi
			rm video.png
			rm audio.ogg
			rm video.webm
			rm *.log
		fi
		POL_SetupWindow_message "$(eval_gettext 'Conversion done')" "$TITLE"
		menu_capture
	fi
	menu_capture
}

menu_capture_glc ()
{
	POL_SetupWindow_menu "$(eval_gettext 'Choose an action to perform')" "$TITLE" "$(eval_gettext 'Begin the capture of an application')-$(eval_gettext 'Play a captured file')-$(eval_gettext 'Convert a captured file to video')" "-"
	CAP_ITEM="$APP_ANSWER"
	if [ "$CAP_ITEM" == "$(eval_gettext 'Begin the capture of an application')" ]; then
		assign_cap_glc
	fi
	if [ "$CAP_ITEM" == "$(eval_gettext 'Play a captured file')" ]; then
		play_cap_glc
	fi
	if [ "$CAP_ITEM" == "$(eval_gettext 'Convert a captured file to video')" ]; then
		convert_cap_glc
	fi
	exit
}

# Capture for non-OpenGL applications
assign_cap_rmd ()
{	
	if [ ! -e "$CAP_FOLDER" ]; then
	mkdir -p "$CAP_FOLDER"
	fi
	cd "$POL_USER_ROOT/tmp/"
	unset GAME

	POL_SetupWindow_shortcuts_list "$(eval_gettext 'Which application do you want to apply the modification to?')" "$TITLE"
	GAME="$APP_ANSWER"
	if [ "$GAME" != "" ]; then
		GAME_CAP="`echo "$GAME"`-Capture"
		COUNT_LINE=`wc -l "$POL_USER_ROOT/shortcuts/$GAME" | awk '{ print($1) }'`
		COUNT_LINE1=`echo $COUNT_LINE - 1 | bc`
		rm -rf "$POL_USER_ROOT/tmp/$GAME_CAP"
		touch "$POL_USER_ROOT/tmp/$GAME_CAP"
		cat "$POL_USER_ROOT/shortcuts/$GAME" | head -n $COUNT_LINE1 > "$POL_USER_ROOT/tmp/$GAME_CAP"
		EXE_CAP=`cat "$POL_USER_ROOT/shortcuts/$GAME" | tail -n 1`
		echo "$EXE_CAP &" >> "$POL_USER_ROOT/tmp/$GAME_CAP"
		bash "$GAME_CAP"
		POL_SetupWindow_message "$(eval_gettext 'Press Next then click on the window\nyou want to capture.\nCapture will start automatically after 5 seconds.\nOnly video will be captured.')" "$TITLE"
		CURRENT_ID=`xwininfo | grep "Window id:" | awk '{ print $4 }'`
		POL_SetupWindow_wait "$(eval_gettext 'Capture in progress...')" "$TITLE" 
		recordmydesktop --delay 5 --stop-shortcut Shift+F8 --fps 25 --v_bitrate 1024000 --no-sound --overwrite --no-wm-check --full-shots --workdir . --windowid=$CURRENT_ID -o "$CAP_FOLDER/$GAME_CAP".ogv
		RMD_ON="1"
		while [ -e "$RMD_ON" ]
		do
		sleep 10
		RMD_ON=`ps aux | grep "recordmydesktop" | grep -v "grep"`
		if [ "$RMD_ON" == "" ];then
			unset RMD_ON
		fi
		done
		CAP_TIME=`date +%D-%T | tr "/" "-"\ | tr ":" "-"`
		GAME_CAP2="`echo "$GAME_CAP"-"$CAP_TIME"`"
		mv "$CAP_FOLDER/$GAME_CAP".ogv "$CAP_FOLDER/$GAME_CAP2".ogv
		CAP_FILE=`echo "$GAME_CAP2"`
		convert_cap_rmd
	else
		menu_capture_rmd
	fi
	menu_capture
}

play_cap_rmd ()
{
	cd "$CAP_FOLDER"
	POL_SetupWindow_browse "$(eval_gettext 'Please choose an OGV capture file to play')" "$TITLE" ""
	CAP_FILE="$APP_ANSWER"
	if [ -z "$CAP_FILE" ];then
		POL_SetupWindow_message "$(eval_gettext 'You must choose a capture to read')" "$TITLE"
		menu_capture_rmd
	else
		POL_SetupWindow_wait "$(eval_gettext 'Reading in progress...')" "$TITLE"
		mplayer "$CAP_FILE"
	fi
	menu_capture
}

convert_cap_rmd ()
{
	cd "$CAP_FOLDER"
	POL_SetupWindow_menu_list "$(eval_gettext 'Add the PlayOnLinux logo to your video?')" "Logo" "$(eval_gettext 'Yes')-$(eval_gettext 'No')" "-"
	if [ "$APP_ANSWER" == "$(eval_gettext 'Yes')" ]; then
		ADD_LOGO="1"
		make_logo
	else
		ADD_LOGO="0"
	fi	
	CAP_VID=`echo "$CAP_FILE" | cut -d . -f 1`
	ffmpeg -i "$CAP_VID".ogv -r 1 -f image2 video.png
	VIDEO_WIDTH=`exiftool video.png | grep "Image Width" | awk '{ print $4 }'`
	VIDEO_HEIGHT=`exiftool video.png | grep "Image Height" | awk '{ print $4 }'`
	WIDTH_16M=`echo $VIDEO_WIDTH/16 | bc`
	HEIGHT_16M=`echo $VIDEO_HEIGHT/16 | bc`
	VIDEO_WIDTH=`echo $WIDTH_16M*16 | bc`
	VIDEO_HEIGHT=`echo $HEIGHT_16M*16 | bc`
	 POL_SetupWindow_wait "$(eval_gettext 'Please wait while your video is being encoded...')" "$TITLE"
	if [ "$ADD_LOGO" == "1" ]; then
		ffmpeg2theora logo.mp4 --noaudio -v 7 --optimize -V 1024 -x $VIDEO_WIDTH -y $VIDEO_HEIGHT --pp ac -F 2500/100 -o logo.ogv
		oggCat "$CAP_VID"-final.ogv logo.ogv "$CAP_VID".ogv
		rm logo.ogv
		rm video.png
	fi
	POL_SetupWindow_message "$(eval_gettext 'Conversion done')" "$TITLE"
	menu_capture
}

menu_capture_rmd ()
{
	POL_SetupWindow_menu "$(eval_gettext 'Choose an action to perform')" "$TITLE" "$(eval_gettext 'Begin the capture of an application')-$(eval_gettext 'Play a captured file')" "-"
	CAP_ITEM="$APP_ANSWER"
	if [ "$CAP_ITEM" == "$(eval_gettext 'Begin the capture of an application')" ]; then
		assign_cap_rmd
	fi
	if [ "$CAP_ITEM" == "$(eval_gettext 'Play a captured file')" ]; then
		play_cap_rmd
	fi
	exit
}

menu_capture ()
{
	POL_SetupWindow_menu "$(eval_gettext 'Choose an action to perform')" "$TITLE" "$(eval_gettext 'Capture menu (OpenGL)')-$(eval_gettext 'Capture menu (not OpenGL)')" "-"
	CAP_ITEM="$APP_ANSWER"
	if [ "$CAP_ITEM" == "$(eval_gettext 'Capture menu (OpenGL)')" ]; then
		menu_capture_glc
	fi
	if [ "$CAP_ITEM" == "$(eval_gettext 'Capture menu (not OpenGL)')" ]; then
		menu_capture_rmd
	fi
	exit
}
