package ffmpeg import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" "time" "github.com/noderunners/dlbot/internal/media" "github.com/noderunners/dlbot/internal/ytdlp" ) func TestArgs(t *testing.T) { tests := map[media.Profile]struct{ want, out string }{ media.P480: {"-vf scale=-2:480 -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -movflags +faststart", "media.mp4"}, media.P720: {"-vf scale=-2:720", "media.mp4"}, media.MP3: {"-vn -c:a libmp3lame -b:a 192k", "media.mp3"}, media.M4A: {"-vn -c:a aac -b:a 160k -movflags +faststart", "media.m4a"}, } for p, tc := range tests { args, out, err := Args("/src/in", "/dst", p) if err != nil { t.Errorf("%s: %v", p, err) continue } joined := strings.Join(args, " ") if !strings.Contains(joined, tc.want) || out != filepath.Join("/dst", tc.out) || args[len(args)-1] != out { t.Errorf("%s: args = %q, out = %q", p, joined, out) } if !strings.Contains(joined, "-i /src/in") || !strings.Contains(joined, "-nostdin") { t.Errorf("%s: input or -nostdin missing: %q", p, joined) } } // Profiles that are not made by transcoding. for _, p := range []media.Profile{media.Best, media.Thumb, media.Subs("en")} { if _, _, err := Args("in", "out", p); err == nil { t.Errorf("%s must not be derivable", p) } } } // TestDeriveWithRealFfmpeg makes a two second 720p test clip and derives // 480p and MP3 from it. It is skipped when ffmpeg is not installed. func TestDeriveWithRealFfmpeg(t *testing.T) { if _, err := exec.LookPath("ffmpeg"); err != nil { t.Skip("ffmpeg is not installed") } dir := t.TempDir() src := filepath.Join(dir, "source.mp4") gen := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "testsrc=duration=2:size=1280x720:rate=15", "-f", "lavfi", "-i", "sine=frequency=440:duration=2", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest", src) if out, err := gen.CombinedOutput(); err != nil { t.Skipf("cannot generate a test clip with this ffmpeg build: %v %s", err, out) } e := &Exec{Timeout: time.Minute} ctx := context.Background() video, err := e.Derive(ctx, src, filepath.Join(dir, "v"), media.P480) if err != nil { t.Fatalf("derive 480: %v", err) } if video.Ext != "mp4" || video.Height != 480 || video.Width != 854 || video.Size == 0 || video.Duration < 1.5 || video.Duration > 2.6 { t.Fatalf("480p result = %+v", video) } audio, err := e.Derive(ctx, src, filepath.Join(dir, "a"), media.MP3) if err != nil { t.Fatalf("derive mp3: %v", err) } if audio.Ext != "mp3" || audio.Size == 0 || audio.Width != 0 || audio.Duration < 1.5 { t.Fatalf("mp3 result = %+v", audio) } // A source that is not media gives a classified error, not a crash. bad := filepath.Join(dir, "bad.mp4") if err := os.WriteFile(bad, []byte("this is not a video"), 0o600); err != nil { t.Fatal(err) } if _, err := e.Derive(ctx, bad, filepath.Join(dir, "b"), media.P480); ytdlp.CodeOf(err) != ytdlp.CodeProcessing { t.Fatalf("bad source: %v, want processing_failed", err) } // Cancelling stops ffmpeg. cctx, cancel := context.WithCancel(ctx) cancel() if _, err := e.Derive(cctx, src, filepath.Join(dir, "c"), media.P480); ytdlp.CodeOf(err) != ytdlp.CodeCancelled { t.Fatalf("cancelled derive: %v, want cancelled", err) } }