PageRenderTime 31ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/DEPRECATED/popcorn/test/scaler.py

https://github.com/clones/kaa
Python | 63 lines | 40 code | 15 blank | 8 comment | 7 complexity | 327633cc30e3823b980d8f63e72d7d5e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. import os
  2. import sys
  3. import kaa.metadata
  4. import kaa.display
  5. SIZE = int(sys.argv[1]), int(sys.argv[2])
  6. window = kaa.display.X11Window(size = SIZE, title = "kaa.popcorn")
  7. window.show()
  8. def scale(video_width, video_height, window_width, window_height,
  9. video_aspect=None, window_aspect=None):
  10. """
  11. Scale the video to fit the window and respect the given aspects.
  12. """
  13. if not video_aspect:
  14. video_aspect = float(video_width) / video_height
  15. if not window_aspect:
  16. window_aspect = float(window_width) / window_height
  17. scaling = float(window_aspect * window_height) / float(window_width)
  18. # correct video width by aspect
  19. video_width = float(video_aspect * video_height)
  20. s1 = (float(window_width) / video_width)
  21. s2 = (float(window_height) / video_height)
  22. width = int((video_width * max(s1, s2)) / scaling)
  23. height = int(video_height * max(s1, s2))
  24. if width > window_width + 1:
  25. # Oops, need different way to scale
  26. # Note: + 1 because of some possible internal errors
  27. width = int(video_width * min(s1, s2))
  28. height = int(video_height * min(s1, s2) * scaling)
  29. # adjust width and height if off by one
  30. if width + 1 == window_width or width -1 == window_width:
  31. width = window_width
  32. if height + 1 == window_height or height -1 == window_height:
  33. height = window_height
  34. return width, height
  35. data = kaa.metadata.parse(sys.argv[3])
  36. video_width = data.video[0]['width']
  37. video_height = data.video[0]['height']
  38. video_aspect = None
  39. # video_aspect = float(16) / 9
  40. window_width, window_height = window.get_size()
  41. window_aspect = None
  42. window_aspect = float(16) / 9
  43. s = scale(video_width, video_height, window_width, window_height,
  44. video_aspect, window_aspect)
  45. filter = 'scale=%s:%s,expand=%s:%s' % (s[0], s[1], window_width, window_height)
  46. cmd = 'mplayer -wid %s -vf %s "%s"' % (hex(window.get_id()), filter, sys.argv[3])
  47. print cmd
  48. os.system(cmd)