PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/play.py

https://gitlab.com/opensourcevideo/variabletv
Python | 61 lines | 45 code | 11 blank | 5 comment | 6 complexity | c8ea28e9ccbb5237d22e60d629e60e2d MD5 | raw file
  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. from subprocess import Popen, PIPE, STDOUT
  4. import sys, re, glob, os
  5. from random import choice, random
  6. from time import sleep
  7. FFMPEG = "ffmpeg"
  8. MPLAYER = "mplayer"
  9. VIDEOS = "/opt/piratebox/share/Shared/"
  10. dpat = re.compile(r"Duration:\s*(?P<h>\d\d):(?P<m>\d\d):(?P<s>\d\d(\.\d+)?)", re.I)
  11. EXT = "mp4 webm ogv ogg avi mov qt mpeg m2t"
  12. FRAGMENTDURATION = 10.0
  13. extensions = {}
  14. for ext in EXT.split():
  15. extensions[ext] = True
  16. def random_file ():
  17. ret = []
  18. for f in os.listdir(VIDEOS):
  19. if f.startswith("."):
  20. continue
  21. (base, ext) = os.path.splitext(f)
  22. ext = ext.lower().lstrip(".")
  23. if ext in extensions:
  24. ret.append(os.path.join(VIDEOS, f))
  25. return choice(ret)
  26. def ffmpeg_get_duration (path):
  27. cmd = '{0} -i "{1}"'.format(FFMPEG, path)
  28. p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
  29. data = p.stdout.read()
  30. # print "got"
  31. # print data
  32. m = dpat.search(data)
  33. if m:
  34. d = m.groupdict()
  35. ss = int(d.get("h"))* 3600
  36. ss += int(d.get("m"))*60
  37. ss += float(d.get("s"))
  38. return ss
  39. def play_random_fragment (p):
  40. totalduration = ffmpeg_get_duration(f)
  41. playduration = min(totalduration, FRAGMENTDURATION)
  42. start = random() * (totalduration-playduration)
  43. cmd = '{0} -fs -ss {1} -endpos {2} "{3}"'.format(MPLAYER, start, playduration, p)
  44. print cmd
  45. os.system(cmd)
  46. # path = sys.argv[1]
  47. while True:
  48. f = random_file()
  49. play_random_fragment(f)
  50. sleep(1)