aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Christopher Rosell <chrippa@tanuki.se>2012-05-26 21:44:15 +0200
committerGravatar Christopher Rosell <chrippa@tanuki.se>2012-05-26 21:44:15 +0200
commit7802a2079970057831ecd5d4bf46f96e48d8eb18 (patch)
tree3843184c26b923f6735bdad512f779cfc94ed652
parentdbe5e67d2685083769e7d154926e6a1a234fa3c4 (diff)
Add support for writing stream to stdout.
-rw-r--r--src/livestreamer/cli.py53
-rw-r--r--src/livestreamer/compat.py6
2 files changed, 37 insertions, 22 deletions
diff --git a/src/livestreamer/cli.py b/src/livestreamer/cli.py
index 75f645e..8b5ba2d 100644
--- a/src/livestreamer/cli.py
+++ b/src/livestreamer/cli.py
@@ -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:
diff --git a/src/livestreamer/compat.py b/src/livestreamer/compat.py
index e9c8c78..4e007fa 100644
--- a/src/livestreamer/compat.py
+++ b/src/livestreamer/compat.py
@@ -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