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 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",
|
||||
fromfile_prefix_chars="@")
|
||||
parser.add_argument("url", help="URL to stream", 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("-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")
|
||||
|
||||
RCFILE = os.path.expanduser("~/.livestreamerrc")
|
||||
|
@ -37,13 +37,31 @@ def write_stream(fd, out, progress):
|
|||
written += len(data)
|
||||
|
||||
if progress:
|
||||
sys.stdout.write(("\rWritten {0} bytes").format(written))
|
||||
sys.stderr.write(("\rWritten {0} bytes").format(written))
|
||||
|
||||
if progress and written > 0:
|
||||
sys.stdout.write("\n")
|
||||
sys.stderr.write("\n")
|
||||
|
||||
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):
|
||||
try:
|
||||
|
@ -66,33 +84,28 @@ def handle_url(args):
|
|||
if args.stream:
|
||||
if args.stream in streams:
|
||||
stream = streams[args.stream]
|
||||
progress = False
|
||||
out = None
|
||||
|
||||
try:
|
||||
fd = stream.open()
|
||||
except livestreamer.StreamError as err:
|
||||
exit(("Could not open stream - {0}").format(err))
|
||||
|
||||
progress = False
|
||||
|
||||
if args.output:
|
||||
progress = True
|
||||
|
||||
if os.path.exists(args.output):
|
||||
answer = input(("File output {0} already exists! Overwrite it? [y/N] ").format(args.output))
|
||||
answer = answer.strip().lower()
|
||||
|
||||
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))
|
||||
if args.output == "-":
|
||||
out = stdout
|
||||
else:
|
||||
out = check_output(args.output)
|
||||
progress = True
|
||||
else:
|
||||
cmd = args.player + " -"
|
||||
player = pbs.sh("-c", cmd, _bg=True, _out=sys.stdout, _err=sys.stderr)
|
||||
out = player.process.stdin
|
||||
|
||||
if not out:
|
||||
exit("Failed to open a valid stream output")
|
||||
|
||||
try:
|
||||
write_stream(fd, out, progress)
|
||||
except KeyboardInterrupt:
|
||||
|
|
|
@ -6,16 +6,18 @@ is_py2 = (sys.version_info[0] == 2)
|
|||
is_py3 = (sys.version_info[0] == 3)
|
||||
|
||||
if is_py2:
|
||||
str = unicode
|
||||
input = raw_input
|
||||
stdout = sys.stdout
|
||||
str = unicode
|
||||
|
||||
def bytes(b, enc="ascii"):
|
||||
return str(b)
|
||||
|
||||
elif is_py3:
|
||||
str = str
|
||||
bytes = bytes
|
||||
input = input
|
||||
stdout = sys.stdout.buffer
|
||||
str = str
|
||||
|
||||
try:
|
||||
import urllib.request as urllib
|
||||
|
|
Loading…
Reference in New Issue