cast.py
                        
                             · 1.5 KiB · Python
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            #!/usr/bin/env python
# requires pychromecast and probably python 2.7, sorry
import pychromecast
import argparse
def play_video(url, cast):
    if cast.media_controller.status.player_state == "PAUSED" or cast.media_controller.status.content_id == url:
        cast.media_controller.play()
    else:
        cast.play_media((url), "video/mp4")
def pause_video(cast):
    if cast.media_controller.status.supports_pause:
        cast.media_controller.pause()
    else:
        print "Cannot pause"
def stop_video(cast):
    cast.quit_app()
def main():
    casts = pychromecast.get_chromecasts_as_dict()
    parser = argparse.ArgumentParser()
    parser.add_argument("url", nargs="?", help="URL of media to play. Doesn't support local addresses yet.")
#    parser.add_argument("-p", "--pause", help="Pause playback", action='store_true')
    parser.add_argument("-s", "--stop", help="Stop playback", action='store_true')
    parser.add_argument("-d", "--device", help="Select device. List devices with -D")
    parser.add_argument("-D", "--devices", help="List devices", action='store_true')
    args = parser.parse_args()
    if args.devices:
        print ", ".join(casts.keys())
        return
    if args.device:
        cast = casts[args.device]
    else:
        cast = casts[casts.keys()[0]]
    if not args.stop:
        play_video(args.url, cast)
        return
#    elif args.pause:
#        pause_video(cast)
#        return
    elif args.stop:
        stop_video(cast)
        return
if __name__ == "__main__":
    main()
                | 1 | #!/usr/bin/env python | 
| 2 | # requires pychromecast and probably python 2.7, sorry | 
| 3 | |
| 4 | import pychromecast | 
| 5 | import argparse | 
| 6 | |
| 7 | def play_video(url, cast): | 
| 8 | if cast.media_controller.status.player_state == "PAUSED" or cast.media_controller.status.content_id == url: | 
| 9 | cast.media_controller.play() | 
| 10 | else: | 
| 11 | cast.play_media((url), "video/mp4") | 
| 12 | |
| 13 | def pause_video(cast): | 
| 14 | if cast.media_controller.status.supports_pause: | 
| 15 | cast.media_controller.pause() | 
| 16 | else: | 
| 17 | print "Cannot pause" | 
| 18 | |
| 19 | def stop_video(cast): | 
| 20 | cast.quit_app() | 
| 21 | |
| 22 | def main(): | 
| 23 | casts = pychromecast.get_chromecasts_as_dict() | 
| 24 | parser = argparse.ArgumentParser() | 
| 25 | parser.add_argument("url", nargs="?", help="URL of media to play. Doesn't support local addresses yet.") | 
| 26 | # parser.add_argument("-p", "--pause", help="Pause playback", action='store_true') | 
| 27 | parser.add_argument("-s", "--stop", help="Stop playback", action='store_true') | 
| 28 | parser.add_argument("-d", "--device", help="Select device. List devices with -D") | 
| 29 | parser.add_argument("-D", "--devices", help="List devices", action='store_true') | 
| 30 | args = parser.parse_args() | 
| 31 | if args.devices: | 
| 32 | print ", ".join(casts.keys()) | 
| 33 | return | 
| 34 | if args.device: | 
| 35 | cast = casts[args.device] | 
| 36 | else: | 
| 37 | cast = casts[casts.keys()[0]] | 
| 38 | if not args.stop: | 
| 39 | play_video(args.url, cast) | 
| 40 | return | 
| 41 | # elif args.pause: | 
| 42 | # pause_video(cast) | 
| 43 | # return | 
| 44 | elif args.stop: | 
| 45 | stop_video(cast) | 
| 46 | return | 
| 47 | |
| 48 | if __name__ == "__main__": | 
| 49 | main() |