Compare commits

..

3 Commits

Author SHA1 Message Date
eugene 3f85891111 add anullsrc audio input to webm converter 2025-09-16 18:59:57 +03:00
eugene ceb495ad1d add anullsrc to mp4 ffmpeg build (tgs) 2025-09-16 18:42:19 +03:00
eugene e668777431 add cachekey 2025-09-14 22:39:50 +03:00
4 changed files with 62 additions and 24 deletions

1
tgs.go
View File

@ -13,6 +13,7 @@ type TGSTransformOptions struct {
Qualtity int Qualtity int
ResizeWidth int ResizeWidth int
ResizeHeight int ResizeHeight int
CacheKey string
} }
type TGSConverter interface { type TGSConverter interface {

View File

@ -38,7 +38,7 @@ func (t tgsConverterImpl) Transform(ctx context.Context, in io.Reader, out io.Wr
return err return err
} }
anim := rlottie.LottieAnimationFromData(buf.String(), "", "") anim := rlottie.LottieAnimationFromData(buf.String(), opts.CacheKey, "")
defer rlottie.LottieAnimationDestroy(anim) defer rlottie.LottieAnimationDestroy(anim)
width, height := rlottie.LottieAnimationGetSize(anim) width, height := rlottie.LottieAnimationGetSize(anim)
@ -102,12 +102,14 @@ var (
"deadline": "realtime", "deadline": "realtime",
} }
PresetMP4 = ffmpeg_go.KwArgs{ PresetMP4 = ffmpeg_go.KwArgs{
"vcodec": "libx264", "c:v": "libx264",
"format": "mp4", "f": "mp4",
"pix_fmt": "yuv420p", "pix_fmt": "yuv420p",
"movflags": "frag_keyframe+empty_moov", "movflags": "frag_keyframe+empty_moov",
"preset": "ultrafast", "preset": "ultrafast",
"tune": "zerolatency", "tune": "zerolatency",
"c:a": "aac",
"shortest": "",
} }
) )
@ -143,19 +145,35 @@ func (t tgsConverterImpl) processVideo(ctx context.Context, anim rlottie.Lottie_
w.Close() w.Close()
}() }()
err := ffmpeg_go. inputVideo := ffmpeg_go.Input("pipe:0", ffmpeg_go.KwArgs{
Input("pipe:0", ffmpeg_go.KwArgs{ "format": "rawvideo",
"format": "rawvideo", "pix_fmt": "bgra",
"pix_fmt": "bgra", "s": fmt.Sprintf("%dx%d", opts.ResizeWidth, opts.ResizeHeight),
"s": fmt.Sprintf("%dx%d", opts.ResizeWidth, opts.ResizeHeight), "r": frameRate,
"r": frameRate, })
}).
Silent(true). var output *ffmpeg_go.Stream
Output("pipe:1", preset).
switch opts.Format {
case converter.FormatMP4:
inputAudio := ffmpeg_go.Input("anullsrc=channel_layout=stereo:sample_rate=44100",
ffmpeg_go.KwArgs{"f": "lavfi"})
output = ffmpeg_go.Output(
[]*ffmpeg_go.Stream{inputVideo, inputAudio},
"pipe:1",
preset,
)
default:
output = inputVideo.Output("pipe:1", preset)
}
err := output.
WithInput(r). WithInput(r).
WithOutput(out). WithOutput(out).
Silent(true).
Run() Run()
if err != nil { if err != nil {
return err return err
} }

View File

@ -15,6 +15,8 @@ var (
"movflags": "frag_keyframe+empty_moov", "movflags": "frag_keyframe+empty_moov",
"preset": "ultrafast", "preset": "ultrafast",
"tune": "zerolatency", "tune": "zerolatency",
"c:a": "aac",
"shortest": "",
} }
PresetPNG = ffmpeg_go.KwArgs{ PresetPNG = ffmpeg_go.KwArgs{

View File

@ -31,21 +31,39 @@ func (ws webmConverter) Transform(ctx context.Context, in io.Reader, out io.Writ
return err return err
} }
stream := ffmpeg_go. inputVideo := ffmpeg_go.
Input("pipe:0", ffmpeg_go.KwArgs{ Input("pipe:0").
"f": "webm", Silent(true)
}).
Silent(true). var output *ffmpeg_go.Stream
Output("pipe:1", preset).
WithInput(rIn). switch opts.Format {
WithOutput(wOut) case converter.FormatMP4:
inputAudio := ffmpeg_go.Input(
"anullsrc=channel_layout=stereo:sample_rate=44100",
ffmpeg_go.KwArgs{"f": "lavfi"},
)
output = ffmpeg_go.Output(
[]*ffmpeg_go.Stream{inputVideo, inputAudio},
"pipe:1",
preset,
)
default:
output = inputVideo.Output("pipe:1", preset)
}
output = output.Silent(true).WithInput(rIn).WithOutput(wOut)
if (opts.Frame == converter.FrameAll || opts.Frame == converter.FrameRange) && if (opts.Frame == converter.FrameAll || opts.Frame == converter.FrameRange) &&
(opts.Format == converter.FormatWEBP || opts.Format == converter.FormatJPEG || opts.Format == converter.FormatPNG) { (opts.Format == converter.FormatWEBP || opts.Format == converter.FormatJPEG || opts.Format == converter.FormatPNG) {
output = output.Silent(true).WithInput(rIn).WithOutput(wOut)
go func() { go func() {
defer wOut.Close() defer wOut.Close()
_ = stream.Run() _ = output.Run()
}() }()
zw := zip.NewWriter(out) zw := zip.NewWriter(out)
@ -77,8 +95,7 @@ func (ws webmConverter) Transform(ctx context.Context, in io.Reader, out io.Writ
return nil return nil
} }
defer wOut.Close() if err := output.WithInput(rIn).WithOutput(out).Run(); err != nil {
if err := stream.WithOutput(out).Run(); err != nil {
return err return err
} }