Added download current track lyrics

This commit is contained in:
srgooglo 2025-06-19 01:22:43 +02:00
parent 5fe7fe2a30
commit 55c61cc2c3
2 changed files with 26 additions and 33 deletions

View File

@ -2,7 +2,7 @@ import React from "react"
import PropTypes from "prop-types"
import classnames from "classnames"
import { parseLRC } from "../../utils/lrcParser"
import { parseLRC, formatToLRC } from "../../utils/lrcParser"
import {
Input,
@ -351,6 +351,17 @@ const LyricsEditor = ({ player }) => {
app.message.success("Language file loaded")
}
const handleLanguageDownload = () => {
const data = formatToLRC(lines)
const blob = new Blob([data], { type: "text/plain" })
const url = URL.createObjectURL(blob)
const link = document.createElement("a")
link.href = url
link.download = `${state.track.title} - ${state.selectedLanguage}.txt`
link.click()
}
const followLineTick = () => {
const currentTime = player.current.audio.current.currentTime
@ -409,6 +420,9 @@ const LyricsEditor = ({ player }) => {
>
Load file
</UploadButton>
<Button onClick={() => handleLanguageDownload()}>
Download current
</Button>
</Col>
</Row>

View File

@ -92,43 +92,22 @@ export const parseLRC = (lrcContent) => {
* @param {Object} lrcData - Structured LRC data
* @returns {string} LRC formatted string
*/
export const formatToLRC = (lrcData) => {
const { metadata = {}, lyrics = [] } = lrcData
const lines = []
export const formatToLRC = (lines) => {
const data = []
// Add metadata
const metadataMapping = {
artist: "ar",
title: "ti",
album: "al",
author: "au",
length: "length",
creator: "by",
editor: "re",
version: "ve",
offset: "offset",
}
lines.forEach((line) => {
if (line.time !== null) {
const timeStr = line.timeStr || formatSecondsToLRC(line.time)
Object.entries(metadata).forEach(([key, value]) => {
const tag = metadataMapping[key] || key
lines.push(`[${tag}:${value}]`)
})
if (lines.length > 0) {
lines.push("") // Empty line after metadata
}
// Add lyrics
lyrics.forEach((lyric) => {
if (lyric.time !== null) {
const timeStr = lyric.timeStr || formatSecondsToLRC(lyric.time)
lines.push(`[${timeStr}]${lyric.text}`)
} else {
lines.push(lyric.text)
if (line.break) {
data.push(`[${timeStr}]`)
} else {
data.push(`[${timeStr}] ${line.text}`)
}
}
})
return lines.join("\n")
return data.join("\n")
}
/**