Skip to content

Commit

Permalink
First commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
n10v committed May 15, 2016
0 parents commit 5aa0629
Show file tree
Hide file tree
Showing 19 changed files with 1,081 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 Albert Nigmatzianov

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.
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# ID3V2

## IMHO
I think **ID3v2** is a very overwhelmed standard: it does **more than it really should do**. There are a lot of aspects, which developer should take into consideration. And that's why it's pretty complicated to write a **good library**. So if you have some thoughts about writing a **new simply standard** for providing information for digital music tracks, just write me. I think, it's a good time to write an appropriate standard for it 😉

## Information
**Stable and fast ID3v2 Library written in Go**

This library can only set and write tags, but can't read them. So if you only want to set tags, it fits you. And if there is a tag of version 3 or 4, this library will just delete it.

What it **can** do:
* Set artist, album, year, genre and **attached pictures** (e.g. album covers) and write all to file
* Work with only one encoding (UTF-8)

What it **can't** do:
* Parse tags
* Work with extended header, flags, padding
* Work with encodings, except UTF-8

**If you want some functionality, that library can't do, just write an issue. I will implement it as fast as I can**

**And of course, pull requests are welcome!**

## Benchmarks

Set title, artist, album, year, genre and 50KB picture to 4,5MB MP3:
```
Benchmark-4 100 10209383 ns/op 383363 B/op 77 allocs/op
```

## Installation
$ go get -u github.com/bogem/id3v2

## Usage
#### Example:
```go
package main

import (
"github.com/bogem/id3v2"
"log"
)

func main() {
tag, err := id3v2.Open("file.mp3")
if err != nil {
fmt.Println("Error while opening mp3 file: ", err)
}

tag.SetArtist("Artist")
tag.SetTitle("Title")
tag.SetYear("2016")
...


if err = tag.Close(); err != nil {
fmt.Println("Error while closing a tag: ", err)
}
}

```

#### Available functions for setting text frames:
```go
tag.SetTitle(string)
tag.SetArtist(string)
tag.SetAlbum(string)
tag.SetYear(string)
tag.SetGenre(string)
```

#### Setting a picture

```go
package main

import (
"github.com/bogem/id3v2"
"log"
)

func main() {
tag, err := id3v2.Open("file.mp3")
if err != nil {
log.Fatal("Error while opening mp3 file: ", err)
}

pic := id3v2.NewAttachedPicture()
pic.SetMimeType("image/jpeg")
pic.SetDescription("Cover")
pic.SetPictureType("Cover (front)")

artwork, err := os.Open("artwork.jpg")
if err != nil {
log.Fatal("Error while opening an artwork file: ", err)
}
defer artwork.Close()

if err = pic.SetPictureFromFile(artwork); err != nil {
log.Fatal("Error while setting a picture: ", err)
}

tag.SetAttachedPicture(pic)

if err = tag.Close(); err != nil {
log.Fatal("Error while closing a tag: ", err)
}
}

```

## TODO

* Parse tags
* Work with other encodings
* Work with extended header, flags, padding ***(Does somebody really use it?)***

## License
MIT
Binary file added artwork.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package id3v2

import (
"os"
"testing"
)

func Benchmark(b *testing.B) {
for n := 0; n < b.N; n++ {
t, err := Open("test.mp3")
if err != nil {
b.Error("Error while opening mp3 file: ", err)
}
t.SetTitle("Title")
t.SetArtist("Artist")
t.SetAlbum("Album")
t.SetYear("2016")
t.SetGenre("Genre")

pic := NewAttachedPicture()
pic.SetMimeType("image/jpeg")
pic.SetDescription("Cover")
pic.SetPictureType("Cover (front)")

artwork, err := os.Open("artwork.jpg")
if err != nil {
b.Error("Error while opening an artwork file: ", err)
}

if err = pic.SetPictureFromFile(artwork); err != nil {
b.Error("Error while setting a picture to picture frame: ", err)
}
t.SetAttachedPicture(pic)
if err = t.Close(); err != nil {
b.Error("Error while closing a tag: ", err)
}
if err = artwork.Close(); err != nil {
b.Error("Error while closing an artwork file: ", err)
}
}
}
72 changes: 72 additions & 0 deletions frame/apic_sequence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package frame

import (
"bytes"
"errors"
)

type APICSequencer interface {
Framer

AddPicture(PictureFramer)
Picture(picType string) (PictureFramer, error)
}

// APICSequence stores several APICs and implements interface Framer.
// Key for APICSequnce is a key for PictureType array,
// so there is only one picture with the same picture type
type APICSequence struct {
sequence map[int]PictureFramer
id string
}

func NewAPICSequence() *APICSequence {
return &APICSequence{
sequence: make(map[int]PictureFramer),
}
}

func (as APICSequence) Form() ([]byte, error) {
var b bytes.Buffer
for _, pf := range as.sequence {
if frame, err := pf.Form(); err != nil {
return nil, err
} else {
b.Write(frame)
}
}
return b.Bytes(), nil
}

func (as APICSequence) ID() string {
return as.id
}

func (as *APICSequence) SetID(id string) {
as.id = id
}

func (as APICSequence) Size() (size uint32) {
for _, pf := range as.sequence {
size += pf.Size()
}
return
}

func (as *APICSequence) AddPicture(pic PictureFramer) {
for k, v := range PictureTypes {
if v == pic.PictureType() {
as.sequence[k] = pic
break
}
}
}

func (as APICSequence) Picture(picType string) (PictureFramer, error) {
for k, v := range PictureTypes {
if v == picType {
return as.sequence[k], nil
}
}
return &PictureFrame{}, errors.New("Unsupported picture type")
}
37 changes: 37 additions & 0 deletions frame/frame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package frame

import (
"bytes"
"github.com/bogem/id3v2/util"
)

const (
FrameHeaderSize = 10
IDSize = 4
BytesInFrameSize = 4
)

type Framer interface {
Form() ([]byte, error)
ID() string
SetID(string)
Size() uint32
}

type FrameHeader struct {
ID string
FrameSize uint32
}

func FormFrameHeader(f Framer) ([]byte, error) {
var b bytes.Buffer
b.WriteString(f.ID())
if size, err := util.FormSize(f.Size()); err != nil {
return nil, err
} else {
b.Write(size)
}
b.WriteByte(0)
b.WriteByte(0)
return b.Bytes(), nil
}

0 comments on commit 5aa0629

Please sign in to comment.