Add audio bitrate and video is default in info

This commit is contained in:
Zoe Roux 2024-08-06 14:53:55 +02:00
parent c0f8f22e6a
commit 7d3c73a1e9
3 changed files with 25 additions and 18 deletions

View File

@ -25,6 +25,7 @@ create table videos(
width integer not null, width integer not null,
height integer not null, height integer not null,
bitrate integer not null, bitrate integer not null,
is_default boolean not null,
keyframes double precision[], keyframes double precision[],
@ -38,6 +39,7 @@ create table audios(
language varchar(256), language varchar(256),
codec varchar(256) not null, codec varchar(256) not null,
mime_codec varchar(256), mime_codec varchar(256),
bitrate integer not null,
is_default boolean not null, is_default boolean not null,
keyframes double precision[], keyframes double precision[],

View File

@ -18,10 +18,10 @@ import (
const InfoVersion = 1 const InfoVersion = 1
type Versions struct { type Versions struct {
Info int32 Info int32 `json:"info"`
Extract int32 Extract int32 `json:"extract"`
Thumbs int32 Thumbs int32 `json:"thumbs"`
Keyframes int32 Keyframes int32 `json:"keyframes"`
} }
type MediaInfo struct { type MediaInfo struct {
@ -42,9 +42,6 @@ type MediaInfo struct {
/// Version of the metadata. This can be used to invalidate older metadata from db if the extraction code has changed. /// Version of the metadata. This can be used to invalidate older metadata from db if the extraction code has changed.
Versions Versions `json:"versions"` Versions Versions `json:"versions"`
// TODO: remove this
Video *Video
/// The list of videos if there are multiples. /// The list of videos if there are multiples.
Videos []Video `json:"videos"` Videos []Video `json:"videos"`
/// The list of audio tracks. /// The list of audio tracks.
@ -76,6 +73,8 @@ type Video struct {
Height uint32 `json:"height"` Height uint32 `json:"height"`
/// The average bitrate of the video in bytes/s /// The average bitrate of the video in bytes/s
Bitrate uint32 `json:"bitrate"` Bitrate uint32 `json:"bitrate"`
/// Is this stream the default one of it's type?
IsDefault bool `json:"isDefault"`
/// Keyframes of this video /// Keyframes of this video
Keyframes *Keyframe `json:"-"` Keyframes *Keyframe `json:"-"`
@ -92,6 +91,8 @@ type Audio struct {
Codec string `json:"codec"` Codec string `json:"codec"`
/// The codec of this stream (defined as the RFC 6381). /// The codec of this stream (defined as the RFC 6381).
MimeCodec *string `json:"mimeCodec"` MimeCodec *string `json:"mimeCodec"`
/// The average bitrate of the audio in bytes/s
Bitrate uint32 `json:"bitrate"`
/// Is this stream the default one of it's type? /// Is this stream the default one of it's type?
IsDefault bool `json:"isDefault"` IsDefault bool `json:"isDefault"`
@ -256,6 +257,7 @@ func RetriveMediaInfo(path string, sha string) (*MediaInfo, error) {
// ffmpeg does not report bitrate in mkv files, fallback to bitrate of the whole container // ffmpeg does not report bitrate in mkv files, fallback to bitrate of the whole container
// (bigger than the result since it contains audio and other videos but better than nothing). // (bigger than the result since it contains audio and other videos but better than nothing).
Bitrate: ParseUint(cmp.Or(stream.BitRate, mi.Format.BitRate)), Bitrate: ParseUint(cmp.Or(stream.BitRate, mi.Format.BitRate)),
IsDefault: stream.Disposition.Default != 0,
} }
}), }),
Audios: MapStream(mi.Streams, ffprobe.StreamAudio, func(stream *ffprobe.Stream, i uint32) Audio { Audios: MapStream(mi.Streams, ffprobe.StreamAudio, func(stream *ffprobe.Stream, i uint32) Audio {
@ -266,6 +268,7 @@ func RetriveMediaInfo(path string, sha string) (*MediaInfo, error) {
Language: NullIfUnd(lang.String()), Language: NullIfUnd(lang.String()),
Codec: stream.CodecName, Codec: stream.CodecName,
MimeCodec: GetMimeCodec(stream), MimeCodec: GetMimeCodec(stream),
Bitrate: ParseUint(cmp.Or(stream.BitRate, mi.Format.BitRate)),
IsDefault: stream.Disposition.Default != 0, IsDefault: stream.Disposition.Default != 0,
} }
}), }),

View File

@ -117,7 +117,7 @@ func (s *MetadataService) getMetadata(path string, sha string) (*MediaInfo, erro
} }
rows, err := s.database.Query( rows, err := s.database.Query(
`select v.idx, v.title, v.language, v.codec, v.mime_codec, v.width, v.height, v.bitrate, v.keyframes `select v.idx, v.title, v.language, v.codec, v.mime_codec, v.width, v.height, v.bitrate, v.is_default, v.keyframes
from videos as v where v.sha=$1`, from videos as v where v.sha=$1`,
sha, sha,
) )
@ -126,7 +126,7 @@ func (s *MetadataService) getMetadata(path string, sha string) (*MediaInfo, erro
} }
for rows.Next() { for rows.Next() {
var v Video var v Video
err := rows.Scan(&v.Index, &v.Title, &v.Language, &v.Codec, &v.MimeCodec, &v.Width, &v.Height, &v.Bitrate, &v.Keyframes) err := rows.Scan(&v.Index, &v.Title, &v.Language, &v.Codec, &v.MimeCodec, &v.Width, &v.Height, &v.Bitrate, &v.IsDefault, &v.Keyframes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -135,7 +135,7 @@ func (s *MetadataService) getMetadata(path string, sha string) (*MediaInfo, erro
} }
rows, err = s.database.Query( rows, err = s.database.Query(
`select a.idx, a.title, a.language, a.codec, a.mime_codec, a.is_default, a.keyframes `select a.idx, a.title, a.language, a.codec, a.mime_codec, a.bitrate, a.is_default, a.keyframes
from audios as a where a.sha=$1`, from audios as a where a.sha=$1`,
sha, sha,
) )
@ -144,7 +144,7 @@ func (s *MetadataService) getMetadata(path string, sha string) (*MediaInfo, erro
} }
for rows.Next() { for rows.Next() {
var a Audio var a Audio
err := rows.Scan(&a.Index, &a.Title, &a.Language, &a.Codec, &a.MimeCodec, &a.IsDefault, &a.Keyframes) err := rows.Scan(&a.Index, &a.Title, &a.Language, &a.Codec, &a.MimeCodec, &a.Bitrate, &a.IsDefault, &a.Keyframes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -232,8 +232,8 @@ func (s *MetadataService) storeFreshMetadata(path string, sha string) (*MediaInf
).Scan(&ret.Versions.Extract, &ret.Versions.Thumbs, &ret.Versions.Keyframes) ).Scan(&ret.Versions.Extract, &ret.Versions.Thumbs, &ret.Versions.Keyframes)
for _, v := range ret.Videos { for _, v := range ret.Videos {
tx.Exec(` tx.Exec(`
insert into videos(sha, idx, title, language, codec, mime_codec, width, height, bitrate) insert into videos(sha, idx, title, language, codec, mime_codec, width, height, is_default, bitrate)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
on conflict (sha, idx) do update set on conflict (sha, idx) do update set
sha = excluded.sha, sha = excluded.sha,
idx = excluded.idx, idx = excluded.idx,
@ -243,15 +243,16 @@ func (s *MetadataService) storeFreshMetadata(path string, sha string) (*MediaInf
mime_codec = excluded.mime_codec, mime_codec = excluded.mime_codec,
width = excluded.width, width = excluded.width,
height = excluded.height, height = excluded.height,
is_default = excluded.is_default,
bitrate = excluded.bitrate bitrate = excluded.bitrate
`, `,
ret.Sha, v.Index, v.Title, v.Language, v.Codec, v.MimeCodec, v.Width, v.Height, v.Bitrate, ret.Sha, v.Index, v.Title, v.Language, v.Codec, v.MimeCodec, v.Width, v.Height, v.IsDefault, v.Bitrate,
) )
} }
for _, a := range ret.Audios { for _, a := range ret.Audios {
tx.Exec(` tx.Exec(`
insert into audios(sha, idx, title, language, codec, mime_codec, is_default) insert into audios(sha, idx, title, language, codec, mime_codec, is_default, bitrate)
values ($1, $2, $3, $4, $5, $6, $7) values ($1, $2, $3, $4, $5, $6, $7, $8)
on conflict (sha, idx) do update set on conflict (sha, idx) do update set
sha = excluded.sha, sha = excluded.sha,
idx = excluded.idx, idx = excluded.idx,
@ -259,9 +260,10 @@ func (s *MetadataService) storeFreshMetadata(path string, sha string) (*MediaInf
language = excluded.language, language = excluded.language,
codec = excluded.codec, codec = excluded.codec,
mime_codec = excluded.mime_codec, mime_codec = excluded.mime_codec,
is_default = excluded.is_default is_default = excluded.is_default,
bitrate = excluded.bitrate
`, `,
ret.Sha, a.Index, a.Title, a.Language, a.Codec, a.MimeCodec, a.IsDefault, ret.Sha, a.Index, a.Title, a.Language, a.Codec, a.MimeCodec, a.IsDefault, a.Bitrate,
) )
} }
for _, s := range ret.Subtitles { for _, s := range ret.Subtitles {