| 1 | """Nachbesprechen: Zeitleiste und PGN in der Opus-Datei.""" |
| 2 | |
| 3 | import os |
| 4 | import shutil |
| 5 | import tempfile |
| 6 | import unittest |
| 7 | |
| 8 | import chess |
| 9 | |
| 10 | from schach import recording |
| 11 | |
| 12 | M = chess.Move.from_uci |
| 13 | |
| 14 | |
| 15 | class TimelineTest(unittest.TestCase): |
| 16 | def test_steps_jumps_and_shapes_round_trip(self): |
| 17 | timeline = recording.Timeline() |
| 18 | timeline.show(0, (), ()) |
| 19 | timeline.show(1200, (M("e2e4"),), ()) |
| 20 | timeline.show(1200, (M("e2e4"),), ()) # unverändert: kein Eintrag |
| 21 | timeline.show(2000, (M("e2e4"), M("e7e5")), ((chess.F7, chess.F7, "red"), (chess.G1, chess.F3, "green"))) |
| 22 | timeline.show(3000, (M("e2e4"),), ((chess.F7, chess.F7, "red"), (chess.G1, chess.F3, "green"))) |
| 23 | timeline.show(4000, (M("d2d4"), M("d7d5")), ()) |
| 24 | text = timeline.encode() |
| 25 | self.assertEqual(text, "0 =;1200 >e2e4;2000 >e7e5;2000 #Rf7,Gg1f3;3000 <;4000 =d2d4,d7d5;4000 #") |
| 26 | frames = recording.decode(text) |
| 27 | self.assertEqual(recording.at(frames, 2500), ((M("e2e4"), M("e7e5")), |
| 28 | ((chess.F7, chess.F7, "red"), (chess.G1, chess.F3, "green")))) |
| 29 | self.assertEqual(recording.at(frames, 3500)[0], (M("e2e4"),)) |
| 30 | self.assertEqual(recording.at(frames, 9999), ((M("d2d4"), M("d7d5")), ())) |
| 31 | self.assertEqual(recording.at(frames, 0), ((), ())) |
| 32 | |
| 33 | |
| 34 | @unittest.skipUnless(shutil.which("gst-launch-1.0"), "GStreamer fehlt") |
| 35 | class OpusTest(unittest.TestCase): |
| 36 | def setUp(self): |
| 37 | import subprocess |
| 38 | self.dir = tempfile.mkdtemp() |
| 39 | self.path = os.path.join(self.dir, "ton.opus") |
| 40 | subprocess.run(["gst-launch-1.0", "-q", "audiotestsrc", "num-buffers=50", "!", "audioconvert", "!", |
| 41 | "opusenc", "!", "oggmux", "!", "filesink", f"location={self.path}"], check=True) |
| 42 | |
| 43 | def tearDown(self): |
| 44 | shutil.rmtree(self.dir) |
| 45 | |
| 46 | def test_pgn_survives_and_audio_stays(self): |
| 47 | with open(self.path, "rb") as f: |
| 48 | original = f.read() |
| 49 | pgn = '[Event "Test"]\n[Replay "0 =;500 >e2e4"]\n\n1. e4 { Idee } *\n' |
| 50 | for text in (pgn, pgn * 2000): # auch über mehrere Ogg-Seiten |
| 51 | data = recording.write_pgn(original, text) |
| 52 | self.assertEqual(recording.read_tags(data)["PGN"], text) |
| 53 | audio = [body for _head, _lacing, body in recording._pages(original)[2:]] |
| 54 | self.assertEqual([body for _head, _lacing, body in recording._pages(data)][-len(audio):], audio) |
| 55 | |
| 56 | |
| 57 | if __name__ == "__main__": |
| 58 | unittest.main() |
| 59 | |
| 60 | |
| 61 | @unittest.skipUnless(shutil.which("ffmpeg"), "ffmpeg fehlt") |
| 62 | class WebmTest(unittest.TestCase): |
| 63 | def test_pgn_from_matroska_tag(self): |
| 64 | import subprocess |
| 65 | pgn = '[Event "Test"]\n[Replay "0 =;500 >e2e4"]\n\n1. e4 { Idee } *\n' |
| 66 | with tempfile.TemporaryDirectory() as folder: |
| 67 | path = os.path.join(folder, "video.webm") |
| 68 | subprocess.run(["ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i", "sine=d=1", "-f", "lavfi", "-i", |
| 69 | "color=s=64x64:d=1", "-c:a", "libopus", "-c:v", "libvpx-vp9", "-shortest", |
| 70 | "-metadata", f"PGN={pgn}", path], check=True) |
| 71 | self.assertEqual(recording.read_pgn(path), pgn) |
| 72 | |
| 73 | |
| 74 | @unittest.skipUnless(shutil.which("ffmpeg") and shutil.which("gst-launch-1.0"), "ffmpeg oder GStreamer fehlt") |
| 75 | class ReviewVideoTest(unittest.TestCase): |
| 76 | def test_video_with_sound_and_pgn(self): |
| 77 | import io |
| 78 | import subprocess |
| 79 | import chess.pgn |
| 80 | from schach import export, savegames |
| 81 | board = chess.Board() |
| 82 | for san in ("e4", "e5", "Bc4"): |
| 83 | board.push_san(san) |
| 84 | state = savegames.GameState(board=board, human=chess.WHITE, elo=600) |
| 85 | with tempfile.TemporaryDirectory() as folder: |
| 86 | audio = os.path.join(folder, "ton.opus") |
| 87 | subprocess.run(["gst-launch-1.0", "-q", "audiotestsrc", "num-buffers=100", "!", "audioconvert", "!", |
| 88 | "opusenc", "!", "oggmux", "!", "filesink", f"location={audio}"], check=True) |
| 89 | with open(audio, "rb") as f: |
| 90 | length = recording.duration_ms(f.read()) |
| 91 | self.assertGreater(length, 1500) |
| 92 | timeline = "0 =;300 >e2e4;900 >e7e5;1200 #Rf7;1500 =e2e4" |
| 93 | pgn = savegames.to_pgn(state).replace("\n\n", f'\n[Replay "{timeline}"]\n\n', 1) |
| 94 | frames = recording.decode(chess.pgn.read_headers(io.StringIO(pgn))["Replay"]) |
| 95 | for fmt in ("webm", "mp4"): # beide öffnet die App wieder als Nachbesprechung |
| 96 | data = export.review_video(fmt, state, frames, length, audio, pgn, export.Options()) |
| 97 | path = os.path.join(folder, f"video.{fmt}") |
| 98 | with open(path, "wb") as f: |
| 99 | f.write(data) |
| 100 | self.assertEqual(recording.read_pgn(path), pgn) |
| 101 | streams = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "stream=codec_type", "-of", "csv=p=0", |
| 102 | path], capture_output=True, text=True).stdout.split() |
| 103 | self.assertEqual(sorted(streams), ["audio", "video"]) |