-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
streamer/audio: ProbeText now also returns the 'comment' tag
Also implemented a WriteMetadata that writes the given songs metadata to the file given and returns the new file in-memory.
- Loading branch information
Showing
3 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package audio | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
radio "github.com/R-a-dio/valkyrie" | ||
"github.com/R-a-dio/valkyrie/errors" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
// DeleteID3Tags runs `id3v2 --delete-all <filename>` this removes any id3 tags | ||
// in the file. | ||
func DeleteID3Tags(ctx context.Context, filename string) { | ||
exec.CommandContext(ctx, "id3v2", "--delete-all", filename) | ||
} | ||
|
||
// WithMetadata puts the metadata of song into the file given by filename and returns | ||
// it as an in-memory file. | ||
func WriteMetadata(ctx context.Context, f afero.File, song radio.Song) (*MemoryBuffer, error) { | ||
const op errors.Op = "streamer/audio.WriteMetadata" | ||
|
||
args := []string{ | ||
"-hide_banner", | ||
"-loglevel", "error", | ||
"-i", "-", | ||
"-c:a", "copy", // copy audio stream | ||
"-id3v2_version", "3", // windows apparently doesn't support v2.4 | ||
} | ||
if song.Title != "" { | ||
args = append(args, "-metadata", "title="+song.Title) | ||
} | ||
if song.Artist != "" { | ||
args = append(args, "-metadata", "artist="+song.Artist) | ||
} | ||
if song.Album != "" { | ||
args = append(args, "-metadata", "album="+song.Album) | ||
} | ||
if song.Tags != "" { | ||
args = append(args, "-metadata", "comment="+song.Tags) | ||
} | ||
|
||
switch strings.ToLower(filepath.Ext(f.Name())) { | ||
case ".flac": | ||
args = append(args, "-f", "flac") | ||
case ".mp3": | ||
args = append(args, "-f", "mp3") | ||
case ".ogg": | ||
args = append(args, "-f", "ogg") | ||
default: | ||
return nil, errors.E(op, errors.InvalidArgument) | ||
} | ||
args = append(args, "-") | ||
|
||
ff, err := newFFmpegCmd("metadata-"+song.TrackID.String(), args) | ||
if err != nil { | ||
return nil, errors.E(op, err) | ||
} | ||
ff.Cmd.Stdin = f | ||
|
||
out, err := ff.Run() | ||
if err != nil { | ||
return nil, errors.E(op, err) | ||
} | ||
// seek the output to the start | ||
_, _ = out.Seek(0, io.SeekStart) | ||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package audio | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
radio "github.com/R-a-dio/valkyrie" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWriteMetadata(t *testing.T) { | ||
fsys := afero.NewOsFs() | ||
f, err := fsys.Open("testdata/MP3_2MG.mp3") | ||
require.NoError(t, err) | ||
|
||
song := radio.Song{ | ||
DatabaseTrack: &radio.DatabaseTrack{ | ||
Title: "test", | ||
Artist: "some kind of artist", | ||
Album: "a kind of album", | ||
Tags: "test effective very", | ||
}, | ||
} | ||
|
||
out, err := WriteMetadata(context.Background(), f, song) | ||
require.NoError(t, err) | ||
|
||
// probe the output file | ||
info, err := probeText(context.Background(), out.Memfd.File) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, song.Title, info.Title) | ||
assert.Equal(t, song.Artist, info.Artist) | ||
assert.Equal(t, song.Album, info.Album) | ||
assert.Equal(t, song.Tags, info.Comment) | ||
} |