### Install go-taglib Source: https://context7.com/sentriz/go-taglib/llms.txt Installs the latest version of the go-taglib library using the go get command. ```bash go get go.senan.xyz/taglib@latest ``` -------------------------------- ### Concurrent Audio File Processing with Go TagLib Source: https://context7.com/sentriz/go-taglib/llms.txt Shows how to safely process multiple audio files concurrently using goroutines and a wait group. This example reads tags from multiple FLAC files in parallel and reports their titles. ```go package main import ( "fmt" "log" "path/filepath" "sync" "go.senan.xyz/taglib" ) func main() { files, _ := filepath.Glob("music/*.flac") var wg sync.WaitGroup results := make(chan string, len(files)) for _, file := range files { wg.Add(1) go func(path string) { defer wg.Done() tags, err := taglib.ReadTags(path) if err != nil { results <- fmt.Sprintf("%s: error - %v", path, err) return } title := "Unknown" if t := tags[taglib.Title]; len(t) > 0 { title = t[0] } results <- fmt.Sprintf("%s: %s", filepath.Base(path), title) }(file) } go func() { wg.Wait() close(results) }() for result := range results { fmt.Println(result) } } ``` -------------------------------- ### Error Handling with Go TagLib Source: https://context7.com/sentriz/go-taglib/llms.txt Demonstrates how to handle specific error types provided by the go-taglib library, such as invalid files or write failures. It uses `errors.Is` for checking specific error conditions and `log.Fatal` for unrecoverable errors. ```go package main import ( "errors" "fmt" "log" "go.senan.xyz/taglib" ) func main() { // Handle invalid/corrupted files tags, err := taglib.ReadTags("corrupted.mp3") if errors.Is(err, taglib.ErrInvalidFile) { fmt.Println("File is not a valid audio file or is corrupted") } else if err != nil { log.Fatal(err) } _ = tags // Handle write failures err = taglib.WriteTags("readonly.flac", map[string][]string{ taglib.Title: {"New Title"}, }, 0) if errors.Is(err, taglib.ErrSavingFile) { fmt.Println("Cannot save file (read-only or permission denied)") } else if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Write Audio Metadata Tags in Go Source: https://context7.com/sentriz/go-taglib/llms.txt Writes metadata tags to an audio file using go-taglib. Supports merging tags, clearing existing tags, removing specific tags, or replacing all tags. Custom tag keys are also supported, with format-dependent behavior. ```go package main import ( "log" "go.senan.xyz/taglib" ) func main() { // Write tags while preserving existing ones (merge mode) err := taglib.WriteTags("music/song.flac", map[string][]string{ taglib.Title: {"My Song Title"}, taglib.Artist: {"Brian Eno", "David Byrne"}, // Multi-valued taglib.AlbumArtist: {"Brian Eno & David Byrne"}, taglib.Album: {"My Life in the Bush of Ghosts"}, taglib.TrackNumber: {"1"}, taglib.DiscNumber: {"1"}, taglib.Date: {"1981-02-09"}, taglib.Genre: {"Electronic", "Experimental", "World"}, // MusicBrainz metadata taglib.MusicBrainzAlbumID: {"abc123-def456"}, taglib.MusicBrainzArtistID: {"xyz789-uvw012"}, // Custom tags (format-dependent support) "ALBUMARTIST_CREDIT": {"Brian Eno & David Byrne"}, "REPLAYGAIN_TRACK_GAIN": {"-5.29 dB"}, }, 0) // 0 = merge with existing tags if err != nil { log.Fatal(err) } // Replace all tags (clear existing first) err = taglib.WriteTags("music/song.flac", map[string][]string{ taglib.Title: {"New Title"}, taglib.Artist: {"New Artist"}, }, taglib.Clear) // Clear removes tags not in the new map if err != nil { log.Fatal(err) } // Remove specific tags by setting empty value err = taglib.WriteTags("music/song.flac", map[string][]string{ taglib.Comment: {}, // Removes the COMMENT tag }, 0) if err != nil { log.Fatal(err) } // Clear all tags err = taglib.WriteTags("music/song.flac", nil, taglib.Clear) if err != nil { log.Fatal(err) } } ``` -------------------------------- ### Read Audio Metadata Tags in Go Source: https://context7.com/sentriz/go-taglib/llms.txt Reads all metadata tags from an audio file using go-taglib. It returns a map of tag keys to string slices, supporting multi-valued tags. Standard and MusicBrainz tags can be accessed directly, and all tags can be iterated. ```go package main import ( "fmt" "log" "go.senan.xyz/taglib" ) func main() { tags, err := taglib.ReadTags("music/song.mp3") if err != nil { log.Fatal(err) } // Access standard tags using provided constants fmt.Printf("Title: %s\n", tags[taglib.Title]) // ["My Song"] fmt.Printf("Artist: %v\n", tags[taglib.Artist]) // ["Artist 1", "Artist 2"] fmt.Printf("Album: %s\n", tags[taglib.Album]) // ["My Album"] fmt.Printf("Album Artist: %v\n", tags[taglib.AlbumArtist]) fmt.Printf("Track: %s\n", tags[taglib.TrackNumber]) // ["5"] fmt.Printf("Genre: %v\n", tags[taglib.Genre]) // ["Rock", "Alternative"] fmt.Printf("Date: %s\n", tags[taglib.Date]) // ["2024-01-15"] // Access MusicBrainz IDs fmt.Printf("MusicBrainz Album ID: %s\n", tags[taglib.MusicBrainzAlbumID]) fmt.Printf("MusicBrainz Artist ID: %s\n", tags[taglib.MusicBrainzArtistID]) // Iterate all tags for key, values := range tags { fmt.Printf("%s: %v\n", key, values) } } ``` -------------------------------- ### Standard Tag Constants in Go Source: https://context7.com/sentriz/go-taglib/llms.txt Defines constants for standard tag keys used by the go-taglib library, ensuring consistent naming across different audio formats like MP3, FLAC, and M4A. These constants map to TagLib's property mapping system. ```go package main import "go.senan.xyz/taglib" // Commonly used tag constants: var standardTags = []string{ taglib.Title, // "TITLE" taglib.Artist, // "ARTIST" taglib.Artists, // "ARTISTS" (multi-valued) taglib.Album, // "ALBUM" taglib.AlbumArtist, // "ALBUMARTIST" taglib.TrackNumber, // "TRACKNUMBER" taglib.DiscNumber, // "DISCNUMBER" taglib.Date, // "DATE" taglib.Genre, // "GENRE" taglib.Comment, // "COMMENT" taglib.Composer, // "COMPOSER" taglib.Conductor, // "CONDUCTOR" taglib.Lyrics, // "LYRICS" taglib.BPM, // "BPM" // Sort fields taglib.TitleSort, // "TITLESORT" taglib.ArtistSort, // "ARTISTSORT" taglib.AlbumSort, // "ALBUMSORT" taglib.AlbumArtistSort, // "ALBUMARTISTSORT" // MusicBrainz identifiers taglib.MusicBrainzTrackID, // "MUSICBRAINZ_TRACKID" taglib.MusicBrainzAlbumID, // "MUSICBRAINZ_ALBUMID" taglib.MusicBrainzArtistID, // "MUSICBRAINZ_ARTISTID" taglib.MusicBrainzAlbumArtistID, // "MUSICBRAINZ_ALBUMARTISTID" taglib.MusicBrainzReleaseGroupID, // "MUSICBRAINZ_RELEASEGROUPID" // Additional metadata taglib.Label, // "LABEL" taglib.CatalogNumber, // "CATALOGNUMBER" taglib.Barcode, // "BARCODE" taglib.ISRC, // "ISRC" taglib.Copyright, // "COPYRIGHT" taglib.EncodedBy, // "ENCODEDBY" } ``` -------------------------------- ### Write Audio Metadata Tags in Go Source: https://github.com/sentriz/go-taglib/blob/master/README.md Writes metadata tags to an audio file using the go-taglib library. It accepts a map of tag names to string slice values and optional flags to control the writing behavior (e.g., clearing existing tags). This function allows modification of audio file metadata. ```Go package main import ( "fmt" "go.senan.xyz/taglib" ) func main() { err := taglib.WriteTags("path/to/audiofile.mp3", map[string][]string{ // Multi-valued tags allowed taglib.AlbumArtist: {"David Byrne", "Brian Eno"}, taglib.Album: {"My Life in the Bush of Ghosts"}, taglib.TrackNumber: {"1"}, // Non-standard allowed too "ALBUMARTIST_CREDIT": {"Brian Eno & David Byrne"}, }, 0) // check(err) } // Options for writing // taglib.Clear can be used to remove all existing tags not present in the new map. // Example: taglib.WriteTags(path, tags, taglib.Clear) // Example: taglib.WriteTags(path, tags, 0) ``` -------------------------------- ### Read Audio Properties with Go Taglib Source: https://context7.com/sentriz/go-taglib/llms.txt Retrieves audio properties such as duration, bitrate, sample rate, channels, and embedded image metadata without loading actual image data. This function is useful for quickly inspecting audio file characteristics. ```go package main import ( "fmt" "log" "go.senan.xyz/taglib" ) func main() { properties, err := taglib.ReadProperties("music/song.flac") if err != nil { log.Fatal(err) } // Audio duration as time.Duration fmt.Printf("Duration: %v\n", properties.Length) // 3m45s fmt.Printf("Duration (seconds): %.0f\n", properties.Length.Seconds()) // Audio quality information fmt.Printf("Bitrate: %d kbps\n", properties.Bitrate) // 320 kbps fmt.Printf("Sample Rate: %d Hz\n", properties.SampleRate) // 44100 Hz fmt.Printf("Channels: %d\n", properties.Channels) // 2 (stereo) // Embedded image metadata (without loading image bytes) fmt.Printf("Number of images: %d\n", len(properties.Images)) for i, img := range properties.Images { fmt.Printf("Image %d:\n", i) fmt.Printf(" Type: %s\n", img.Type) // "Front Cover", "Back Cover", etc. fmt.Printf(" Description: %s\n", img.Description) fmt.Printf(" MIME Type: %s\n", img.MIMEType) // "image/jpeg", "image/png" } } ``` -------------------------------- ### Read Audio File Properties in Go Source: https://github.com/sentriz/go-taglib/blob/master/README.md Retrieves properties of an audio file, such as length, bitrate, sample rate, and channels, using the go-taglib library. It also provides information about embedded images without loading their data. This is useful for analyzing audio file characteristics. ```Go package main import ( "fmt" "go.senan.xyz/taglib" ) func main() { properties, err := taglib.ReadProperties("path/to/audiofile.mp3") // check(err) fmt.Printf("Length: %v\n", properties.Length) fmt.Printf("Bitrate: %d\n", properties.Bitrate) fmt.Printf("SampleRate: %d\n", properties.SampleRate) fmt.Printf("Channels: %d\n", properties.Channels) // Image metadata (without reading actual image data) for i, img := range properties.Images { fmt.Printf("Image %d - Type: %s, Description: %s, MIME type: %s\n", i, img.Type, img.Description, img.MIMEType) } } ``` -------------------------------- ### Write Embedded Image to Audio File in Go Source: https://github.com/sentriz/go-taglib/blob/master/README.md Writes an embedded image to an audio file using go-taglib. It supports writing the image with auto-detected MIME type or with custom options specifying index, picture type, description, and MIME type. This allows for adding or replacing album artwork. ```Go package main import ( "go.senan.xyz/taglib" ) func main() { var imageBytes []byte // Some image data, from somewhere // Write as front cover with auto-detected MIME type err := taglib.WriteImage("path/to/audiofile.mp3", imageBytes) // check(err) // Write with custom options err = taglib.WriteImageOptions( "path/to/audiofile.mp3", imageBytes, 0, // replaces image at index; use higher index to append "Back Cover", // picture type "Back artwork", // description "image/jpeg", // MIME type ) // check(err) } ``` -------------------------------- ### Adding taglib Subdirectory and Include Directories Source: https://github.com/sentriz/go-taglib/blob/master/CMakeLists.txt Includes the 'taglib' subdirectory into the build and specifies include directories for the taglib and taglib/toolkit headers. This is essential for compiling the project's source files. ```cmake add_subdirectory( taglib ) include_directories( taglib/taglib taglib/taglib/toolkit ) ``` -------------------------------- ### Extract Embedded Images with Go Taglib Source: https://context7.com/sentriz/go-taglib/llms.txt Extracts embedded album artwork from audio files as raw bytes. The extracted bytes can then be decoded using Go's standard image package. This function is useful for displaying album art or processing it further. ```go package main import ( "bytes" "fmt" "image" "log" "os" _ "image/gif" _ "image/jpeg" _ "image/png" "go.senan.xyz/taglib" ) func main() { // Read the first (front cover) image imageBytes, err := taglib.ReadImage("music/song.mp3") if err != nil { log.Fatal(err) } if imageBytes == nil { fmt.Println("No embedded image found") return } // Decode and inspect the image img, format, err := image.Decode(bytes.NewReader(imageBytes)) if err != nil { log.Fatal(err) } bounds := img.Bounds() fmt.Printf("Format: %s\n", format) // "jpeg", "png", etc. fmt.Printf("Width: %d px\n", bounds.Dx()) // 700 fmt.Printf("Height: %d px\n", bounds.Dy()) // 700 // Save to file os.WriteFile("cover.jpg", imageBytes, 0644) // Read a specific image by index (e.g., back cover at index 1) backCover, err := taglib.ReadImageOptions("music/song.mp3", 1) if err != nil { log.Fatal(err) } if backCover != nil { os.WriteFile("back_cover.jpg", backCover, 0644) } } ``` -------------------------------- ### Read Embedded Image from Audio File in Go Source: https://github.com/sentriz/go-taglib/blob/master/README.md Reads the first embedded image from an audio file using go-taglib. It returns the image data as a byte slice. If the file contains no image, it returns nil. The function supports decoding various image formats. ```Go package main import ( "bytes" "fmt" "image" _ "image/gif" _ "image/jpeg" _ "image/png" "go.senan.xyz/taglib" ) func main() { // Read first image (index 0) imageBytes, err := taglib.ReadImage("path/to/audiofile.mp3") // check(err) if imageBytes == nil { fmt.Printf("File contains no image") return } img, format, err := image.Decode(bytes.NewReader(imageBytes)) // check(err) fmt.Printf("format: %q\n", format) bounds := img.Bounds() fmt.Printf("width: %d\n", bounds.Dx()) fmt.Printf("height: %d\n", bounds.Dy()) // Read a specific image by index backCover, err := taglib.ReadImageOptions("path/to/audiofile.mp3", 1) // check(err) } ``` -------------------------------- ### Read Audio Metadata Tags in Go Source: https://github.com/sentriz/go-taglib/blob/master/README.md Reads metadata tags from an audio file using the go-taglib library. It returns a map of tag names to their string slice values and an error if the operation fails. This function is useful for inspecting audio file information. ```Go package main import ( "fmt" "go.senan.xyz/taglib" ) func main() { tags, err := taglib.ReadTags("path/to/audiofile.mp3") // check(err) fmt.Printf("tags: %v\n", tags) // map[string][]string fmt.Printf("AlbumArtist: %q\n", tags[taglib.AlbumArtist]) fmt.Printf("Album: %q\n", tags[taglib.Album]) fmt.Printf("TrackNumber: %q\n", tags[taglib.TrackNumber]) } ``` -------------------------------- ### Configuring WASM Executable for taglib Source: https://github.com/sentriz/go-taglib/blob/master/CMakeLists.txt Defines the 'taglib' executable, links it with the 'tag' library, and sets WASM-specific properties. This includes setting the executable suffix to '.wasm', specifying WASM32-WASI target with optimization and debugging flags, and linker options for WASM execution model. ```cmake add_executable(taglib taglib.cpp) set_target_properties(taglib PROPERTIES SUFFIX ".wasm") target_compile_options(taglib PRIVATE --target=wasm32-wasi -g0 -O2) target_link_options(taglib PRIVATE -Wl,--allow-undefined -mexec-model=reactor) target_link_libraries(taglib PRIVATE tag) ``` -------------------------------- ### CMake Build Configuration for taglib_wasm Source: https://github.com/sentriz/go-taglib/blob/master/CMakeLists.txt Configures the CMake build for the taglib_wasm project. It sets the minimum required CMake version, project name, build type to Release, enables export of compile commands, and disables ZLIB and testing. It also configures shared library behavior. ```cmake cmake_minimum_required(VERSION 3.5.0 FATAL_ERROR) project(taglib_wasm) set(CMAKE_BUILD_TYPE Release) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(WITH_ZLIB OFF) set(BUILD_SHARED_LIBS OFF) set(BUILD_TESTING OFF) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.