utils: Move duration formatting to shared function

We'll be using this a few different places.
This commit is contained in:
Justin Maggard 2017-05-17 11:56:44 -07:00
parent 49aa42d893
commit 2b3bdb8373
3 changed files with 29 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/* MiniDLNA media server /* MiniDLNA media server
* Copyright (C) 2008-2009 Justin Maggard * Copyright (C) 2008-2017 Justin Maggard
* *
* This file is part of MiniDLNA. * This file is part of MiniDLNA.
* *
@ -363,11 +363,7 @@ GetAudioMetadata(const char *path, char *name)
m.dlna_pn = strdup(song.dlna_pn); m.dlna_pn = strdup(song.dlna_pn);
if( song.year ) if( song.year )
xasprintf(&m.date, "%04d-01-01", song.year); xasprintf(&m.date, "%04d-01-01", song.year);
xasprintf(&m.duration, "%d:%02d:%02d.%03d", m.duration = duration_str(song.song_length);
(song.song_length/3600000),
(song.song_length/60000%60),
(song.song_length/1000%60),
(song.song_length%1000));
if( song.title && *song.title ) if( song.title && *song.title )
{ {
m.title = trim(song.title); m.title = trim(song.title);
@ -806,20 +802,13 @@ GetVideoMetadata(const char *path, char *name)
if( vstream ) if( vstream )
{ {
int off; int off;
int duration, hours, min, sec, ms;
ts_timestamp_t ts_timestamp = NONE; ts_timestamp_t ts_timestamp = NONE;
DPRINTF(E_DEBUG, L_METADATA, "Container: '%s' [%s]\n", ctx->iformat->name, basepath); DPRINTF(E_DEBUG, L_METADATA, "Container: '%s' [%s]\n", ctx->iformat->name, basepath);
xasprintf(&m.resolution, "%dx%d", lav_width(vstream), lav_height(vstream)); xasprintf(&m.resolution, "%dx%d", lav_width(vstream), lav_height(vstream));
if( ctx->bit_rate > 8 ) if( ctx->bit_rate > 8 )
m.bitrate = ctx->bit_rate / 8; m.bitrate = ctx->bit_rate / 8;
if( ctx->duration > 0 ) { if( ctx->duration > 0 )
duration = (int)(ctx->duration / AV_TIME_BASE); m.duration = duration_str(ctx->duration / (AV_TIME_BASE/1000));
hours = (int)(duration / 3600);
min = (int)(duration / 60 % 60);
sec = (int)(duration % 60);
ms = (int)(ctx->duration / (AV_TIME_BASE/1000) % 1000);
xasprintf(&m.duration, "%d:%02d:%02d.%03d", hours, min, sec, ms);
}
/* NOTE: The DLNA spec only provides for ASF (WMV), TS, PS, and MP4 containers. /* NOTE: The DLNA spec only provides for ASF (WMV), TS, PS, and MP4 containers.
* Skip DLNA parsing for everything else. */ * Skip DLNA parsing for everything else. */

16
utils.c
View File

@ -1,5 +1,5 @@
/* MiniDLNA media server /* MiniDLNA media server
* Copyright (C) 2008-2009 Justin Maggard * Copyright (C) 2008-2017 Justin Maggard
* *
* This file is part of MiniDLNA. * This file is part of MiniDLNA.
* *
@ -230,6 +230,20 @@ escape_tag(const char *tag, int force_alloc)
return esc_tag; return esc_tag;
} }
char *
duration_str(int msec)
{
char *str;
xasprintf(&str, "%d:%02d:%02d.%03d",
(msec / 3600000),
(msec / 60000 % 60),
(msec / 1000 % 60),
(msec % 1000));
return str;
}
char * char *
strip_ext(char *name) strip_ext(char *name)
{ {

View File

@ -5,7 +5,7 @@
* Author : Justin Maggard * Author : Justin Maggard
* *
* MiniDLNA media server * MiniDLNA media server
* Copyright (C) 2008-2009 Justin Maggard * Copyright (C) 2008-2017 Justin Maggard
* *
* This file is part of MiniDLNA. * This file is part of MiniDLNA.
* *
@ -80,6 +80,7 @@ char *strcasestrc(const char *s, const char *p, const char t);
char *modifyString(char *string, const char *before, const char *after, int noalloc); char *modifyString(char *string, const char *before, const char *after, int noalloc);
char *escape_tag(const char *tag, int force_alloc); char *escape_tag(const char *tag, int force_alloc);
char *unescape_tag(const char *tag, int force_alloc); char *unescape_tag(const char *tag, int force_alloc);
char *duration_str(int msec);
char *strip_ext(char *name); char *strip_ext(char *name);
/* Metadata functions */ /* Metadata functions */