Skip to content

Commit c54301f

Browse files
committed
feat: add music-metadata to pull duration and container type
1 parent b54e224 commit c54301f

8 files changed

Lines changed: 240 additions & 11 deletions

File tree

app/lib/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ export const pageTitle = (...parts: string[]) => {
2323
export const docsLink = (path: string) => {
2424
return `${DOCS_URL}${path}`
2525
}
26+
27+
export const getSecondsAsTime = (seconds: number) => {
28+
let secondTime = seconds
29+
let minuteTime = Math.floor(secondTime / 60)
30+
31+
secondTime %= 60
32+
minuteTime %= 60
33+
34+
return [minuteTime.toString(), secondTime.toString().padStart(2, '0')].join(
35+
':'
36+
)
37+
}

app/routes/sounds.$sound._index.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import {
44
redirect
55
} from '@remix-run/node'
66
import {useLoaderData, useNavigate} from '@remix-run/react'
7+
import {parseFile} from 'music-metadata'
8+
import path from 'path'
79

810
import {getPrisma} from '~/lib/prisma.server'
911
import {checkSession} from '~/lib/session'
10-
import {pageTitle} from '~/lib/utils'
12+
import {pageTitle, getSecondsAsTime} from '~/lib/utils'
1113
import {Page, Actions} from '~/lib/ui'
1214

1315
export const meta: MetaFunction<typeof loader> = ({data}) => {
@@ -27,6 +29,24 @@ export const loader = async ({request, params}: LoaderFunctionArgs) => {
2729
where: {id: params.sound}
2830
})
2931

32+
if (sound.audioContainer === '' || sound.duration === 0) {
33+
console.log('updating meta data')
34+
const meta = await parseFile(
35+
path.join(process.cwd(), 'public', 'sounds', sound.fileName)
36+
)
37+
38+
await prisma.audio.update({
39+
where: {id: sound.id},
40+
data: {
41+
duration: meta.format.duration,
42+
audioContainer: meta.format.container
43+
}
44+
})
45+
46+
sound.audioContainer = meta.format.container ? meta.format.container : ''
47+
sound.duration = meta.format.duration ? meta.format.duration : 0
48+
}
49+
3050
return {sound}
3151
}
3252

@@ -41,6 +61,8 @@ const Sound = () => {
4161
<source src={`/sounds/${sound.fileName}`} type="audio/mp3" />
4262
</audio>
4363
<p>Ringer Wire: {sound.ringerWire}</p>
64+
<p>Duration: {getSecondsAsTime(sound.duration)}</p>
65+
<p>Audio Type: {sound.audioContainer}</p>
4466
</div>
4567
<Actions
4668
actions={[

app/routes/sounds.add-tts.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import path from 'path'
1010
import fs from 'fs'
1111
import {finished} from 'stream/promises'
1212
import {Readable} from 'stream'
13+
import {parseFile} from 'music-metadata'
1314

1415
import {getPrisma} from '~/lib/prisma.server'
1516
import {updateSounders} from '~/lib/update-sounders.server'
@@ -76,9 +77,17 @@ export const action = async ({request}: ActionFunctionArgs) => {
7677
Readable.fromWeb(downloadResponse!.body as any).pipe(downloadStream)
7778
)
7879

80+
const meta = await parseFile(
81+
path.join(process.cwd(), 'public', 'sounds', `${sound.id}.wav`)
82+
)
83+
7984
await prisma.audio.update({
8085
where: {id: sound.id},
81-
data: {fileName: `${sound.id}.wav`}
86+
data: {
87+
fileName: `${sound.id}.wav`,
88+
duration: meta.format.duration,
89+
audioContainer: meta.format.container
90+
}
8291
})
8392

8493
await updateSounders()

app/routes/sounds.add.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {useNavigate} from '@remix-run/react'
1212
import {invariant} from '@arcath/utils'
1313
import path from 'path'
1414
import fs from 'fs'
15+
import {parseFile} from 'music-metadata'
1516

1617
import {getPrisma} from '~/lib/prisma.server'
1718
import {checkSession} from '~/lib/session'
@@ -87,9 +88,22 @@ export const action = async ({request}: ActionFunctionArgs) => {
8788
)
8889
)
8990

91+
const meta = await parseFile(
92+
path.join(
93+
process.cwd(),
94+
'public',
95+
'sounds',
96+
`${sound.id}${path.extname(fileData.filepath)}`
97+
)
98+
)
99+
90100
await prisma.audio.update({
91101
where: {id: sound.id},
92-
data: {fileName: `${sound.id}${path.extname(fileData.filepath)}`}
102+
data: {
103+
fileName: `${sound.id}${path.extname(fileData.filepath)}`,
104+
duration: meta.format.duration,
105+
audioContainer: meta.format.container
106+
}
93107
})
94108

95109
await updateSounders()

package-lock.json

Lines changed: 157 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"isbot": "^5.1.30",
3030
"jsonwebtoken": "^9.0.2",
3131
"mkdirp": "^3.0.1",
32+
"music-metadata": "^11.9.0",
3233
"prisma": "^6.15.0",
3334
"react": "^18.2.0",
3435
"react-dom": "^18.2.0",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- RedefineTables
2+
PRAGMA defer_foreign_keys=ON;
3+
PRAGMA foreign_keys=OFF;
4+
CREATE TABLE "new_Audio" (
5+
"id" TEXT NOT NULL PRIMARY KEY,
6+
"name" TEXT NOT NULL,
7+
"fileName" TEXT NOT NULL,
8+
"ringerWire" TEXT NOT NULL DEFAULT '',
9+
"duration" INTEGER NOT NULL DEFAULT 0,
10+
"audioContainer" TEXT NOT NULL DEFAULT ''
11+
);
12+
INSERT INTO "new_Audio" ("fileName", "id", "name", "ringerWire") SELECT "fileName", "id", "name", "ringerWire" FROM "Audio";
13+
DROP TABLE "Audio";
14+
ALTER TABLE "new_Audio" RENAME TO "Audio";
15+
PRAGMA foreign_keys=ON;
16+
PRAGMA defer_foreign_keys=OFF;

0 commit comments

Comments
 (0)