20-test_videohash.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import pytest
  2. import os
  3. from videohash.videohash import VideoHash
  4. from videohash.exceptions import DidNotSupplyPathOrUrl, StoragePathDoesNotExist
  5. from videohash.utils import create_and_return_temporary_directory
  6. this_dir = os.path.dirname(os.path.realpath(__file__))
  7. def test_all():
  8. source1 = (
  9. "https://raw.githubusercontent.com/akamhy/videohash/main/assets/rocket.mkv"
  10. )
  11. videohash1 = VideoHash(url=source1, frame_interval=3)
  12. videohash1.delete_storage_path()
  13. hash1 = videohash1.hash
  14. hash_hex1 = videohash1.hash_hex
  15. assert hash1 == "0b1110000111100001111111111111111111111111000000000000000000000000"
  16. assert (
  17. str(videohash1)
  18. == "0b1110000111100001111111111111111111111111000000000000000000000000"
  19. )
  20. assert hash_hex1 == "0xe1e1ffffff000000"
  21. assert (
  22. videohash1
  23. - "0b1110000111100001111111111111111111111111000000000000000000000000"
  24. == 0
  25. )
  26. assert hash_hex1 in repr(videohash1)
  27. assert hash1 in repr(videohash1)
  28. assert (len(videohash1) - 2) == videohash1.bits_in_hash
  29. with pytest.raises(AssertionError):
  30. assert videohash1 != hash_hex1
  31. with pytest.raises(TypeError):
  32. _ = videohash1 - None
  33. with pytest.raises(ValueError):
  34. _ = videohash1 - hash1[0:-2]
  35. with pytest.raises(TypeError):
  36. _ = videohash1 - ("XX" + hash1[2:])
  37. with pytest.raises(TypeError):
  38. _ = videohash1 - True
  39. source2 = (
  40. this_dir
  41. + os.path.sep
  42. + os.path.pardir
  43. + os.path.sep
  44. + "assets"
  45. + os.path.sep
  46. + "rocket.mkv"
  47. )
  48. videohash2 = VideoHash(path=source2, frame_interval=3)
  49. hash2 = videohash2.hash
  50. hash_hex2 = videohash2.hash_hex
  51. assert hash2 == "0b1110000111100001111111111111111111111111000000000000000000000000"
  52. assert hash_hex2 == "0xe1e1ffffff000000"
  53. source3 = "https://www.youtube.com/watch?v=PapBjpzRhnA"
  54. videohash3 = VideoHash(url=source3)
  55. hash3 = videohash3.hash
  56. hash_hex3 = videohash3.hash_hex
  57. assert hash3 == "0b0011010000011111111011111111111110001111011110000000000000000000"
  58. assert hash_hex3 == "0x341fefff8f780000"
  59. assert hash1 == hash2
  60. assert videohash1 == videohash2.bitlist
  61. assert hash_hex1 == hash_hex2
  62. assert videohash1 - hash_hex3 != 2
  63. assert videohash1 == videohash2
  64. assert videohash1 - videohash3 != 2
  65. source4 = "https://www.youtube.com/watch?v=_T8cn2J13-4"
  66. videohash4 = VideoHash(url=source4, download_worst=True)
  67. hash4 = videohash4.hash
  68. assert hash4 != hash1
  69. assert hash4 != hash2
  70. assert hash4 != hash3
  71. assert videohash1 != videohash4
  72. assert videohash2 != videohash4
  73. assert videohash3 != videohash4
  74. with pytest.raises(ValueError):
  75. # not padded with 0x
  76. VideoHash.hex2bin("741fcfff8f780000", 64)
  77. with pytest.raises(ValueError):
  78. # not padded with 0b
  79. VideoHash.bin2hex("010101001")
  80. with pytest.raises(ValueError):
  81. # hamming distance not defined for unequal length bitlists
  82. _ = videohash1 - [1, 0, 1, 1, 1]
  83. class FakeVideoHashForTesting(VideoHash):
  84. def __init__(self, hash=None):
  85. self.hash = hash
  86. fake_videohash_object = FakeVideoHashForTesting(hash="0b0011010")
  87. fake_videohash_object.hamming_distance(string_a="0b1011010", string_b="0b1011010")
  88. fake_videohash_object = FakeVideoHashForTesting()
  89. with pytest.raises(ValueError):
  90. # hamming_distance is not defined.
  91. fake_videohash_object.hamming_distance(string_a="abc", string_b="abcd")
  92. with pytest.raises(ValueError):
  93. # hamming_distance is not defined.
  94. fake_videohash_object.hamming_distance(
  95. bitlist_a=[1, 0, 1, 1, 0], bitlist_b=[1, 0, 1, 1]
  96. )
  97. with pytest.raises(DidNotSupplyPathOrUrl):
  98. VideoHash(url=None, path=None)
  99. with pytest.raises(StoragePathDoesNotExist):
  100. storage_path = os.path.join(
  101. create_and_return_temporary_directory(),
  102. ("thisdirdoesnotexist" + os.path.sep),
  103. )
  104. VideoHash(url="https://example.com", storage_path=storage_path)
  105. with pytest.raises(ValueError):
  106. VideoHash(
  107. url="https://example.com", path=create_and_return_temporary_directory()
  108. )
  109. with pytest.raises(ValueError):
  110. path = os.path.join(
  111. create_and_return_temporary_directory(), "file_extension_less_video"
  112. )
  113. VideoHash(path=path)