diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 02a09f0..dbfcaae 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,9 +37,6 @@ jobs: - uses: actions/setup-node@v6 with: node-version: "26" - - uses: AnimMouse/setup-ffmpeg@v1 - with: - version: "9.0" - uses: moonrepo/setup-toolchain@v0 with: moon-version: "2.5.5" diff --git a/README.md b/README.md index fd211c8..e507cbb 100644 --- a/README.md +++ b/README.md @@ -3,18 +3,18 @@ Official command-line client for publishing and managing podcasts on [Listenbox](https://listenbox.app). -The CLI uses Listenbox's public API. YouTube imports use [kkdai/youtube](https://github.com/kkdai/youtube) to download on your computer and bundled FFmpeg 9+ to prepare media before uploading it directly to Listenbox's media storage. +The CLI uses Listenbox's public API. YouTube imports use [kkdai/youtube](https://github.com/kkdai/youtube) to download on your computer and statically linked FFmpeg 9.0.2 through [go-astiav](https://github.com/asticode/go-astiav) to prepare media before uploading it directly to Listenbox's media storage. ## Install -Build a native distribution with Go 1.27+, Node.js 26, Moon, and FFmpeg/FFprobe 9+ installed: +Build a native distribution with Go 1.27+, Node.js 26, Moon, a C compiler, Make, pkg-config, and tar installed: ```sh -moon run package +moon run cli:package tar -xzf dist/listenbox.tar.gz -C /your/bin/directory ``` -Keep `listenbox`, `ffmpeg`, `ffprobe`, and the license notices together. The bundle uses the build machine's native media binaries; Linux builds require compatible system C libraries. Media processing resolves these bundled executables directly. +The package contains one executable and its license notices. FFmpeg is built from a checksum-verified source archive, then linked into the CLI through CGO. No FFmpeg/FFprobe installation or media subprocess is needed at runtime. Linux builds still use the system C library. `prepare-ffmpeg.ts` records the exact codecs and build options; the additional avdevice, avfilter, and swscale libraries satisfy go-astiav's binding requirements. Make sure the installation directory is on `PATH`, then authorize the CLI: @@ -38,14 +38,13 @@ Generated API client and configuration files are committed, so a fresh clone builds without another repository or a code-generation step. ```sh -go build ./cmd/listenbox -go test -tags=dev ./... +moon run cli:build cli:test ``` [Moon](https://moonrepo.dev) runs the complete project check: ```sh -moon run check +moon run cli:check ``` CI follows [Moon's CI guide](https://moonrepo.dev/docs/guides/ci): full Git diff --git a/bundle-media.ts b/bundle-media.ts deleted file mode 100644 index 93e8e3e..0000000 --- a/bundle-media.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { execFile } from "node:child_process" -import { chmod, copyFile, mkdir, writeFile } from "node:fs/promises" -import { delimiter, join } from "node:path" -import { promisify } from "node:util" - -const run = promisify(execFile) -const destination = new URL("./dist/", import.meta.url) -await mkdir(destination, { recursive: true }) - -async function bundle(name: string) { - const executable = process.platform === "win32" ? `${name}.exe` : name - const { stdout } = await run(executable, ["-version"]) - const version = new RegExp(`^${name} version (?:n)?(\\d+)\\.`).exec(stdout) - if (version === null || Number(version[1]) < 9) { - throw new Error(`The CLI bundle requires ${name} 9 or newer: ${stdout.split("\n")[0]}`) - } - for (const directory of (process.env.PATH ?? "").split(delimiter)) { - try { - await copyFile(join(directory, executable), new URL(executable, destination)) - await chmod(new URL(executable, destination), 0o755) - const license = await run(executable, ["-L"]) - await writeFile(new URL(`${name}-NOTICE.txt`, destination), `${stdout}\n${license.stdout}\n${license.stderr}\nUpstream source: https://github.com/FFmpeg/FFmpeg\nThe version and build configuration above identify this bundled build.\n`) - return - } catch (error) { - if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") throw error - } - } - throw new Error(`Cannot locate ${executable} for the CLI bundle`) -} - -await bundle("ffmpeg") -await bundle("ffprobe") diff --git a/ffmpeg-env.sh b/ffmpeg-env.sh new file mode 100644 index 0000000..ce78a7c --- /dev/null +++ b/ffmpeg-env.sh @@ -0,0 +1,4 @@ +# Source from the project root before compiling packages that use go-astiav. +export CGO_ENABLED=1 +export PKG_CONFIG_PATH="$PWD/.cache/ffmpeg/lib/pkgconfig" +export CGO_LDFLAGS="$(pkg-config --static --libs libavcodec libavdevice libavfilter libavformat libswresample libswscale libavutil)" diff --git a/go.mod b/go.mod index 1334153..6c2ddef 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.27.0 require ( github.com/Oudwins/zog v0.23.0 + github.com/asticode/go-astiav v0.43.0 github.com/kkdai/youtube/v2 v2.10.6 github.com/knadh/koanf/parsers/yaml v1.1.1 github.com/knadh/koanf/providers/file v1.2.1 @@ -15,6 +16,7 @@ require ( ) require ( + github.com/asticode/go-astikit v0.42.0 // indirect github.com/bitly/go-simplejson v0.5.1 // indirect github.com/dlclark/regexp2 v1.11.5 // indirect github.com/dop251/goja v0.0.0-20260311135729-065cd970411c // indirect diff --git a/go.sum b/go.sum index 5209957..bbdf6de 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,10 @@ github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0 github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= github.com/Oudwins/zog v0.23.0 h1:aNjOcy7mE5NWOQVR9sFO1L0YCKfEjWMlaUTNquKGRQY= github.com/Oudwins/zog v0.23.0/go.mod h1:yznncrIHC5tQeOui28FMz67LEGZV3nAoCdWL5t8QsNM= +github.com/asticode/go-astiav v0.43.0 h1:Pn+lQSdDs9ifSh5KcmesyUAjCC0/qJwAWFa1TYnGxBw= +github.com/asticode/go-astiav v0.43.0/go.mod h1:GI0pHw6K2/pl/o8upCtT49P/q4KCwhv/8nGLlCsZLdA= +github.com/asticode/go-astikit v0.42.0 h1:pnir/2KLUSr0527Tv908iAH6EGYYrYta132vvjXsH5w= +github.com/asticode/go-astikit v0.42.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0= github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow= github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= diff --git a/internal/cli/media.go b/internal/cli/media.go new file mode 100644 index 0000000..dd24124 --- /dev/null +++ b/internal/cli/media.go @@ -0,0 +1,279 @@ +package cli + +import ( + "context" + "errors" + "fmt" + "path/filepath" + "time" + + "github.com/asticode/go-astiav" +) + +const ( + mediaFastStart = "+faststart" + mediaMovFlags = "movflags" +) + +// A mediaInput owns its demuxer and a single lookahead packet. Only downloaded +// local files enter FFmpeg; HTTP and cancellation remain owned by the importer. +type mediaInput struct { + format *astiav.FormatContext + stream *astiav.Stream + packet *astiav.Packet + ready bool +} + +func openMediaInput(path string, kind astiav.MediaType) (*mediaInput, error) { + input := &mediaInput{format: astiav.AllocFormatContext(), packet: astiav.AllocPacket()} + if input.format == nil || input.packet == nil { + input.close() + return nil, errors.New("allocate native media input") + } + if err := input.format.OpenInput(path, nil, nil); err != nil { + input.close() + return nil, fmt.Errorf("open media %q: %w", path, err) + } + if err := input.format.FindStreamInfo(nil); err != nil { + input.close() + return nil, fmt.Errorf("inspect media %q: %w", path, err) + } + for _, stream := range input.format.Streams() { + if stream.CodecParameters().MediaType() == kind { + input.stream = stream + return input, nil + } + } + input.close() + return nil, fmt.Errorf("media %q has no %s stream", path, kind) +} + +func (input *mediaInput) close() { + if input.packet != nil { + input.packet.Free() + } + if input.format != nil { + input.format.CloseInput() + input.format.Free() + } +} + +func (input *mediaInput) next(ctx context.Context) error { + input.ready = false + input.packet.Unref() + for { + if err := ctx.Err(); err != nil { + return err + } + if err := input.format.ReadFrame(input.packet); err != nil { + if errors.Is(err, astiav.ErrEof) { + return nil + } + return fmt.Errorf("read media packet: %w", err) + } + if input.packet.StreamIndex() == input.stream.Index() { + input.ready = true + return nil + } + input.packet.Unref() + } +} + +func (input *mediaInput) timestamp() int64 { + timestamp := input.packet.Dts() + if timestamp == astiav.NoPtsValue { + timestamp = input.packet.Pts() + } + if timestamp == astiav.NoPtsValue { + timestamp = 0 + } + return astiav.RescaleQ(timestamp, input.stream.TimeBase(), astiav.NewRational(1, int(time.Second/time.Microsecond))) +} + +type mediaOutput struct { + format *astiav.FormatContext + io *astiav.IOContext +} + +func openMediaOutput(path string) (*mediaOutput, error) { + format, err := astiav.AllocOutputFormatContext(nil, "", path) + if err != nil { + return nil, fmt.Errorf("allocate output %q: %w", path, err) + } + output := &mediaOutput{format: format} + if !format.OutputFormat().Flags().Has(astiav.IOFormatFlagNofile) { + output.io, err = astiav.OpenIOContext(path, astiav.NewIOContextFlags(astiav.IOContextFlagWrite), nil, nil) + if err != nil { + format.Free() + return nil, fmt.Errorf("open output %q: %w", path, err) + } + format.SetPb(output.io) + } + return output, nil +} + +func (output *mediaOutput) close() error { + var err error + if output.io != nil { + err = output.io.Close() + } + output.format.Free() + return err +} + +func (output *mediaOutput) copyStream(input *mediaInput) (*astiav.Stream, error) { + stream := output.format.NewStream(nil) + if stream == nil { + return nil, errors.New("allocate output stream") + } + if err := input.stream.CodecParameters().Copy(stream.CodecParameters()); err != nil { + return nil, err + } + stream.CodecParameters().SetCodecTag(0) + stream.SetTimeBase(input.stream.TimeBase()) + return stream, nil +} + +func (output *mediaOutput) header(options map[string]string) error { + dictionary := astiav.NewDictionary() + defer dictionary.Free() + for key, value := range options { + if err := dictionary.Set(key, value, 0); err != nil { + return err + } + } + if err := output.format.WriteHeader(dictionary); err != nil { + return fmt.Errorf("write media header: %w", err) + } + return nil +} + +func (output *mediaOutput) write(input *mediaInput, stream *astiav.Stream) error { + input.packet.RescaleTs(input.stream.TimeBase(), stream.TimeBase()) + input.packet.SetStreamIndex(stream.Index()) + input.packet.SetPos(-1) + if err := output.format.WriteInterleavedFrame(input.packet); err != nil { + return fmt.Errorf("write media packet: %w", err) + } + return nil +} + +func muxMedia(ctx context.Context, videoPath, audioPath, destination string) (result error) { + video, err := openMediaInput(videoPath, astiav.MediaTypeVideo) + if err != nil { + return err + } + defer video.close() + audio, err := openMediaInput(audioPath, astiav.MediaTypeAudio) + if err != nil { + return err + } + defer audio.close() + output, err := openMediaOutput(destination) + if err != nil { + return err + } + defer func() { result = errors.Join(result, output.close()) }() + videoStream, err := output.copyStream(video) + if err != nil { + return err + } + audioStream, err := output.copyStream(audio) + if err != nil { + return err + } + if err := output.header(map[string]string{mediaMovFlags: mediaFastStart}); err != nil { + return err + } + if err := video.next(ctx); err != nil { + return err + } + if err := audio.next(ctx); err != nil { + return err + } + if !video.ready || !audio.ready { + return errors.New("downloaded media stream is empty") + } + return interleaveMedia(ctx, output, video, audio, videoStream, audioStream) +} + +func interleaveMedia( + ctx context.Context, output *mediaOutput, video, audio *mediaInput, + videoStream, audioStream *astiav.Stream, +) error { + // Reading both inputs in timestamp order keeps muxer buffering bounded even + // for long recordings. Never enqueue the entire video before the audio. + for video.ready || audio.ready { + input, stream := audio, audioStream + if video.ready && (!audio.ready || video.timestamp() <= audio.timestamp()) { + input, stream = video, videoStream + } + if err := output.write(input, stream); err != nil { + return err + } + if err := input.next(ctx); err != nil { + return err + } + } + return output.format.WriteTrailer() +} + +func remuxMedia( + ctx context.Context, source, destination string, kind astiav.MediaType, options map[string]string, +) (result error) { + input, err := openMediaInput(source, kind) + if err != nil { + return err + } + defer input.close() + output, err := openMediaOutput(destination) + if err != nil { + return err + } + defer func() { result = errors.Join(result, output.close()) }() + stream, err := output.copyStream(input) + if err != nil { + return err + } + if err := output.header(options); err != nil { + return err + } + count := 0 + for { + if err := input.next(ctx); err != nil { + return err + } + if !input.ready { + break + } + if err := output.write(input, stream); err != nil { + return err + } + count++ + } + if count == 0 { + return errors.New("downloaded media stream is empty") + } + return output.format.WriteTrailer() +} + +func mediaDimensions(source string) (int, int, error) { + input, err := openMediaInput(source, astiav.MediaTypeVideo) + if err != nil { + return 0, 0, err + } + defer input.close() + parameters := input.stream.CodecParameters() + width, height := parameters.Width(), parameters.Height() + if width <= 0 || height <= 0 { + return 0, 0, fmt.Errorf("invalid video dimensions %dx%d", width, height) + } + return width, height, nil +} + +func packageMediaTrack(ctx context.Context, source, root string, kind astiav.MediaType) error { + return remuxMedia(ctx, source, filepath.Join(root, "index.m3u8"), kind, map[string]string{ + "hls_time": "6", "hls_playlist_type": "vod", "hls_segment_type": "fmp4", + "hls_fmp4_init_filename": "init.mp4", "hls_segment_filename": filepath.Join(root, "segment-%05d.m4s"), + }) +} diff --git a/internal/cli/media_audio.go b/internal/cli/media_audio.go new file mode 100644 index 0000000..6028f36 --- /dev/null +++ b/internal/cli/media_audio.go @@ -0,0 +1,291 @@ +package cli + +import ( + "context" + "errors" + "fmt" + + "github.com/asticode/go-astiav" +) + +const ( + mediaAudioRate = 48000 + mediaStereoBitrate = 128000 +) + +func prepareMediaAudio(ctx context.Context, source, destination string) error { + if err := ctx.Err(); err != nil { + return err + } + input, err := openMediaInput(source, astiav.MediaTypeAudio) + if err != nil { + return err + } + defer input.close() + if input.stream.CodecParameters().CodecID() == astiav.CodecIDAac { + return remuxMedia(ctx, source, destination, astiav.MediaTypeAudio, map[string]string{mediaMovFlags: mediaFastStart}) + } + return transcodeMediaAudio(ctx, input, destination) +} + +type mediaAudioTranscoder struct { + decoder, encoder *astiav.CodecContext + resampler *astiav.SoftwareResampleContext + fifo *astiav.AudioFifo + output *mediaOutput + stream *astiav.Stream + timestamp int64 +} + +func (transcoder *mediaAudioTranscoder) close() { + if transcoder.decoder != nil { + transcoder.decoder.Free() + } + if transcoder.encoder != nil { + transcoder.encoder.Free() + } + if transcoder.resampler != nil { + transcoder.resampler.Free() + } + if transcoder.fifo != nil { + transcoder.fifo.Free() + } +} + +func (transcoder *mediaAudioTranscoder) openCodecs(input *mediaInput) error { + decoder := astiav.FindDecoder(input.stream.CodecParameters().CodecID()) + encoder := astiav.FindEncoder(astiav.CodecIDAac) + if decoder == nil || encoder == nil { + return errors.New("bundled audio codec is unavailable") + } + transcoder.decoder = astiav.AllocCodecContext(decoder) + transcoder.encoder = astiav.AllocCodecContext(encoder) + if transcoder.decoder == nil || transcoder.encoder == nil { + return errors.New("allocate audio codec") + } + if err := input.stream.CodecParameters().ToCodecContext(transcoder.decoder); err != nil { + return err + } + if err := transcoder.decoder.Open(decoder, nil); err != nil { + return err + } + if transcoder.decoder.SampleRate() <= 0 || transcoder.decoder.ChannelLayout().Channels() <= 0 { + return errors.New("invalid input audio rate or channel layout") + } + layout := astiav.ChannelLayoutStereo + bitrate := int64(mediaStereoBitrate) + if transcoder.decoder.ChannelLayout().Channels() == 1 { + layout = astiav.ChannelLayoutMono + bitrate = 64000 + } + transcoder.encoder.SetChannelLayout(layout) + transcoder.encoder.SetSampleRate(mediaAudioRate) + transcoder.encoder.SetSampleFormat(astiav.SampleFormatFltp) + transcoder.encoder.SetBitRate(bitrate) + transcoder.encoder.SetTimeBase(astiav.NewRational(1, mediaAudioRate)) + transcoder.encoder.SetFlags(astiav.NewCodecContextFlags(astiav.CodecContextFlagGlobalHeader)) + return transcoder.encoder.Open(encoder, nil) +} + +func (transcoder *mediaAudioTranscoder) openOutput() error { + transcoder.stream = transcoder.output.format.NewStream(nil) + if transcoder.stream == nil { + return errors.New("allocate AAC output stream") + } + transcoder.stream.SetTimeBase(transcoder.encoder.TimeBase()) + if err := transcoder.encoder.ToCodecParameters(transcoder.stream.CodecParameters()); err != nil { + return err + } + if err := transcoder.output.header(map[string]string{mediaMovFlags: mediaFastStart}); err != nil { + return err + } + transcoder.resampler = astiav.AllocSoftwareResampleContext() + transcoder.fifo = astiav.AllocAudioFifo( + transcoder.encoder.SampleFormat(), transcoder.encoder.ChannelLayout().Channels(), 1, + ) + if transcoder.resampler == nil || transcoder.fifo == nil { + return errors.New("allocate audio resampler") + } + return nil +} + +func transcodeMediaAudio(ctx context.Context, input *mediaInput, destination string) (result error) { + transcoder := &mediaAudioTranscoder{} + defer transcoder.close() + if err := transcoder.openCodecs(input); err != nil { + return fmt.Errorf("open audio codecs: %w", err) + } + output, err := openMediaOutput(destination) + if err != nil { + return err + } + transcoder.output = output + defer func() { result = errors.Join(result, output.close()) }() + if err := transcoder.openOutput(); err != nil { + return err + } + return transcoder.run(ctx, input) +} + +func (transcoder *mediaAudioTranscoder) run(ctx context.Context, input *mediaInput) error { + for { + if err := input.next(ctx); err != nil { + return err + } + if !input.ready { + break + } + if err := transcoder.decode(ctx, input.packet); err != nil { + return err + } + } + if err := transcoder.decode(ctx, nil); err != nil { + return err + } + return transcoder.finish(ctx) +} + +func (transcoder *mediaAudioTranscoder) finish(ctx context.Context) error { + for { + if err := ctx.Err(); err != nil { + return err + } + samples, err := transcoder.resample(nil) + if err != nil { + return err + } + if samples == 0 { + break + } + if err := transcoder.encodeReady(ctx, false); err != nil { + return err + } + } + if err := transcoder.encodeReady(ctx, true); err != nil { + return err + } + if transcoder.timestamp == 0 { + return errors.New("downloaded audio stream is empty") + } + if err := transcoder.encoder.SendFrame(nil); err != nil { + return err + } + if err := transcoder.drainEncoder(); err != nil { + return err + } + return transcoder.output.format.WriteTrailer() +} + +func mediaCodecWaiting(err error) bool { + return errors.Is(err, astiav.ErrEagain) || errors.Is(err, astiav.ErrEof) +} + +func (transcoder *mediaAudioTranscoder) decode(ctx context.Context, packet *astiav.Packet) error { + if err := transcoder.decoder.SendPacket(packet); err != nil { + return err + } + frame := astiav.AllocFrame() + if frame == nil { + return errors.New("allocate decoded audio frame") + } + defer frame.Free() + for { + if err := ctx.Err(); err != nil { + return err + } + if err := transcoder.decoder.ReceiveFrame(frame); err != nil { + if mediaCodecWaiting(err) { + return nil + } + return fmt.Errorf("decode audio frame: %w", err) + } + if _, err := transcoder.resample(frame); err != nil { + return err + } + frame.Unref() + if err := transcoder.encodeReady(ctx, false); err != nil { + return err + } + } +} + +func (transcoder *mediaAudioTranscoder) resample(input *astiav.Frame) (int, error) { + frame := astiav.AllocFrame() + if frame == nil { + return 0, errors.New("allocate resampled audio frame") + } + defer frame.Free() + frame.SetChannelLayout(transcoder.encoder.ChannelLayout()) + frame.SetSampleFormat(transcoder.encoder.SampleFormat()) + frame.SetSampleRate(mediaAudioRate) + // Let swresample allocate enough space for the input and retained delay. + if err := transcoder.resampler.ConvertFrame(input, frame); err != nil { + return 0, fmt.Errorf("resample audio: %w", err) + } + if frame.NbSamples() > 0 { + if _, err := transcoder.fifo.Write(frame); err != nil { + return 0, err + } + } + return frame.NbSamples(), nil +} + +func (transcoder *mediaAudioTranscoder) encodeReady(ctx context.Context, finish bool) error { + size := transcoder.encoder.FrameSize() + for transcoder.fifo.Size() >= size || (finish && transcoder.fifo.Size() > 0) { + if err := ctx.Err(); err != nil { + return err + } + if err := transcoder.encodeFrame(min(size, transcoder.fifo.Size())); err != nil { + return err + } + } + return nil +} + +func (transcoder *mediaAudioTranscoder) encodeFrame(samples int) error { + frame := astiav.AllocFrame() + if frame == nil { + return errors.New("allocate encoder audio frame") + } + defer frame.Free() + frame.SetChannelLayout(transcoder.encoder.ChannelLayout()) + frame.SetSampleFormat(transcoder.encoder.SampleFormat()) + frame.SetSampleRate(mediaAudioRate) + frame.SetNbSamples(samples) + if err := frame.AllocBuffer(0); err != nil { + return err + } + if _, err := transcoder.fifo.Read(frame); err != nil { + return err + } + frame.SetPts(transcoder.timestamp) + transcoder.timestamp += int64(samples) + if err := transcoder.encoder.SendFrame(frame); err != nil { + return err + } + return transcoder.drainEncoder() +} + +func (transcoder *mediaAudioTranscoder) drainEncoder() error { + packet := astiav.AllocPacket() + if packet == nil { + return errors.New("allocate encoded audio packet") + } + defer packet.Free() + for { + if err := transcoder.encoder.ReceivePacket(packet); err != nil { + if mediaCodecWaiting(err) { + return nil + } + return fmt.Errorf("encode AAC packet: %w", err) + } + packet.RescaleTs(transcoder.encoder.TimeBase(), transcoder.stream.TimeBase()) + packet.SetStreamIndex(transcoder.stream.Index()) + packet.SetPos(-1) + if err := transcoder.output.format.WriteInterleavedFrame(packet); err != nil { + return err + } + packet.Unref() + } +} diff --git a/internal/cli/media_test.go b/internal/cli/media_test.go new file mode 100644 index 0000000..767fd66 --- /dev/null +++ b/internal/cli/media_test.go @@ -0,0 +1,78 @@ +package cli + +import ( + "bytes" + "context" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/asticode/go-astiav" + "gotest.tools/v3/assert" +) + +func TestNativeMediaProducesAVCAACAndHLS(t *testing.T) { + t.Parallel() + for _, audio := range []string{"aac.m4a", "opus.webm"} { + t.Run(audio, func(t *testing.T) { + t.Parallel() + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + defer cancel() + directory := t.TempDir() + prepared := filepath.Join(directory, "audio.m4a") + assert.NilError(t, prepareMediaAudio(ctx, filepath.Join("testdata", "media", audio), prepared)) + destination := filepath.Join(directory, "video.mp4") + assert.NilError(t, muxMedia(ctx, filepath.Join("testdata", "media", "video.mp4"), prepared, destination)) + duration, err := packageYouTubeVideo(ctx, directory) + assert.NilError(t, err) + assert.Assert(t, duration >= 1 && duration <= 4, "unexpected package duration %d", duration) + assertNativePackage(t, directory) + }) + } +} + +func assertNativePackage(t *testing.T, directory string) { + t.Helper() + destination := filepath.Join(directory, "video.mp4") + audio, err := openMediaInput(destination, astiav.MediaTypeAudio) + assert.NilError(t, err) + defer audio.close() + assert.Equal(t, audio.stream.CodecParameters().CodecID(), astiav.CodecIDAac) + video, err := openMediaInput(destination, astiav.MediaTypeVideo) + assert.NilError(t, err) + defer video.close() + assert.Equal(t, video.stream.CodecParameters().CodecID(), astiav.CodecIDH264) + assert.Equal(t, video.stream.CodecParameters().Width(), 1280) + assert.Equal(t, video.stream.CodecParameters().Height(), 720) + data, err := os.ReadFile(destination) + assert.NilError(t, err) + assert.Assert(t, len(data) > 100_000 && len(data) < 2_000_000) + moov, mdat := bytes.Index(data, []byte("moov")), bytes.Index(data, []byte("mdat")) + assert.Assert(t, moov > 0 && mdat > moov, "MP4 must be fast-start") + for _, kind := range []string{youtubeVideoKind, youtubeAudioKind} { + root := filepath.Join(directory, "hls", kind) + playlist, err := os.ReadFile(filepath.Join(root, "index.m3u8")) + assert.NilError(t, err) + assert.Assert(t, strings.Contains(string(playlist), "#EXT-X-ENDLIST")) + for _, name := range []string{"init.mp4", "segment-00000.m4s"} { + info, err := os.Stat(filepath.Join(root, name)) + assert.NilError(t, err) + assert.Assert(t, info.Size() > 100 && info.Size() < 2_000_000) + } + } +} + +func TestNativeMediaCancellationAndInvalidInput(t *testing.T) { + t.Parallel() + ctx, cancel := context.WithCancel(t.Context()) + cancel() + output := filepath.Join(t.TempDir(), "audio.m4a") + assert.ErrorIs(t, prepareMediaAudio(ctx, "testdata/media/opus.webm", output), context.Canceled) + _, err := os.Stat(output) + assert.Assert(t, os.IsNotExist(err), "cancelled preparation must not create output") + invalid := filepath.Join(t.TempDir(), "invalid.mp4") + assert.NilError(t, os.WriteFile(invalid, []byte("not media"), 0o600)) + assert.ErrorContains(t, prepareMediaAudio(t.Context(), invalid, output), "open media") +} diff --git a/internal/cli/testdata/media/README.md b/internal/cli/testdata/media/README.md new file mode 100644 index 0000000..a0b3cf8 --- /dev/null +++ b/internal/cli/testdata/media/README.md @@ -0,0 +1,10 @@ +Synthetic, two-second media fixtures copied from Listenbox's deterministic +YouTube E2E catalogue (listenbox/listenbox2, apps/fixtures/e2e/youtube). +No network or external executable is needed by the native media tests. + +- video.mp4: dash-range/720/manifest-stream0.mp4, AVC 1280x720. +- aac.m4a: dash-range/720/manifest-stream1.mp4, AAC audio. +- opus.webm: audio/opus.webm, Opus audio. + +Tests exercise AAC remux, Opus decode/resample/AAC encode, bounded two-input +muxing, fast-start MP4, HLS output, cancellation, and malformed input. diff --git a/internal/cli/testdata/media/aac.m4a b/internal/cli/testdata/media/aac.m4a new file mode 100644 index 0000000..6c8b609 Binary files /dev/null and b/internal/cli/testdata/media/aac.m4a differ diff --git a/internal/cli/testdata/media/opus.webm b/internal/cli/testdata/media/opus.webm new file mode 100644 index 0000000..2e8d43b Binary files /dev/null and b/internal/cli/testdata/media/opus.webm differ diff --git a/internal/cli/testdata/media/video.mp4 b/internal/cli/testdata/media/video.mp4 new file mode 100644 index 0000000..893f2cf Binary files /dev/null and b/internal/cli/testdata/media/video.mp4 differ diff --git a/internal/cli/youtube.go b/internal/cli/youtube.go index 571eced..09e267b 100644 --- a/internal/cli/youtube.go +++ b/internal/cli/youtube.go @@ -2,11 +2,9 @@ package cli import ( - "bytes" "context" "crypto/sha256" "encoding/hex" - "encoding/json" "errors" "fmt" "io" @@ -15,14 +13,13 @@ import ( "net/http" "net/url" "os" - "os/exec" "path/filepath" - "runtime" "slices" "strconv" "strings" "time" + "github.com/asticode/go-astiav" youtube "github.com/kkdai/youtube/v2" publicapi "github.com/listenbox/listenbox-cli/publicapi" ) @@ -32,7 +29,6 @@ const ( youtubeCleanupTimeout = 10 * time.Second youtubeFileMode = 0o600 youtubeDirectoryMode = 0o700 - youtubeProcessWait = 2 * time.Second youtubeResponseLimit = 64 << 10 youtubeVideoKind = "video" youtubeAudioKind = "audio" @@ -264,7 +260,7 @@ func downloadYouTubeVideo(ctx context.Context, client *youtube.Client, video *yo if err := downloadYouTubeStream(ctx, client, video, selected, videoPath); err != nil { return err } - args := []string{"-i", videoPath} + audioPath := videoPath if selected.AudioChannels == 0 { var audio *youtube.Format for index := range formats { @@ -276,17 +272,16 @@ func downloadYouTubeVideo(ctx context.Context, client *youtube.Client, video *yo if audio == nil { return errors.New("YouTube video has no audio stream") } - audioPath := filepath.Join(directory, "source-audio") + audioPath = filepath.Join(directory, "source-audio") if err := downloadYouTubeStream(ctx, client, video, audio, audioPath); err != nil { return err } - args = append(args, "-i", audioPath, "-map", "0:v:0", "-map", "1:a:0") - } else { - args = append(args, "-map", "0:v:0", "-map", "0:a:0") } - args = append(args, "-map_metadata", "-1", "-c:v", "copy", "-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", filepath.Join(directory, "video.mp4")) - _, err := runBundledMedia(ctx, "ffmpeg", args...) - return err + preparedAudio := filepath.Join(directory, "audio.m4a") + if err := prepareMediaAudio(ctx, audioPath, preparedAudio); err != nil { + return err + } + return muxMedia(ctx, videoPath, preparedAudio, filepath.Join(directory, "video.mp4")) } func downloadYouTubeStream(ctx context.Context, client *youtube.Client, video *youtube.Video, format *youtube.Format, destination string) error { @@ -315,63 +310,27 @@ func downloadYouTubeStream(ctx context.Context, client *youtube.Client, video *y return errors.Join(copyErr, file.Close()) } -func runBundledMedia(ctx context.Context, name string, args ...string) ([]byte, error) { - executable, err := os.Executable() - if err != nil { - return nil, err - } - fileName := name - if runtime.GOOS == "windows" { - fileName += ".exe" - } - program := filepath.Join(filepath.Dir(executable), fileName) - if name == "ffmpeg" { - args = append([]string{"-nostdin", "-hide_banner", "-loglevel", "error", "-y"}, args...) - } - command := exec.CommandContext(ctx, program, args...) - command.WaitDelay = youtubeProcessWait - var stderr bytes.Buffer - command.Stderr = &stderr - output, err := command.Output() - if err != nil { - return nil, fmt.Errorf("bundled %s: %w: %s", name, err, strings.TrimSpace(stderr.String())) - } - return output, nil -} - func packageYouTubeVideo(ctx context.Context, directory string) (int64, error) { source := filepath.Join(directory, "video.mp4") - var dimensions struct { - Streams []struct { - Width int `json:"width"` - Height int `json:"height"` - } `json:"streams"` - } - encoded, err := runBundledMedia(ctx, "ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", "-of", "json", source) + width, height, err := mediaDimensions(source) if err != nil { return 0, err } - if err := json.Unmarshal(encoded, &dimensions); err != nil { - return 0, err - } - if len(dimensions.Streams) != 1 { - return 0, errors.New("processed video has no video stream") - } for _, kind := range []string{youtubeVideoKind, youtubeAudioKind} { root := filepath.Join(directory, "hls", kind) if err := os.MkdirAll(root, youtubeDirectoryMode); err != nil { return 0, err } - mapping := "0:v:0" + mediaType := astiav.MediaTypeVideo if kind == youtubeAudioKind { - mapping = "0:a:0" + mediaType = astiav.MediaTypeAudio } - _, err := runBundledMedia(ctx, "ffmpeg", "-i", source, "-map", mapping, "-map_metadata", "-1", "-c", "copy", "-f", "hls", "-hls_time", "6", "-hls_playlist_type", "vod", "-hls_segment_type", "fmp4", "-hls_fmp4_init_filename", "init.mp4", "-hls_segment_filename", filepath.Join(root, "segment-%05d.m4s"), filepath.Join(root, "index.m3u8")) + err := packageMediaTrack(ctx, source, root, mediaType) if err != nil { return 0, err } } - master := fmt.Sprintf("#EXTM3U\n#EXT-X-VERSION:7\n#EXT-X-INDEPENDENT-SEGMENTS\n#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio\",NAME=\"Audio\",DEFAULT=YES,AUTOSELECT=YES,URI=\"audio/index.m3u8\"\n#EXT-X-STREAM-INF:BANDWIDTH=12000000,RESOLUTION=%dx%d,AUDIO=\"audio\"\nvideo/index.m3u8\n", dimensions.Streams[0].Width, dimensions.Streams[0].Height) + master := fmt.Sprintf("#EXTM3U\n#EXT-X-VERSION:7\n#EXT-X-INDEPENDENT-SEGMENTS\n#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio\",NAME=\"Audio\",DEFAULT=YES,AUTOSELECT=YES,URI=\"audio/index.m3u8\"\n#EXT-X-STREAM-INF:BANDWIDTH=12000000,RESOLUTION=%dx%d,AUDIO=\"audio\"\nvideo/index.m3u8\n", width, height) if err := os.WriteFile(filepath.Join(directory, "hls", "master.m3u8"), []byte(master), youtubeFileMode); err != nil { return 0, err } diff --git a/moon.yml b/moon.yml index 7f63cdc..abf80fd 100644 --- a/moon.yml +++ b/moon.yml @@ -2,33 +2,32 @@ toolchains: default: system fileGroups: sources: - - bundle-media.ts + - prepare-ffmpeg.ts + - ffmpeg-env.sh - go.mod - go.sum - ./**/*.go - ./**/*.yaml tasks: - media-runtime: - description: Bundle FFmpeg 9+ and FFprobe beside the native CLI executable. + native-ffmpeg: + description: Build pinned static FFmpeg libraries for go-astiav. inputs: - - bundle-media.ts - checks: - - check: fingerprint - script: ffmpeg -version && ffprobe -version - hash: stdout - command: node bundle-media.ts + - prepare-ffmpeg.ts + command: node prepare-ffmpeg.ts outputs: - - dist/ffmpeg - - dist/ffprobe - - dist/ffmpeg-NOTICE.txt - - dist/ffprobe-NOTICE.txt + - .cache/ffmpeg + options: + cache: true build: deps: - ~:mod-download - - ~:media-runtime + - ~:native-ffmpeg inputs: - "@group(sources)" - command: go build -o dist/listenbox ./cmd/listenbox + script: |- + set -euo pipefail + source ffmpeg-env.sh + go build -trimpath -ldflags="-s -w" -o dist/listenbox ./cmd/listenbox outputs: - dist/listenbox mod-download: @@ -43,31 +42,37 @@ tasks: test: deps: - ~:mod-download + - ~:native-ffmpeg inputs: - moon.yml + - ffmpeg-env.sh - go.mod - go.sum - ./**/*.go - ./**/*.yaml + - internal/cli/testdata/**/* script: |- set -euo pipefail - go test -count=1 -tags=dev ./... + source ffmpeg-env.sh + go test -count=1 -tags=dev -timeout=30s ./... options: cache: true lint: - env: - CGO_ENABLED: "0" deps: - ~:mod-download + - ~:native-ffmpeg inputs: - moon.yml + - ffmpeg-env.sh - go.mod - go.sum - .golangci.yaml - ./**/*.go - ./**/*.yaml + - internal/cli/testdata/**/* script: |- set -euo pipefail + source ffmpeg-env.sh if [[ "${CI:-}" == "true" ]]; then test -z "$(gofmt -l .)" else @@ -81,19 +86,19 @@ tasks: options: cache: true smoke: - description: Smoke-test the CLI entrypoint directly from source. + description: Smoke-test the CLI executable. deps: - - ~:mod-download - inputs: - - "@group(sources)" - command: go run ./cmd/listenbox help + - ~:build + command: dist/listenbox help options: cache: true package: description: Package the CLI with its native media runtime and license notices. deps: - ~:build - script: tar -czf dist/listenbox.tar.gz -C dist listenbox ffmpeg ffprobe ffmpeg-NOTICE.txt ffprobe-NOTICE.txt + script: |- + cp .cache/ffmpeg/NOTICE.txt dist/ffmpeg-NOTICE.txt + tar -czf dist/listenbox.tar.gz -C dist listenbox ffmpeg-NOTICE.txt -C .. LICENSE outputs: - dist/listenbox.tar.gz check: diff --git a/prepare-ffmpeg.ts b/prepare-ffmpeg.ts new file mode 100644 index 0000000..2ed17df --- /dev/null +++ b/prepare-ffmpeg.ts @@ -0,0 +1,74 @@ +import { spawn } from "node:child_process" +import { createHash } from "node:crypto" +import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises" +import { availableParallelism } from "node:os" +import { join, resolve } from "node:path" + +const cache = resolve(import.meta.dirname, ".cache") +const prefix = join(cache, "ffmpeg") +const version = "9.0.2" +const checksum = + "8c3850283eb25fa026482078a04051e0be17347b09ef81a0849bec15a96e002e" +const archive = join(cache, `ffmpeg-${version}.tar.xz`) +const source = join(cache, `ffmpeg-${version}`) + +async function run(command: string, args: string[], cwd: string) { + await new Promise((resolve, reject) => { + const child = spawn(command, args, { cwd, stdio: "inherit" }) + child.once("error", reject) + child.once("exit", (code, signal) => { + if (code === 0) resolve() + else reject(new Error(`${command} failed: ${signal ?? code}`)) + }) + }) +} + +await mkdir(cache, { recursive: true }) +const response = await fetch( + `https://ffmpeg.org/releases/ffmpeg-${version}.tar.xz`, +) +if (!response.ok) throw new Error(`Download FFmpeg: HTTP ${response.status}`) +const bytes = Buffer.from(await response.arrayBuffer()) +if (createHash("sha256").update(bytes).digest("hex") !== checksum) { + throw new Error("FFmpeg source checksum mismatch") +} +await writeFile(archive, bytes) +await run("tar", ["-xf", archive, "-C", cache], cache) +await run( + "./configure", + [ + `--prefix=${prefix}`, + "--disable-everything", + "--disable-autodetect", + "--disable-programs", + "--disable-doc", + "--disable-debug", + "--disable-network", + "--disable-shared", + "--enable-static", + "--enable-pic", + "--enable-avdevice", + "--enable-avfilter", + "--enable-swscale", + "--enable-swresample", + "--enable-protocol=file", + "--enable-demuxer=mov,matroska,ogg,aac", + "--enable-muxer=ipod,mp4,hls,mpegts", + "--enable-decoder=aac,opus,vorbis,h264", + "--enable-encoder=aac", + "--enable-parser=aac,opus,vorbis,h264", + "--enable-bsf=aac_adtstoasc,h264_mp4toannexb", + "--disable-x86asm", + ], + source, +) +await run("make", [`-j${availableParallelism()}`], source) +await run("make", ["install"], source) +await copyFile( + join(source, "COPYING.LGPLv2.1"), + join(prefix, "COPYING.LGPLv2.1"), +) +await writeFile( + join(prefix, "NOTICE.txt"), + `FFmpeg ${version}\nSource: https://ffmpeg.org/releases/ffmpeg-${version}.tar.xz\nSHA-256: ${checksum}\nConfiguration: see prepare-ffmpeg.ts\n${await readFile(join(source, "COPYING.LGPLv2.1"), "utf8")}`, +)