Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): audio-source metadata enriched with file byteSize #723

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/backend-modules/documents/graphql/schema-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type AudioSource implements PlayableMedia {
aac: String
ogg: String
durationMs: Int!
byteSize: Int
}

enum AudioSourceKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const {

const { hashObject } = require('../../../lib/git')
const { updateCurrentPhase, toCommit } = require('../../../lib/postgres')
const { maybeApplyAudioSourceDuration } = require('../../../lib/audioSource')
const {
maybeApplyAudioSourceDurationAndByteSize,
} = require('../../../lib/audioSource')

const {
lib: {
Expand Down Expand Up @@ -162,7 +164,7 @@ module.exports = async (_, args, context) => {
{ fields: ['id', 'meta'] },
))

await maybeApplyAudioSourceDuration(meta, parentCommit?.meta)
await maybeApplyAudioSourceDurationAndByteSize(meta, parentCommit?.meta)

const author = {
name: user.name,
Expand Down
2 changes: 2 additions & 0 deletions packages/backend-modules/publikator/lib/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const prepareMetaForPublish = async ({
audioSourceAac,
audioSourceOgg,
audioSourceDurationMs,
audioSourceByteSize,
} = doc.content.meta

const audioSource =
Expand All @@ -115,6 +116,7 @@ const prepareMetaForPublish = async ({
aac: audioSourceAac,
ogg: audioSourceOgg,
durationMs: audioSourceDurationMs,
byteSize: audioSourceByteSize,
}
: null

Expand Down
7 changes: 6 additions & 1 deletion packages/backend-modules/publikator/lib/audioSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const debug = Debug('publikator:lib:audioSource')
interface CommitMeta {
audioSourceMp3?: string
audioSourceDurationMs?: number
audioSourceByteSize?: number
}

const resToArrayBuffer = (res: Response) => {
Expand All @@ -28,7 +29,7 @@ const checkSeconds = (seconds: number) => {

const toMiliseconds = (seconds: number) => Math.round(seconds * 1000)

export const maybeApplyAudioSourceDuration = async (
export const maybeApplyAudioSourceDurationAndByteSize = async (
currentMeta: CommitMeta,
previousMeta?: CommitMeta,
): Promise<void> => {
Expand All @@ -45,6 +46,10 @@ export const maybeApplyAudioSourceDuration = async (
await fetch(currentMeta.audioSourceMp3)
.then(resToArrayBuffer)
.then(Buffer.from)
.then((buffer) => {
currentMeta.audioSourceByteSize = buffer.byteLength
return buffer
})
.then(measureDuration)
.then(checkSeconds)
.then(toMiliseconds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const {
} = require('@orbiting/backend-modules-base')

const { maybeDelcareMilestonePublished } = require('../lib/postgres')
const { maybeApplyAudioSourceDuration } = require('../lib/audioSource')
const {
maybeApplyAudioSourceDurationAndByteSize,
} = require('../lib/audioSource')

const debug = Debug('publikator:script:migrateAudioSources')

Expand Down Expand Up @@ -102,7 +104,7 @@ const handleBatch = async (rows: any[], count: number, pgdb: any) => {
return
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script probably woudln't work correctly like this, you would also need to check for audioSourceByteSize in the checks above. But afaik this script was used to add a synthetic voice to all past articles, so probably it isn't that relevant

await maybeApplyAudioSourceDuration(meta)
await maybeApplyAudioSourceDurationAndByteSize(meta)

const tx = await pgdb.transactionBegin()
debug('transaction begin')
Expand Down
Loading