-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageseq.go
More file actions
106 lines (90 loc) · 3.06 KB
/
Copy pathimageseq.go
File metadata and controls
106 lines (90 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//go:build amd64 || arm64
package ffmpeg
import (
"fmt"
"github.com/bstkhq/go-ffmpeg-ffi/avutil"
)
// ImageSequenceConfig configures image sequence reading or writing.
type ImageSequenceConfig struct {
// Pattern is the file pattern with printf-style format specifier.
// Examples: "frame_%04d.png", "image-%03d.jpg", "out%d.bmp"
Pattern string
// StartNumber is the first frame number (default: 0).
StartNumber int
// FrameRate is the frame rate for the sequence.
// For decoding, this determines the duration of each frame.
// For encoding, this is the output frame rate.
FrameRate avutil.Rational
}
// NewImageSequenceDecoder creates a decoder that reads an image sequence as video.
// The pattern should use printf-style format specifier (e.g., "frame_%04d.png").
// The sequence is decoded as a video stream with the specified frame rate.
func NewImageSequenceDecoder(config ImageSequenceConfig, decoderOpts *DecoderOptions) (*Decoder, error) {
if config.Pattern == "" {
return nil, fmt.Errorf("ffmpeg: image sequence pattern cannot be empty")
}
return NewDecoder(config.Pattern, imageSequenceDecoderOptions(config, decoderOpts))
}
func imageSequenceDecoderOptions(config ImageSequenceConfig, decoderOpts *DecoderOptions) *DecoderOptions {
decoderOpts = cloneDecoderOptions(decoderOpts)
if decoderOpts.AVOptions == nil {
decoderOpts.AVOptions = make(map[string]string)
}
decoderOpts.Format = "image2"
decoderOpts.AVOptions["pattern_type"] = "sequence"
if config.StartNumber > 0 {
decoderOpts.AVOptions["start_number"] = fmt.Sprintf("%d", config.StartNumber)
}
if config.FrameRate.Num > 0 && config.FrameRate.Den > 0 {
decoderOpts.AVOptions["framerate"] = fmt.Sprintf("%d/%d", config.FrameRate.Num, config.FrameRate.Den)
}
return decoderOpts
}
// NewImageSequenceEncoder creates an encoder that writes video as an image sequence.
// The pattern should use printf-style format specifier (e.g., "frame_%04d.png").
// The output format is determined by the file extension in the pattern.
func NewImageSequenceEncoder(config ImageSequenceConfig, width, height int, pixFmt PixelFormat) (*Encoder, error) {
if config.Pattern == "" {
return nil, fmt.Errorf("ffmpeg: image sequence pattern cannot be empty")
}
// Determine codec from pattern extension
codec := "png" // default
if len(config.Pattern) > 4 {
ext := config.Pattern[len(config.Pattern)-4:]
switch ext {
case ".jpg", ".JPG":
codec = "mjpeg"
case "jpeg", "JPEG":
codec = "mjpeg"
case ".bmp", ".BMP":
codec = "bmp"
case ".png", ".PNG":
codec = "png"
}
}
frameRate := config.FrameRate
if frameRate.Num <= 0 || frameRate.Den <= 0 {
frameRate = NewRational(25, 1)
}
// Map codec name to codec ID
var codecID CodecID
switch codec {
case "mjpeg":
codecID = CodecIDMJPEG
case "bmp":
codecID = CodecIDBMP
case "png":
codecID = CodecIDPNG
default:
codecID = CodecIDPNG
}
return NewEncoder(config.Pattern, &EncoderOptions{
Video: &VideoEncoderConfig{
Width: width,
Height: height,
PixelFormat: pixFmt,
FrameRate: frameRate,
Codec: codecID,
},
})
}