Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add --list and --fullscreen options to media utility.

  • Loading branch information...
commit d7f926561ec4745abbeb6ed4081e0965a4dd368a 1 parent 359b047
Mike Sampson authored

Showing 1 changed file with 37 additions and 8 deletions. Show diff stats Hide diff stats

  1. +37 8 media
45 media
@@ -17,30 +17,56 @@
17 17
18 18 import sys
19 19 import glob
  20 +import os.path
  21 +import argparse
20 22 import mimetypes
21 23 import subprocess
22 24
  25 +def parse_args():
  26 + parser = argparse.ArgumentParser(description =
  27 + 'a simple mplayer launcher.')
  28 + parser.add_argument('filename', metavar='FILE', type=str, nargs = '?',
  29 + help='file to play, if not provided allow user to select from media files in current directory.')
  30 + parser.add_argument('-l', '--list', action = 'store_true',
  31 + help = 'list media files in current directory.')
  32 + parser.add_argument('-f', '--fullscreen', action = 'store_true',
  33 + help = 'launch mplayer fullscreen.')
  34 +
  35 + return parser.parse_args()
  36 +
23 37 def mplayer(filename):
24 38 fd = open('/dev/null')
25   - subprocess.Popen(['mplayer', filename], stdin = fd, stdout = fd, stderr = fd)
  39 + if args.fullscreen:
  40 + command = ['mplayer', '-fs', filename]
  41 + else:
  42 + command = ['mplayer', filename]
  43 + subprocess.Popen(command, stdin = fd, stdout = fd, stderr = fd)
26 44 fd.close()
27 45
28   -def select_media():
  46 +def get_media_files():
29 47 def fn(filename):
30 48 mt = mimetypes.guess_type(filename)[0]
31 49 return mt != None and mt.startswith('video')
32 50
33 51 files = list(filter(fn, glob.glob('*')))
34 52
  53 + files.sort()
  54 +
  55 + return files
  56 +
  57 +def list_media(allow_selection = False):
  58 + files = get_media_files()
  59 +
35 60 if not files:
36 61 print('No media files found in current directory.')
37 62 return
38 63
39   - files.sort()
40   -
41   - for i,f in enumerate(files):
  64 + for i, f in enumerate(files):
42 65 print('[%2d] - %s' % (i, f))
43 66
  67 + if not allow_selection:
  68 + return
  69 +
44 70 selection = input('\nSelect file to play (0 to %d, q to quit): ' % (len(files) - 1))
45 71 if selection == 'q':
46 72 return
@@ -51,7 +77,10 @@ def select_media():
51 77 except:
52 78 print('Invalid selection.')
53 79
54   -if len(sys.argv) == 1:
55   - select_media()
  80 +args = parse_args()
  81 +if args.filename and os.path.isfile(args.filename):
  82 + mplayer(args.filename)
  83 +elif args.filename:
  84 + print('Error: %s not found.' % args.filename)
56 85 else:
57   - mplayer(sys.argv[1])
  86 + list_media(not args.list)

0 comments on commit d7f9265

Please sign in to comment.
Something went wrong with that request. Please try again.