|
15 | 15 |
|
16 | 16 | import { describe, expect, it, mock, spyOn } from "bun:test"; |
17 | 17 | import * as fs from "node:fs"; |
18 | | -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 18 | +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
19 | 19 | import { tmpdir } from "node:os"; |
20 | 20 | import { join } from "node:path"; |
21 | 21 | import { |
@@ -606,3 +606,132 @@ describe("constrainAacTruePeak from a PCM source", () => { |
606 | 606 | } |
607 | 607 | }); |
608 | 608 | }); |
| 609 | + |
| 610 | +describe("padOrTrimAudioToVideoFrameCount from a PCM source", () => { |
| 611 | + // A second AAC generation reads hotter than the first: the fake peaks model that measured case. |
| 612 | + function pcmHarness(dir: string, peaks: { fromPcm: number[]; fromAac: number }) { |
| 613 | + const inputs: string[] = []; |
| 614 | + const sourcePath = join(dir, "audio-source.wav"); |
| 615 | + const input: PadTrimAudioInput = { |
| 616 | + videoPath: join(dir, "video.mp4"), |
| 617 | + audioPath: join(dir, "audio.m4a"), |
| 618 | + outputPath: join(dir, "audio.duration-normalized.m4a"), |
| 619 | + sourcePath, |
| 620 | + probeVideoFrameInfo: async () => ({ frameCount: 120, fpsNum: 30, fpsDen: 1 }), |
| 621 | + probeAudioInfo: async () => ({ durationSeconds: 4.021 }), |
| 622 | + probeAudioTruePeakDbfs: async (path) => |
| 623 | + path === sourcePath |
| 624 | + ? -3 |
| 625 | + : inputs.at(-1) === sourcePath |
| 626 | + ? peaks.fromPcm[inputs.length - 1]! |
| 627 | + : peaks.fromAac, |
| 628 | + encodeAac: async (from, output) => { |
| 629 | + inputs.push(from); |
| 630 | + writeFileSync(output, "aac"); |
| 631 | + return { success: true }; |
| 632 | + }, |
| 633 | + }; |
| 634 | + return { input, inputs, sourcePath }; |
| 635 | + } |
| 636 | + |
| 637 | + it("trims a compliant mix from its PCM, so a second AAC generation cannot push it over and cut it", async () => { |
| 638 | + const dir = mkdtempSync(join(tmpdir(), "hf-trim-pcm-")); |
| 639 | + try { |
| 640 | + const { input, inputs, sourcePath } = pcmHarness(dir, { fromPcm: [-1.8], fromAac: 0.2 }); |
| 641 | + |
| 642 | + const result = await padOrTrimAudioToVideoFrameCount(input); |
| 643 | + |
| 644 | + expect(result.success, result.error).toBe(true); |
| 645 | + expect(result.operation).toBe("trim"); |
| 646 | + expect(inputs).toEqual([sourcePath]); |
| 647 | + expect(result).not.toHaveProperty("audioLoweredDb"); |
| 648 | + } finally { |
| 649 | + rmSync(dir, { recursive: true, force: true }); |
| 650 | + } |
| 651 | + }); |
| 652 | + |
| 653 | + it("re-encodes a peak correction from the PCM, not from the normalized AAC", async () => { |
| 654 | + const dir = mkdtempSync(join(tmpdir(), "hf-trim-pcm-cut-")); |
| 655 | + try { |
| 656 | + const { input, inputs, sourcePath } = pcmHarness(dir, { fromPcm: [0.5, -1.6], fromAac: 3 }); |
| 657 | + |
| 658 | + const result = await padOrTrimAudioToVideoFrameCount(input); |
| 659 | + |
| 660 | + expect(result.success, result.error).toBe(true); |
| 661 | + expect(inputs).toEqual([sourcePath, sourcePath]); |
| 662 | + expect(result.audioLoweredDb).toBe(2); |
| 663 | + } finally { |
| 664 | + rmSync(dir, { recursive: true, force: true }); |
| 665 | + } |
| 666 | + }); |
| 667 | +}); |
| 668 | + |
| 669 | +describe("constrainAacTruePeak keeps its PCM source matching the delivered AAC", () => { |
| 670 | + function peakHarness(dir: string, rewrite: { success: boolean }) { |
| 671 | + const audioPath = join(dir, "audio.m4a"); |
| 672 | + const sourcePath = join(dir, "audio-source.wav"); |
| 673 | + writeFileSync(audioPath, "aac"); |
| 674 | + writeFileSync(sourcePath, "pcm"); |
| 675 | + const rewrites: string[] = []; |
| 676 | + let corrected = false; |
| 677 | + const run = (firstPeak: number) => |
| 678 | + constrainAacTruePeak({ |
| 679 | + audioPath, |
| 680 | + sourcePath, |
| 681 | + targetDurationSeconds: 4, |
| 682 | + probeTruePeakDbfs: async (path) => |
| 683 | + path === sourcePath ? -3 : path === audioPath && !corrected ? firstPeak : -1.6, |
| 684 | + encodeAac: async (_from, output) => { |
| 685 | + corrected = true; |
| 686 | + writeFileSync(output, "corrected aac"); |
| 687 | + return { success: true }; |
| 688 | + }, |
| 689 | + rewriteSource: async (_from, output, filter) => { |
| 690 | + rewrites.push(filter); |
| 691 | + if (rewrite.success) writeFileSync(output, "cut pcm"); |
| 692 | + return rewrite; |
| 693 | + }, |
| 694 | + }); |
| 695 | + return { run, rewrites, sourcePath }; |
| 696 | + } |
| 697 | + |
| 698 | + it("applies the cut to the PCM too, so encoding it again reproduces the delivered AAC", async () => { |
| 699 | + const dir = mkdtempSync(join(tmpdir(), "hf-cut-source-")); |
| 700 | + try { |
| 701 | + const { run, rewrites, sourcePath } = peakHarness(dir, { success: true }); |
| 702 | + |
| 703 | + const result = await run(0.5); |
| 704 | + |
| 705 | + expect(result).toEqual({ success: true, loweredDb: 2 }); |
| 706 | + expect(rewrites).toEqual(["volume=-2.000dB"]); |
| 707 | + expect(readFileSync(sourcePath, "utf-8")).toBe("cut pcm"); |
| 708 | + } finally { |
| 709 | + rmSync(dir, { recursive: true, force: true }); |
| 710 | + } |
| 711 | + }); |
| 712 | + |
| 713 | + it("leaves the PCM alone when no cut is needed", async () => { |
| 714 | + const dir = mkdtempSync(join(tmpdir(), "hf-cut-source-none-")); |
| 715 | + try { |
| 716 | + const { run, rewrites, sourcePath } = peakHarness(dir, { success: true }); |
| 717 | + |
| 718 | + expect(await run(-1.8)).toEqual({ success: true }); |
| 719 | + expect(rewrites).toEqual([]); |
| 720 | + expect(readFileSync(sourcePath, "utf-8")).toBe("pcm"); |
| 721 | + } finally { |
| 722 | + rmSync(dir, { recursive: true, force: true }); |
| 723 | + } |
| 724 | + }); |
| 725 | + |
| 726 | + it("removes a PCM it could not cut rather than leave one louder than the delivered AAC", async () => { |
| 727 | + const dir = mkdtempSync(join(tmpdir(), "hf-cut-source-fail-")); |
| 728 | + try { |
| 729 | + const { run, sourcePath } = peakHarness(dir, { success: false }); |
| 730 | + |
| 731 | + expect(await run(0.5)).toEqual({ success: true, loweredDb: 2, sourceStale: true }); |
| 732 | + expect(existsSync(sourcePath)).toBe(false); |
| 733 | + } finally { |
| 734 | + rmSync(dir, { recursive: true, force: true }); |
| 735 | + } |
| 736 | + }); |
| 737 | +}); |
0 commit comments