Compare commits
4 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
3f85891111 | |
|
|
ceb495ad1d | |
|
|
e668777431 | |
|
|
83663aa9db |
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
converter "github.com/yazmeyaa/telegram_sticker_converter"
|
||||
"github.com/yazmeyaa/telegram_sticker_converter/tgs"
|
||||
|
|
@ -31,7 +32,7 @@ func main() {
|
|||
inputType := inputParts[0]
|
||||
var in *os.File
|
||||
if inputType == "file" {
|
||||
input, err := os.Open(*input)
|
||||
input, err := os.Open(inputParts[1])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error opening file: %v\n", err)
|
||||
os.Exit(1)
|
||||
|
|
@ -82,11 +83,13 @@ func main() {
|
|||
}
|
||||
|
||||
converter := tgs.NewConverter()
|
||||
|
||||
start := time.Now()
|
||||
if err := converter.Transform(context.Background(), in, out, opts); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "transform failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
duration := time.Since(start)
|
||||
fmt.Printf("Complete in %d ms\n", duration.Milliseconds())
|
||||
|
||||
fmt.Println("success:", *outPath)
|
||||
}
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -3,8 +3,8 @@ module github.com/yazmeyaa/telegram_sticker_converter
|
|||
go 1.24.6
|
||||
|
||||
require (
|
||||
github.com/arugaz/go-rlottie v0.1.0
|
||||
github.com/u2takey/ffmpeg-go v0.5.0
|
||||
github.com/yazmeyaa/go-rlottie v1.0.3
|
||||
golang.org/x/image v0.30.0
|
||||
)
|
||||
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -1,5 +1,3 @@
|
|||
github.com/arugaz/go-rlottie v0.1.0 h1:/rQaoBoSEG2T+PW56ANvpkgOHdYYtGZpvxzjI6touZ8=
|
||||
github.com/arugaz/go-rlottie v0.1.0/go.mod h1:m50xy50q5U9ngFIBJja9m09vFhvfw6cxkRiqIxjKeWQ=
|
||||
github.com/aws/aws-sdk-go v1.38.20/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
|
||||
github.com/aws/aws-sdk-go v1.55.8 h1:JRmEUbU52aJQZ2AjX4q4Wu7t4uZjOu71uyNmaWlUkJQ=
|
||||
github.com/aws/aws-sdk-go v1.55.8/go.mod h1:ZkViS9AqA6otK+JBBNH2++sx1sgxrPKcSzPPvQkUtXk=
|
||||
|
|
@ -39,6 +37,8 @@ github.com/u2takey/ffmpeg-go v0.5.0 h1:r7d86XuL7uLWJ5mzSeQ03uvjfIhiJYvsRAJFCW4uk
|
|||
github.com/u2takey/ffmpeg-go v0.5.0/go.mod h1:ruZWkvC1FEiUNjmROowOAps3ZcWxEiOpFoHCvk97kGc=
|
||||
github.com/u2takey/go-utils v0.3.1 h1:TaQTgmEZZeDHQFYfd+AdUT1cT4QJgJn/XVPELhHw4ys=
|
||||
github.com/u2takey/go-utils v0.3.1/go.mod h1:6e+v5vEZ/6gu12w/DC2ixZdZtCrNokVxD0JUklcqdCs=
|
||||
github.com/yazmeyaa/go-rlottie v1.0.3 h1:md1ptW6ZWEv9U6YpC/kg2QbQQoIhJcHik4U9+WAGNyc=
|
||||
github.com/yazmeyaa/go-rlottie v1.0.3/go.mod h1:Ci2En07mkAOGiTMq5/js/AkTwH6q/ov/nA4lBkkUoB8=
|
||||
gocv.io/x/gocv v0.25.0/go.mod h1:Rar2PS6DV+T4FL+PM535EImD/h13hGVaHhnCu1xarBs=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
|
|
|
|||
1
tgs.go
1
tgs.go
|
|
@ -13,6 +13,7 @@ type TGSTransformOptions struct {
|
|||
Qualtity int
|
||||
ResizeWidth int
|
||||
ResizeHeight int
|
||||
CacheKey string
|
||||
}
|
||||
|
||||
type TGSConverter interface {
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ import (
|
|||
"image/png"
|
||||
"io"
|
||||
|
||||
"github.com/arugaz/go-rlottie"
|
||||
ffmpeg_go "github.com/u2takey/ffmpeg-go"
|
||||
"github.com/yazmeyaa/go-rlottie"
|
||||
converter "github.com/yazmeyaa/telegram_sticker_converter"
|
||||
)
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ func (t tgsConverterImpl) Transform(ctx context.Context, in io.Reader, out io.Wr
|
|||
return err
|
||||
}
|
||||
|
||||
anim := rlottie.LottieAnimationFromData(buf.String(), "", "")
|
||||
anim := rlottie.LottieAnimationFromData(buf.String(), opts.CacheKey, "")
|
||||
defer rlottie.LottieAnimationDestroy(anim)
|
||||
|
||||
width, height := rlottie.LottieAnimationGetSize(anim)
|
||||
|
|
@ -102,12 +102,14 @@ var (
|
|||
"deadline": "realtime",
|
||||
}
|
||||
PresetMP4 = ffmpeg_go.KwArgs{
|
||||
"vcodec": "libx264",
|
||||
"format": "mp4",
|
||||
"c:v": "libx264",
|
||||
"f": "mp4",
|
||||
"pix_fmt": "yuv420p",
|
||||
"movflags": "frag_keyframe+empty_moov",
|
||||
"preset": "ultrafast",
|
||||
"tune": "zerolatency",
|
||||
"c:a": "aac",
|
||||
"shortest": "",
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -143,19 +145,35 @@ func (t tgsConverterImpl) processVideo(ctx context.Context, anim rlottie.Lottie_
|
|||
w.Close()
|
||||
}()
|
||||
|
||||
err := ffmpeg_go.
|
||||
Input("pipe:0", ffmpeg_go.KwArgs{
|
||||
"format": "rawvideo",
|
||||
"pix_fmt": "bgra",
|
||||
"s": fmt.Sprintf("%dx%d", opts.ResizeWidth, opts.ResizeHeight),
|
||||
"r": frameRate,
|
||||
}).
|
||||
Silent(true).
|
||||
Output("pipe:1", preset).
|
||||
inputVideo := ffmpeg_go.Input("pipe:0", ffmpeg_go.KwArgs{
|
||||
"format": "rawvideo",
|
||||
"pix_fmt": "bgra",
|
||||
"s": fmt.Sprintf("%dx%d", opts.ResizeWidth, opts.ResizeHeight),
|
||||
"r": frameRate,
|
||||
})
|
||||
|
||||
var output *ffmpeg_go.Stream
|
||||
|
||||
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).
|
||||
WithOutput(out).
|
||||
Silent(true).
|
||||
Run()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ var (
|
|||
"movflags": "frag_keyframe+empty_moov",
|
||||
"preset": "ultrafast",
|
||||
"tune": "zerolatency",
|
||||
"c:a": "aac",
|
||||
"shortest": "",
|
||||
}
|
||||
|
||||
PresetPNG = ffmpeg_go.KwArgs{
|
||||
|
|
|
|||
|
|
@ -31,21 +31,39 @@ func (ws webmConverter) Transform(ctx context.Context, in io.Reader, out io.Writ
|
|||
return err
|
||||
}
|
||||
|
||||
stream := ffmpeg_go.
|
||||
Input("pipe:0", ffmpeg_go.KwArgs{
|
||||
"f": "webm",
|
||||
}).
|
||||
Silent(true).
|
||||
Output("pipe:1", preset).
|
||||
WithInput(rIn).
|
||||
WithOutput(wOut)
|
||||
inputVideo := ffmpeg_go.
|
||||
Input("pipe:0").
|
||||
Silent(true)
|
||||
|
||||
var output *ffmpeg_go.Stream
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
output = output.Silent(true).WithInput(rIn).WithOutput(wOut)
|
||||
|
||||
if (opts.Frame == converter.FrameAll || opts.Frame == converter.FrameRange) &&
|
||||
(opts.Format == converter.FormatWEBP || opts.Format == converter.FormatJPEG || opts.Format == converter.FormatPNG) {
|
||||
|
||||
output = output.Silent(true).WithInput(rIn).WithOutput(wOut)
|
||||
|
||||
go func() {
|
||||
defer wOut.Close()
|
||||
_ = stream.Run()
|
||||
_ = output.Run()
|
||||
}()
|
||||
|
||||
zw := zip.NewWriter(out)
|
||||
|
|
@ -77,8 +95,7 @@ func (ws webmConverter) Transform(ctx context.Context, in io.Reader, out io.Writ
|
|||
return nil
|
||||
}
|
||||
|
||||
defer wOut.Close()
|
||||
if err := stream.WithOutput(out).Run(); err != nil {
|
||||
if err := output.WithInput(rIn).WithOutput(out).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue