Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

75 rader
3.0 KiB

  1. # Copyright 2015 Adafruit Industries.
  2. # Author: Tony DiCola
  3. # License: GNU GPLv2, see LICENSE.txt
  4. import os
  5. import subprocess
  6. import time
  7. class OMXPlayer(object):
  8. def __init__(self, config):
  9. """Create an instance of a video player that runs omxplayer in the
  10. background.
  11. """
  12. self._process = None
  13. self._load_config(config)
  14. def _load_config(self, config):
  15. self._extensions = config.get('omxplayer', 'extensions') \
  16. .translate(None, ' \t\r\n.') \
  17. .split(',')
  18. self._extra_args = config.get('omxplayer', 'extra_args').split()
  19. self._sound = config.get('omxplayer', 'sound').lower()
  20. assert self._sound in ('hdmi', 'local', 'both'), 'Unknown omxplayer sound configuration value: {0} Expected hdmi, local, or both.'.format(self._sound)
  21. def supported_extensions(self):
  22. """Return list of supported file extensions."""
  23. return self._extensions
  24. def play(self, movie, loop=False):
  25. """Play the provided movied file, optionally looping it repeatedly."""
  26. self.stop(3) # Up to 3 second delay to let the old player stop.
  27. # Assemble list of arguments.
  28. args = ['omxplayer']
  29. args.extend(['-o', self._sound]) # Add sound arguments.
  30. args.extend(self._extra_args) # Add extra arguments from config.
  31. if loop:
  32. args.append('--loop') # Add loop parameter if necessary.
  33. args.append(movie) # Add movie file path.
  34. # Run omxplayer process and direct standard output to /dev/null.
  35. self._process = subprocess.Popen(args,
  36. stdout=open(os.devnull, 'wb'),
  37. close_fds=True)
  38. def is_playing(self):
  39. """Return true if the video player is running, false otherwise."""
  40. if self._process is None:
  41. return False
  42. self._process.poll()
  43. return self._process.returncode is None
  44. def stop(self, block_timeout_sec=None):
  45. """Stop the video player. block_timeout_sec is how many seconds to
  46. block waiting for the player to stop before moving on.
  47. """
  48. # Stop the player if it's running.
  49. if self._process is not None and self._process.returncode is None:
  50. # process.kill() doesn't seem to work reliably if USB drive is
  51. # removed, instead just run a kill -9 on it.
  52. subprocess.call(['kill', '-9', str(self._process.pid)])
  53. # If a blocking timeout was specified, wait up to that amount of time
  54. # for the process to stop.
  55. start = time.time()
  56. while self._process is not None and self._process.returncode is None:
  57. if (time.time() - start) >= block_timeout_sec:
  58. break
  59. time.sleep(0)
  60. # Let the process be garbage collected.
  61. self._process = None
  62. def create_player(config):
  63. """Create new video player based on omxplayer."""
  64. return OMXPlayer(config)