ffmpeg_tests.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. # ##### BEGIN GPL LICENSE BLOCK #####
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software Foundation,
  16. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. #
  18. # ##### END GPL LICENSE BLOCK #####
  19. # <pep8 compliant>
  20. import argparse
  21. import pathlib
  22. import sys
  23. import unittest
  24. from modules.test_utils import AbstractBlenderRunnerTest
  25. class AbstractFFmpegTest(AbstractBlenderRunnerTest):
  26. @classmethod
  27. def setUpClass(cls):
  28. cls.blender = args.blender
  29. cls.testdir = pathlib.Path(args.testdir)
  30. class AbstractFFmpegSequencerTest(AbstractFFmpegTest):
  31. def get_script_for_file(self, filename: pathlib.Path) -> str:
  32. movie = self.testdir / filename
  33. return \
  34. "import bpy; " \
  35. "bpy.context.scene.sequence_editor_create(); " \
  36. "strip = bpy.context.scene.sequence_editor.sequences.new_movie(" \
  37. "'test_movie', %r, channel=1, frame_start=1); " \
  38. "print(f'fps:{strip.fps}'); " \
  39. "print(f'duration:{strip.frame_final_duration}'); " % movie.as_posix()
  40. def get_movie_file_field(self, filename: pathlib.Path, field: str) -> str:
  41. script = self.get_script_for_file(filename)
  42. output = self.run_blender('', script)
  43. prefix = field + ":"
  44. for line in output.splitlines():
  45. if line.startswith(prefix):
  46. return line.split(':')[1]
  47. return ""
  48. def get_movie_file_field_float(self, filename: pathlib.Path, field: str) -> float:
  49. return float(self.get_movie_file_field(filename, field))
  50. def get_movie_file_field_int(self, filename: pathlib.Path, field: str) -> float:
  51. return int(self.get_movie_file_field(filename, field))
  52. def get_movie_file_fps(self, filename: pathlib.Path) -> float:
  53. return self.get_movie_file_field_float(filename, "fps")
  54. def get_movie_file_duration(self, filename: pathlib.Path) -> float:
  55. return self.get_movie_file_field_int(filename, "duration")
  56. class FPSDetectionTest(AbstractFFmpegSequencerTest):
  57. def test_T51153(self):
  58. self.assertAlmostEqual(
  59. self.get_movie_file_fps('T51153_bad_clip_2.mts'),
  60. 29.97,
  61. places=2)
  62. def test_T53857(self):
  63. self.assertAlmostEqual(
  64. self.get_movie_file_fps('T53857_2018-01-22_15-30-49.mkv'),
  65. 30.0,
  66. places=2)
  67. def test_T54148(self):
  68. self.assertAlmostEqual(
  69. self.get_movie_file_fps('T54148_magn_0.mkv'),
  70. 1.0,
  71. places=2)
  72. def test_T54834(self):
  73. self.assertEqual(
  74. self.get_movie_file_duration('T54834.ogg'),
  75. 50)
  76. if __name__ == '__main__':
  77. parser = argparse.ArgumentParser()
  78. parser.add_argument('--blender', required=True)
  79. parser.add_argument('--testdir', required=True)
  80. args, remaining = parser.parse_known_args()
  81. unittest.main(argv=sys.argv[0:1] + remaining)