Skip to content

Commit be58d09

Browse files
committed
Adding back extension filtering for directories
1 parent a1ce32b commit be58d09

File tree

1 file changed

+110
-6
lines changed

1 file changed

+110
-6
lines changed

main.go

+110-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929
"unicode"
3030

31+
"github.com/asticode/go-astiav"
3132
"github.com/davidbyttow/govips/v2/vips"
3233
"github.com/gabriel-vasile/mimetype"
3334
"github.com/kenshaw/colors"
@@ -137,7 +138,7 @@ func open(name string) ([]string, error) {
137138
return nil, fmt.Errorf("unable to open directory %q: %v", name, err)
138139
}
139140
for _, entry := range entries {
140-
if s := entry.Name(); !entry.IsDir() {
141+
if s := entry.Name(); !entry.IsDir() && extensions[strings.TrimPrefix(filepath.Ext(s), ".")] {
141142
v = append(v, filepath.Join(name, s))
142143
}
143144
}
@@ -188,9 +189,9 @@ func (args *Args) renderFile(name string) (image.Image, string, error) {
188189
switch ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(name), ".")); {
189190
case typ == "image/svg":
190191
g = args.renderResvg
191-
case isImageBuiltin(typ):
192+
case isImageBuiltin(typ): // builtin
192193
g = args.renderImage
193-
case isLibreOffice(typ, ext):
194+
case isLibreOffice(typ, ext): // soffice
194195
g, notStream = args.renderLibreOffice, true
195196
case isVipsImage(typ): // use vips
196197
g = args.renderVips
@@ -295,9 +296,31 @@ func (args *Args) renderVips(r io.Reader, name string) (image.Image, error) {
295296
}
296297

297298
// renderAstiv renders the image using the astiav (ffmpeg) library.
298-
func (args *Args) renderAstiav(r io.Reader, name string) (image.Image, error) {
299+
func (args *Args) renderAstiav(_ io.Reader, name string) (image.Image, error) {
299300
astiavOnce.Do(astiavInit(args.logger, args.Verbose))
300-
return nil, nil
301+
in := astiav.AllocFormatContext()
302+
defer in.Free()
303+
if err := in.OpenInput(name, nil, nil); err != nil {
304+
return nil, err
305+
}
306+
defer in.CloseInput()
307+
if err := in.FindStreamInfo(nil); err != nil {
308+
return nil, err
309+
}
310+
for i, is := range in.Streams() {
311+
p := is.CodecParameters()
312+
typ := p.MediaType()
313+
if typ != astiav.MediaTypeVideo {
314+
continue
315+
}
316+
args.logger("stream %d: %s", i, typ)
317+
rate := ox.NewRate(p.BitRate(), time.Second)
318+
args.logger(
319+
" bit rate: %s, pixel format: %v, time base: %v, duration: %v, frames: %v",
320+
rate, p.PixelFormat(), is.TimeBase(), is.Duration(), is.NbFrames(),
321+
)
322+
}
323+
return nil, errors.New("oops!")
301324
}
302325

303326
// renderLibreOffice renders the image using the `soffice` command.
@@ -506,6 +529,20 @@ func vipsLevel(level vips.LogLevel) string {
506529
// astiavInit initializes the astiav package.
507530
func astiavInit(logger func(string, ...any), verbose bool) func() {
508531
return func() {
532+
level := astiav.LogLevelQuiet
533+
if verbose {
534+
level = astiav.LogLevelDebug
535+
}
536+
astiav.SetLogLevel(level)
537+
astiav.SetLogCallback(func(c astiav.Classer, l astiav.LogLevel, fmt, msg string) {
538+
var cs string
539+
if c != nil {
540+
if cl := c.Class(); cl != nil {
541+
cs = "class: " + cl.String()
542+
}
543+
}
544+
logger("astiav %d: %s%s", l, strings.TrimSpace(msg), cs)
545+
})
509546
}
510547
}
511548

@@ -661,7 +698,74 @@ func isLibreOffice(typ, ext string) bool {
661698
return false
662699
}
663700

664-
var urlRE = regexp.MustCompile(`(?i)^https?`)
701+
// extensions are the extensions to check for directories.
702+
var extensions = map[string]bool{
703+
"3g2": true,
704+
"3gp": true,
705+
"asf": true,
706+
"avif": true,
707+
"avi": true,
708+
"bmp": true,
709+
"bpg": true,
710+
"csv": true,
711+
"doc": true,
712+
"docx": true,
713+
"dvb": true,
714+
"dwg": true,
715+
"eot": true,
716+
"flv": true,
717+
"gif": true,
718+
"heic": true,
719+
"heif": true,
720+
"ico": true,
721+
"jp2": true,
722+
"jpeg": true,
723+
"jpf": true,
724+
"jpg": true,
725+
"jxl": true,
726+
"jxs": true,
727+
"m4v": true,
728+
"markdown": true,
729+
"md": true,
730+
"mj2": true,
731+
"mkv": true,
732+
"mov": true,
733+
"mp4": true,
734+
"mpeg": true,
735+
"mpg": true,
736+
"odc": true,
737+
"odf": true,
738+
"odg": true,
739+
"odp": true,
740+
"ods": true,
741+
"odt": true,
742+
"otf": true,
743+
"otg": true,
744+
"otp": true,
745+
"ots": true,
746+
"ott": true,
747+
"pdf": true,
748+
"png": true,
749+
"ppt": true,
750+
"pptx": true,
751+
"pub": true,
752+
"rtf": true,
753+
"svg": true,
754+
"tiff": true,
755+
"tsv": true,
756+
"ttc": true,
757+
"ttf": true,
758+
"txt": true,
759+
"webm": true,
760+
"webp": true,
761+
"woff2": true,
762+
"woff": true,
763+
"xls": true,
764+
"xlsx": true,
765+
"xpm": true,
766+
}
767+
768+
var urlRE = regexp.MustCompile(`(?i)^https?://`)
665769

666770
var (
667771
vipsOnce sync.Once

0 commit comments

Comments
 (0)