Skip to content

Commit

Permalink
Merge pull request funmusicplace#829 from funmusicplace/various-fixes
Browse files Browse the repository at this point in the history
fix: at least ensure that metadata tags are added to downloaded songs
  • Loading branch information
simonv3 authored Aug 30, 2024
2 parents ea2730a + f14fcbc commit 03f5afe
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
33 changes: 22 additions & 11 deletions src/jobs/generate-album.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ import {
import { convertAudioToFormat } from "../utils/tracks";
import archiver from "archiver";
import { PassThrough } from "stream";
import { Track, TrackGroup } from "@mirlo/prisma/client";
import {
Track,
TrackArtist,
TrackAudio,
TrackGroup,
} from "@mirlo/prisma/client";
import filenamify from "filenamify";
import prisma from "@mirlo/prisma";

const {
MINIO_HOST = "",
Expand Down Expand Up @@ -41,13 +47,10 @@ const parseFormat = (format: string) => {
};

export default async (job: Job) => {
const {
trackGroup,
tracks,
format: formatString,
} = job.data as {
trackGroup: TrackGroup;
tracks: (Track & { audio: { id: string; fileExtension: string } })[];
const { trackGroup, format: formatString } = job.data as {
trackGroup: TrackGroup & {
tracks: (Track & { audio: TrackAudio; trackArtists: TrackArtist[] })[];
};
format: string;
};
const format = parseFormat(formatString);
Expand All @@ -72,7 +75,7 @@ export default async (job: Job) => {
const profiler = logger.startTimer();

let i = 0;
for await (const track of tracks) {
for await (const track of trackGroup.tracks) {
const minioTrackLocation = `${track.audio.id}/original.${track.audio.fileExtension}`;
logger.info(`audioId ${track.audio.id}: Fetching ${minioTrackLocation}`);
const originalTrackPath = `${tempFolder}/original.${track.audio.fileExtension}`;
Expand All @@ -82,10 +85,18 @@ export default async (job: Job) => {
minioTrackLocation,
originalTrackPath
);
progress += (i * 70) / tracks.length;
progress += (i * 70) / trackGroup.tracks.length;
i += 1;
await job.updateProgress(progress);

const artist = await prisma.artist.findFirst({
where: { id: trackGroup.artistId },
});

if (!artist) {
throw "Couldn't find artist, weird";
}

await new Promise((resolve, reject) => {
logger.info(
`audioId ${track.audio.id}: Processing stream for ${format.format}${
Expand All @@ -94,7 +105,7 @@ export default async (job: Job) => {
);

convertAudioToFormat(
track.audio.id,
{ track, artist, trackGroup },
createReadStream(originalTrackPath),
format,
`${tempFolder}/${track.order ?? i}-${filenamify(track.title ?? "")}`,
Expand Down
6 changes: 2 additions & 4 deletions src/queues/album-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ generateAlbumQueueEvents.on("error", async (error) => {
});

export const startGeneratingAlbum = async (
trackGroup: TrackGroup,
format: string,
tracks: Track[]
trackGroup: TrackGroup & { tracks: Track[] },
format: string
) => {
const job = await generateAlbumQueue.add("generate-album", {
trackGroup,
format,
tracks,
});

return job.id;
Expand Down
6 changes: 1 addition & 5 deletions src/routers/v1/trackGroups/{id}/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ export default function () {
await minioClient.statObject(trackGroupFormatBucket, zipName);
} catch (e) {
logger.info("trackGroup doesn't exist yet, start generating it");
const jobId = await startGeneratingAlbum(
trackGroup,
format,
trackGroup.tracks
);
const jobId = await startGeneratingAlbum(trackGroup, format);
return res.json({
message: "We've started generating the album",
result: { jobId },
Expand Down
2 changes: 1 addition & 1 deletion src/routers/v1/trackGroups/{id}/generateDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default function () {
return next();
}

await startGeneratingAlbum(trackGroup, format, trackGroup.tracks);
await startGeneratingAlbum(trackGroup, format);

return res.json({
message: "Success",
Expand Down
2 changes: 2 additions & 0 deletions src/utils/trackGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Response } from "express";
import { DefaultArgs } from "@prisma/client/runtime/library";
import { doesTrackGroupBelongToUser } from "./ownership";
import { AppError } from "./error";
import trackArtists from "../routers/v1/manage/tracks/{trackId}/trackArtists";

export const whereForPublishedTrackGroups = (): Prisma.TrackGroupWhereInput => {
return {
Expand Down Expand Up @@ -341,6 +342,7 @@ export const basicTrackGroupInclude = {
tracks: {
include: {
audio: true,
trackArtists: true,
},
where: {
deletedAt: null,
Expand Down
23 changes: 22 additions & 1 deletion src/utils/tracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import { finalAudioBucket, removeObjectsFromBucket } from "../utils/minio";
import ffmpeg from "fluent-ffmpeg";
import logger from "../logger";
import { Readable } from "stream";
import {
Artist,
Track,
TrackArtist,
TrackAudio,
TrackGroup,
} from "@mirlo/prisma/client";

export const deleteTrack = async (trackId: number) => {
await prisma.track.delete({
Expand Down Expand Up @@ -47,7 +54,11 @@ const generateDestination = (
};

export const convertAudioToFormat = (
audioId: string,
content: {
track: Track & { audio: TrackAudio; trackArtists: TrackArtist[] };
artist: Artist;
trackGroup: TrackGroup;
},
stream: Readable,
formatDetails: {
format: "wav" | "flac" | "opus" | "mp3";
Expand All @@ -58,15 +69,25 @@ export const convertAudioToFormat = (
onError?: (err: unknown) => void,
onSuccess?: (dunno: null) => void
) => {
const audioId = content.track.audio.id;
const { format, audioBitrate, audioCodec } = formatDetails;
logger.info(
`audioId ${audioId}: converting ${format} going to ${goingTo} @${audioBitrate}`
);

console.log(content.track.metadata);

let destination = generateDestination(format, goingTo, audioBitrate);
const processor = ffmpeg(stream)
.noVideo()
.toFormat(format)
// FIXME why don't these work?
// .outputOptions("-map_metadata:s:a", "0:s:a")
// .outputOption("-map_metadata:s:a 0:s:a")
// .outputOptions("-map_metadata 0:s")
.outputOptions("-metadata", `title=${content.track.title}`)
.outputOptions("-metadata", `album=${content.trackGroup.title}`)
.outputOptions("-metadata", `artist=${content.artist.name}`)
.on("stderr", function (stderrLine) {
// logger.info("Stderr output: " +resolve stderrLine);
})
Expand Down

0 comments on commit 03f5afe

Please sign in to comment.