Casting YouTube to Chromecast on Macos (with VLC)

I have a Chromecast. I use Firefox. The only extension for casting content from Firefox that I found is fx_cast, but I wasn’t able to get it to work properly :(

This post describes a solution I hacked together to cast video without Chrome (and without an Android emulator). It relies on VLC to do the actual casting. VLC is capable of (a) opening video from a network stream, and (b) using a Chromecast as the video renderer.

The script just copies the URL of the current Firefox tab and then invokes the VLC command line interface, passing in the video URL. I invoke the script with Alfred when FF is browsing to a YouTube video (haven’t tested with other streams).

use scripting additions
use framework "Foundation"

on get_url_ff()
	tell application "Firefox" to activate
	
	set thePasteboard to current application's NSPasteboard's generalPasteboard()
	set theCount to thePasteboard's changeCount()
	
	tell application "System Events"
		keystroke "l" using {command down}
		delay 0.2
		keystroke "c" using {command down}
	end tell
	-- hacky heuristic to help ensure the URL is copied
	repeat 20 times
		if thePasteboard's changeCount() is not theCount then exit repeat
		delay 0.1
	end repeat
	
	set the_url to the clipboard
	
	return the_url as text
end get_url_ff

on chromecast_tab_ff()
	set the_url to get_url_ff()
	-- how to get chromecast IP https://old.reddit.com/r/Chromecast/comments/8nu0d7/how_to_find_chromecasts_ip/dzyenff/
	
	do shell script "/Applications/VLC.app/Contents/MacOS/VLC " & quoted form of the_url & " --sout \"#chromecast\" --sout-chromecast-ip=192.168.0.198 --demux-filter=demux_chromecast"
end chromecast_tab_ff

chromecast_tab_ff()