Skip to content

Instantly share code, notes, and snippets.

@fabiocolacio
Last active February 14, 2020 22:06
Show Gist options
  • Save fabiocolacio/457737f6143609a65d368526f0289089 to your computer and use it in GitHub Desktop.
Save fabiocolacio/457737f6143609a65d368526f0289089 to your computer and use it in GitHub Desktop.
Stream audio or video from online sources
#!/usr/bin/perl
use strict;
if (scalar @ARGV < 1) {
print "No stream specified.\n";
exit 0;
}
sub show_help {
print <<~EOH;
USAGE
=====
$0 [args] [stream url/channel name]
Supports all streaming sites supported by youtube-dl.
The URL can be replaced with the name of a Twitch.tv channel to stream from.
ARGUMENTS
=========
-h --help Show this help menu
-q --quality Set the quality of the stream.
The following formats are available:
* worst
* best (default)
* worstvideo
* bestvideo
* worstaudio
* bestaudio
-p --player Set the media player to open the stream with.
Only players that can read from stdin are supported.
The default player is mpv.
EOH
}
my $url;
my $title;
my $quality = "best";
my $player = "mpv";
while (scalar @ARGV > 0) {
my $arg = shift;
if ($arg eq "-h" || $arg eq "--help") {
show_help;
exit 0;
}
elsif ($arg eq "-q" || $arg eq "--quality") {
$quality = shift;
}
elsif ($arg eq "-p" || $arg eq "--player") {
$player = shift;
}
else {
if ($arg =~ m[^https?://]) {
$url = $arg;
$url =~ /\/([^\/]+)$/;
$title = $1;
} else {
$title = $arg;
$url = "https://twitch.tv/$title";
}
if ($player eq "mpv") {
$player = "mpv --title=\"$title\"";
}
if (fork == 0) {
`youtube-dl -f $quality -o - "$url" 2> /dev/null | $player - 2> /dev/null`;
exit 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment