mirror of https://github.com/streamlink/streamlink
Add support for writing stream to stdout.
This commit is contained in:
parent
dbe5e67d26
commit
7802a20799
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
import sys, os, pbs
|
import sys, os, pbs
|
||||||
import livestreamer
|
import livestreamer
|
||||||
from livestreamer.compat import input
|
from livestreamer.compat import input, stdout
|
||||||
|
|
||||||
parser = livestreamer.utils.ArgumentParser(description="Util to play various livestreaming services in a custom player",
|
parser = livestreamer.utils.ArgumentParser(description="Util to play various livestreaming services in a custom player",
|
||||||
fromfile_prefix_chars="@")
|
fromfile_prefix_chars="@")
|
||||||
parser.add_argument("url", help="URL to stream", nargs="?")
|
parser.add_argument("url", help="URL to stream", nargs="?")
|
||||||
parser.add_argument("stream", help="stream to play", nargs="?")
|
parser.add_argument("stream", help="stream to play", nargs="?")
|
||||||
parser.add_argument("-p", "--player", metavar="player", help="commandline for player", default="vlc")
|
parser.add_argument("-p", "--player", metavar="player", help="commandline for player", default="vlc")
|
||||||
parser.add_argument("-o", "--output", metavar="filename", help="write stream to file instead of playing it")
|
parser.add_argument("-o", "--output", metavar="filename", help="write stream to file instead of playing it, use - for stdout")
|
||||||
parser.add_argument("-l", "--plugins", action="store_true", help="print installed plugins")
|
parser.add_argument("-l", "--plugins", action="store_true", help="print installed plugins")
|
||||||
|
|
||||||
RCFILE = os.path.expanduser("~/.livestreamerrc")
|
RCFILE = os.path.expanduser("~/.livestreamerrc")
|
||||||
|
@ -37,13 +37,31 @@ def write_stream(fd, out, progress):
|
||||||
written += len(data)
|
written += len(data)
|
||||||
|
|
||||||
if progress:
|
if progress:
|
||||||
sys.stdout.write(("\rWritten {0} bytes").format(written))
|
sys.stderr.write(("\rWritten {0} bytes").format(written))
|
||||||
|
|
||||||
if progress and written > 0:
|
if progress and written > 0:
|
||||||
sys.stdout.write("\n")
|
sys.stderr.write("\n")
|
||||||
|
|
||||||
fd.close()
|
fd.close()
|
||||||
out.close()
|
|
||||||
|
if out != stdout:
|
||||||
|
out.close()
|
||||||
|
|
||||||
|
def check_output(output):
|
||||||
|
if os.path.isfile(output):
|
||||||
|
sys.stderr.write(("File output {0} already exists! Overwrite it? [y/N] ").format(output))
|
||||||
|
answer = input()
|
||||||
|
answer = answer.strip().lower()
|
||||||
|
|
||||||
|
if answer != "y":
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
try:
|
||||||
|
out = open(output, "wb")
|
||||||
|
except IOError as err:
|
||||||
|
exit(("Failed to open file {0} - ").format(output, err))
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
def handle_url(args):
|
def handle_url(args):
|
||||||
try:
|
try:
|
||||||
|
@ -66,33 +84,28 @@ def handle_url(args):
|
||||||
if args.stream:
|
if args.stream:
|
||||||
if args.stream in streams:
|
if args.stream in streams:
|
||||||
stream = streams[args.stream]
|
stream = streams[args.stream]
|
||||||
|
progress = False
|
||||||
|
out = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fd = stream.open()
|
fd = stream.open()
|
||||||
except livestreamer.StreamError as err:
|
except livestreamer.StreamError as err:
|
||||||
exit(("Could not open stream - {0}").format(err))
|
exit(("Could not open stream - {0}").format(err))
|
||||||
|
|
||||||
progress = False
|
|
||||||
|
|
||||||
if args.output:
|
if args.output:
|
||||||
progress = True
|
if args.output == "-":
|
||||||
|
out = stdout
|
||||||
if os.path.exists(args.output):
|
else:
|
||||||
answer = input(("File output {0} already exists! Overwrite it? [y/N] ").format(args.output))
|
out = check_output(args.output)
|
||||||
answer = answer.strip().lower()
|
progress = True
|
||||||
|
|
||||||
if answer != "y":
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
try:
|
|
||||||
out = open(args.output, "wb")
|
|
||||||
except IOError as err:
|
|
||||||
exit(("Failed to open file {0} - ").format(args.output, err))
|
|
||||||
else:
|
else:
|
||||||
cmd = args.player + " -"
|
cmd = args.player + " -"
|
||||||
player = pbs.sh("-c", cmd, _bg=True, _out=sys.stdout, _err=sys.stderr)
|
player = pbs.sh("-c", cmd, _bg=True, _out=sys.stdout, _err=sys.stderr)
|
||||||
out = player.process.stdin
|
out = player.process.stdin
|
||||||
|
|
||||||
|
if not out:
|
||||||
|
exit("Failed to open a valid stream output")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
write_stream(fd, out, progress)
|
write_stream(fd, out, progress)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|
|
@ -6,16 +6,18 @@ is_py2 = (sys.version_info[0] == 2)
|
||||||
is_py3 = (sys.version_info[0] == 3)
|
is_py3 = (sys.version_info[0] == 3)
|
||||||
|
|
||||||
if is_py2:
|
if is_py2:
|
||||||
str = unicode
|
|
||||||
input = raw_input
|
input = raw_input
|
||||||
|
stdout = sys.stdout
|
||||||
|
str = unicode
|
||||||
|
|
||||||
def bytes(b, enc="ascii"):
|
def bytes(b, enc="ascii"):
|
||||||
return str(b)
|
return str(b)
|
||||||
|
|
||||||
elif is_py3:
|
elif is_py3:
|
||||||
str = str
|
|
||||||
bytes = bytes
|
bytes = bytes
|
||||||
input = input
|
input = input
|
||||||
|
stdout = sys.stdout.buffer
|
||||||
|
str = str
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import urllib.request as urllib
|
import urllib.request as urllib
|
||||||
|
|
Loading…
Reference in New Issue