/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Single-file source-code for id3pic. To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath id3pic.go */ package main import ( "bufio" "encoding/binary" "errors" "io" "os" ) const info = ` id3pic [options...] [file...] Extract picture/thumbnail bytes from ID3/MP3 metadata, if available. All (optional) leading options start with either single or double-dash: -h show this help message -help show this help message ` // errNoMoreOutput is a dummy error, whose message is ignored, and which // causes the app to quit immediately and successfully var errNoMoreOutput = errors.New(`no more output`) // errNoThumb is a generic error to handle lack of thumbnails, in case no // picture-metadata-starters are found at all var errNoThumb = errors.New(`no thumbnail data found`) const errorStyle = "\x1b[31m" func main() { if len(os.Args) > 1 { switch os.Args[1] { case `-h`, `--h`, `-help`, `--help`: os.Stderr.WriteString(info[1:]) return } } if len(os.Args) > 2 { os.Stderr.WriteString(errorStyle) os.Stderr.WriteString(`can only handle 1 file`) os.Stderr.WriteString("\x1b[0m\n") os.Exit(1) } name := `-` if len(os.Args) > 1 { name = os.Args[1] } if err := run(os.Stdout, name); isActualError(err) { os.Stderr.WriteString(errorStyle) os.Stderr.WriteString(err.Error()) os.Stderr.WriteString("\x1b[0m\n") os.Exit(1) } } func run(w io.Writer, name string) error { if name == `-` { return id3pic(w, bufio.NewReader(os.Stdin)) } f, err := os.Open(name) if err != nil { return errors.New(`can't read from file named "` + name + `"`) } defer f.Close() return id3pic(w, bufio.NewReader(f)) } // isActualError is to figure out whether not to ignore an error, and thus // show it as an error message func isActualError(err error) bool { return err != nil && err != io.EOF && err != errNoMoreOutput } func match(r *bufio.Reader, what []byte) bool { for _, v := range what { b, err := r.ReadByte() if b != v || err != nil { return false } } return true } func id3pic(w io.Writer, r *bufio.Reader) error { for { b, err := r.ReadByte() if err == io.EOF { return errNoThumb } if err != nil { return err } // handle APIC-type chunks if b == 'A' { if match(r, []byte{'P', 'I', 'C'}) { // section-size seems stored as 4 big-endian bytes var size uint32 err := binary.Read(r, binary.BigEndian, &size) if err != nil { return err } // a, err1 := r.ReadByte() // b, err2 := r.ReadByte() // c, err3 := r.ReadByte() // d, err4 := r.ReadByte() // if err1 != nil || err2 != nil || err3 != nil || err4 != nil { // continue // } // var size int64 = int(a)<<24 + int(b)<<16 + int(c)<<8 + int(d) n, err := skipThumbnailTypeAPIC(r) if err != nil { continue } _, err = io.Copy(w, io.LimitReader(r, int64(int(size)-n))) return err } } // handle PIC-type chunks if b == 'P' { if match(r, []byte{'I', 'C'}) { // http://www.unixgods.org/Ruby/ID3/docs/id3v2-00.html#PIC // thumbnail-payload-size seems stored as 3 big-endian bytes a, err1 := r.ReadByte() b, err2 := r.ReadByte() c, err3 := r.ReadByte() if err1 != nil || err2 != nil || err3 != nil { continue } // skip the text encoding n, err := r.Discard(5) if n != 5 || err != nil { continue } // skip a null-delimited string _, err = r.ReadSlice(0) if err != nil { continue } size := int(a)<<16 + int(b)<<8 + int(c) _, err = io.Copy(w, io.LimitReader(r, int64(size))) return err } } } } func skipThumbnailTypeAPIC(r *bufio.Reader) (int, error) { n := 0 _, err := r.Discard(1) if err != nil { return -1, errors.New(`failed to sync APIC thumbnail text-encoding`) } n++ junk, err := r.ReadSlice('/') if err != nil { return -1, errors.New(`failed to sync to APIC thumbnail MIME-type`) } n += len(junk) // skip a null-delimited string junk, err = r.ReadSlice(0) if err != nil { return -1, errors.New(`failed to sync to APIC thumbnail MIME-type`) } n += len(junk) _, err = r.Discard(1) if err != nil { return -1, errors.New(`failed to sync APIC thumbnail picture type`) } n++ // skip a null-delimited string junk, err = r.ReadSlice(0) if err != nil { return -1, errors.New(`failed to sync to thumbnail comment`) } n += len(junk) return n, nil }