Skip to content

🎧 Your Personal Streaming Service

feat: store duration as float, to cater for milliseconds

Deluanfc14e346b98c
diff --git a/db/migration/20200220143731_change_duration_to_float.go b/db/migration/20200220143731_change_duration_to_float.go
new file mode 100644
index 000000000..085fcd408
--- /dev/null
+++ b/db/migration/20200220143731_change_duration_to_float.go
@@ -0,0 +1,129 @@
+package migration
+
+import (
+ "database/sql"
+ "github.com/deluan/navidrome/log"
+ "github.com/pressly/goose"
+)
+
+func init() {
+ goose.AddMigration(Up20200220143731, Down20200220143731)
+}
+
+func Up20200220143731(tx *sql.Tx) error {
+ log.Warn("This version will force the next scan to be a requires a full rescan!")
+ _, err := tx.Exec(`
+create table media_file_dg_tmp
+(
+ id varchar(255) not null
+ primary key,
+ path varchar(255) default '' not null,
+ title varchar(255) default '' not null,
+ album varchar(255) default '' not null,
+ artist varchar(255) default '' not null,
+ artist_id varchar(255) default '' not null,
+ album_artist varchar(255) default '' not null,
+ album_id varchar(255) default '' not null,
+ has_cover_art bool default FALSE not null,
+ track_number integer default 0 not null,
+ disc_number integer default 0 not null,
+ year integer default 0 not null,
+ size integer default 0 not null,
+ suffix varchar(255) default '' not null,
+ duration real default 0 not null,
+ bit_rate integer default 0 not null,
+ genre varchar(255) default '' not null,
+ compilation bool default FALSE not null,
+ created_at datetime,
+ updated_at datetime
+);
+
+insert into media_file_dg_tmp(id, path, title, album, artist, artist_id, album_artist, album_id, has_cover_art, track_number, disc_number, year, size, suffix, duration, bit_rate, genre, compilation, created_at, updated_at) select id, path, title, album, artist, artist_id, album_artist, album_id, has_cover_art, track_number, disc_number, year, size, suffix, duration, bit_rate, genre, compilation, created_at, updated_at from media_file;
+
+drop table media_file;
+
+alter table media_file_dg_tmp rename to media_file;
+
+create index media_file_album_id
+ on media_file (album_id);
+
+create index media_file_genre
+ on media_file (genre);
+
+create index media_file_path
+ on media_file (path);
+
+create index media_file_title
+ on media_file (title);
+
+create table album_dg_tmp
+(
+ id varchar(255) not null
+ primary key,
+ name varchar(255) default '' not null,
+ artist_id varchar(255) default '' not null,
+ cover_art_path varchar(255) default '' not null,
+ cover_art_id varchar(255) default '' not null,
+ artist varchar(255) default '' not null,
+ album_artist varchar(255) default '' not null,
+ year integer default 0 not null,
+ compilation bool default FALSE not null,
+ song_count integer default 0 not null,
+ duration real default 0 not null,
+ genre varchar(255) default '' not null,
+ created_at datetime,
+ updated_at datetime
+);
+
+insert into album_dg_tmp(id, name, artist_id, cover_art_path, cover_art_id, artist, album_artist, year, compilation, song_count, duration, genre, created_at, updated_at) select id, name, artist_id, cover_art_path, cover_art_id, artist, album_artist, year, compilation, song_count, duration, genre, created_at, updated_at from album;
+
+drop table album;
+
+alter table album_dg_tmp rename to album;
+
+create index album_artist
+ on album (artist);
+
+create index album_artist_id
+ on album (artist_id);
+
+create index album_genre
+ on album (genre);
+
+create index album_name
+ on album (name);
+
+create index album_year
+ on album (year);
+
+create table playlist_dg_tmp
+(
+ id varchar(255) not null
+ primary key,
+ name varchar(255) default '' not null,
+ comment varchar(255) default '' not null,
+ duration real default 0 not null,
+ owner varchar(255) default '' not null,
+ public bool default FALSE not null,
+ tracks text not null
+);
+
+insert into playlist_dg_tmp(id, name, comment, duration, owner, public, tracks) select id, name, comment, duration, owner, public, tracks from playlist;
+
+drop table playlist;
+
+alter table playlist_dg_tmp rename to playlist;
+
+create index playlist_name
+ on playlist (name);
+
+-- Force a full rescan
+delete from property where id like 'LastScan%';
+update media_file set updated_at = '0001-01-01';
+`)
+ return err
+}
+
+func Down20200220143731(tx *sql.Tx) error {
+ return nil
+}
diff --git a/engine/browser.go b/engine/browser.go
index 116d4e1ac..a929fb1b6 100644
--- a/engine/browser.go
+++ b/engine/browser.go
@@ -159,7 +159,7 @@ func (b *browser) buildAlbumDir(al *model.Album, tracks model.MediaFiles) *Direc
Artist: al.Artist,
ArtistId: al.ArtistID,
SongCount: al.SongCount,
- Duration: al.Duration,
+ Duration: int(al.Duration),
Created: al.CreatedAt,
Year: al.Year,
Genre: al.Genre,
diff --git a/engine/common.go b/engine/common.go
index a988548e8..257f37475 100644
--- a/engine/common.go
+++ b/engine/common.go
@@ -69,7 +69,7 @@ func FromAlbum(al *model.Album) Entry {
e.Created = al.CreatedAt
e.AlbumId = al.ID
e.ArtistId = al.ArtistID
- e.Duration = al.Duration
+ e.Duration = int(al.Duration)
e.SongCount = al.SongCount
e.Starred = al.StarredAt
e.PlayCount = int32(al.PlayCount)
@@ -88,7 +88,7 @@ func FromMediaFile(mf *model.MediaFile) Entry {
e.Artist = mf.Artist
e.Genre = mf.Genre
e.Track = mf.TrackNumber
- e.Duration = mf.Duration
+ e.Duration = int(mf.Duration)
e.Size = mf.Size
e.Suffix = mf.Suffix
e.BitRate = mf.BitRate
diff --git a/engine/media_streamer.go b/engine/media_streamer.go
index ee4504e3e..654556630 100644
--- a/engine/media_streamer.go
+++ b/engine/media_streamer.go
@@ -179,7 +179,7 @@ type streamHandlerFileInfo struct {
}
func (f *streamHandlerFileInfo) Name() string { return f.mf.Title }
-func (f *streamHandlerFileInfo) Size() int64 { return int64((f.mf.Duration)*f.bitRate*1000) / 8 }
+func (f *streamHandlerFileInfo) Size() int64 { return int64(f.mf.Duration*float32(f.bitRate*1000)) / 8 }
func (f *streamHandlerFileInfo) Mode() os.FileMode { return os.FileMode(0777) }
func (f *streamHandlerFileInfo) ModTime() time.Time { return f.mf.UpdatedAt }
func (f *streamHandlerFileInfo) IsDir() bool { return false }
diff --git a/engine/playlists.go b/engine/playlists.go
index 4235467d6..7dd1c6306 100644
--- a/engine/playlists.go
+++ b/engine/playlists.go
@@ -127,7 +127,7 @@ func (p *playlists) Get(ctx context.Context, id string) (*PlaylistInfo, error) {
Id: pl.ID,
Name: pl.Name,
SongCount: len(pl.Tracks),
- Duration: pl.Duration,
+ Duration: int(pl.Duration),
Public: pl.Public,
Owner: pl.Owner,
Comment: pl.Comment,
diff --git a/model/album.go b/model/album.go
index fd4027b0e..a8dfca2ad 100644
--- a/model/album.go
+++ b/model/album.go
@@ -13,7 +13,7 @@ type Album struct {
Year int `json:"year"`
Compilation bool `json:"compilation"`
SongCount int `json:"songCount"`
- Duration int `json:"duration"`
+ Duration float32 `json:"duration"`
Genre string `json:"genre"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
diff --git a/model/mediafile.go b/model/mediafile.go
index 29fb05844..244b5c115 100644
--- a/model/mediafile.go
+++ b/model/mediafile.go
@@ -20,7 +20,7 @@ type MediaFile struct {
Year int `json:"year"`
Size int `json:"size"`
Suffix string `json:"suffix"`
- Duration int `json:"duration"`
+ Duration float32 `json:"duration"`
BitRate int `json:"bitRate"`
Genre string `json:"genre"`
Compilation bool `json:"compilation"`
diff --git a/model/playlist.go b/model/playlist.go
index bec2d89e9..575cd63f5 100644
--- a/model/playlist.go
+++ b/model/playlist.go
@@ -4,7 +4,7 @@ type Playlist struct {
ID string
Name string
Comment string
- Duration int
+ Duration float32
Owner string
Public bool
Tracks MediaFiles
diff --git a/persistence/playlist_repository.go b/persistence/playlist_repository.go
index 3f32042e3..bbf4c8154 100644
--- a/persistence/playlist_repository.go
+++ b/persistence/playlist_repository.go
@@ -13,7 +13,7 @@ type playlist struct {
ID string `orm:"column(id)"`
Name string
Comment string
- Duration int
+ Duration float32
Owner string
Public bool
Tracks string
diff --git a/scanner/metadata_ffmpeg.go b/scanner/metadata_ffmpeg.go
index 8bf8480b0..e922a3ab8 100644
--- a/scanner/metadata_ffmpeg.go
+++ b/scanner/metadata_ffmpeg.go
@@ -36,7 +36,7 @@ func (m *Metadata) DiscNumber() (int, int) { return m.parseTuple("tpa", "di
func (m *Metadata) HasPicture() bool { return m.getTag("has_picture") == "true" }
func (m *Metadata) Comment() string { return m.getTag("comment") }
func (m *Metadata) Compilation() bool { return m.parseBool("compilation") }
-func (m *Metadata) Duration() int { return m.parseDuration("duration") }
+func (m *Metadata) Duration() float32 { return m.parseDuration("duration") }
func (m *Metadata) BitRate() int { return m.parseInt("bitrate") }
func (m *Metadata) ModificationTime() time.Time { return m.fileInfo.ModTime() }
func (m *Metadata) FilePath() string { return m.filePath }
@@ -257,13 +257,13 @@ func (m *Metadata) parseBool(tagName string) bool {
var zeroTime = time.Date(0000, time.January, 1, 0, 0, 0, 0, time.UTC)
-func (m *Metadata) parseDuration(tagName string) int {
+func (m *Metadata) parseDuration(tagName string) float32 {
if v, ok := m.tags[tagName]; ok {
d, err := time.Parse("15:04:05", v)
if err != nil {
return 0
}
- return int(d.Sub(zeroTime).Seconds())
+ return float32(d.Sub(zeroTime).Seconds())
}
return 0
}
diff --git a/scanner/metadata_test.go b/scanner/metadata_test.go
index 35bfec6b1..5e294eb4a 100644
--- a/scanner/metadata_test.go
+++ b/scanner/metadata_test.go
@@ -98,6 +98,14 @@ Input #0, mp3, from '/Users/deluan/Music/iTunes/iTunes Media/Music/Compilations/
Expect(md.Compilation()).To(BeTrue())
})
+ It("parses duration with milliseconds", func() {
+ const output = `
+Input #0, mp3, from '/Users/deluan/Music/iTunes/iTunes Media/Music/Compilations/Putumayo Presents Blues Lounge/09 Pablo's Blues.mp3':
+ Duration: 00:05:02.63, start: 0.000000, bitrate: 140 kb/s`
+ md, _ := extractMetadata("tests/fixtures/test.mp3", output)
+ Expect(md.Duration()).To(BeNumerically("~", 302.63, 0.001))
+ })
+
It("parses stream level tags", func() {
const output = `
Input #0, ogg, from './01-02 Drive (Teku).opus':
diff --git a/server/subsonic/playlists.go b/server/subsonic/playlists.go
index 0f517d3ee..e64f97a76 100644
--- a/server/subsonic/playlists.go
+++ b/server/subsonic/playlists.go
@@ -32,7 +32,7 @@ func (c *PlaylistsController) GetPlaylists(w http.ResponseWriter, r *http.Reques
playlists[i].Name = p.Name
playlists[i].Comment = p.Comment
playlists[i].SongCount = len(p.Tracks)
- playlists[i].Duration = p.Duration
+ playlists[i].Duration = int(p.Duration)
playlists[i].Owner = p.Owner
playlists[i].Public = p.Public
}