diff options
Diffstat (limited to 'src/livestreamer/cli.py')
-rw-r--r-- | src/livestreamer/cli.py | 74 |
1 files changed, 61 insertions, 13 deletions
diff --git a/src/livestreamer/cli.py b/src/livestreamer/cli.py index d8d1bc0..75f645e 100644 --- a/src/livestreamer/cli.py +++ b/src/livestreamer/cli.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 -import sys, os +import sys, os, pbs import livestreamer +from livestreamer.compat import input parser = livestreamer.utils.ArgumentParser(description="Util to play various livestreaming services in a custom player", fromfile_prefix_chars="@") @@ -9,7 +10,6 @@ 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("-c", "--cmdline", action="store_true", help="print commandline used internally to play stream") parser.add_argument("-l", "--plugins", action="store_true", help="print installed plugins") RCFILE = os.path.expanduser("~/.livestreamerrc") @@ -18,6 +18,33 @@ def exit(msg): sys.stderr.write("error: " + msg + "\n") sys.exit() +def msg(msg): + sys.stderr.write(msg + "\n") + +def write_stream(fd, out, progress): + written = 0 + + while True: + data = fd.read(8192) + if len(data) == 0: + break + + try: + out.write(data) + except IOError: + break + + written += len(data) + + if progress: + sys.stdout.write(("\rWritten {0} bytes").format(written)) + + if progress and written > 0: + sys.stdout.write("\n") + + fd.close() + out.close() + def handle_url(args): try: channel = livestreamer.resolve_url(args.url) @@ -39,26 +66,47 @@ def handle_url(args): if args.stream: if args.stream in streams: stream = streams[args.stream] - cmdline = stream.cmdline(args.output or "-") - if args.cmdline: - print(cmdline.format()) - sys.exit() + 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)) else: - if not args.output: - cmdline.pipe = ("{0} -").format(args.player) + cmd = args.player + " -" + player = pbs.sh("-c", cmd, _bg=True, _out=sys.stdout, _err=sys.stderr) + out = player.process.stdin - os.system(cmdline.format()) + try: + write_stream(fd, out, progress) + except KeyboardInterrupt: + sys.exit() else: - print(("This channel does not have stream: {0}").format(args.stream)) - print(("Valid streams: {0}").format(validstreams)) + msg(("This channel does not have stream: {0}").format(args.stream)) + msg(("Valid streams: {0}").format(validstreams)) else: - print(("Found streams: {0}").format(validstreams)) + msg(("Found streams: {0}").format(validstreams)) def print_plugins(): pluginlist = list(livestreamer.get_plugins().keys()) - print(("Installed plugins: {0}").format(", ".join(pluginlist))) + msg(("Installed plugins: {0}").format(", ".join(pluginlist))) def main(): |