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 PropTypes from "prop-types"
import classnames from "classnames" import classnames from "classnames"
import { parseLRC } from "../../utils/lrcParser" import { parseLRC, formatToLRC } from "../../utils/lrcParser"
import { import {
Input, Input,
@ -351,6 +351,17 @@ const LyricsEditor = ({ player }) => {
app.message.success("Language file loaded") 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 followLineTick = () => {
const currentTime = player.current.audio.current.currentTime const currentTime = player.current.audio.current.currentTime
@ -409,6 +420,9 @@ const LyricsEditor = ({ player }) => {
> >
Load file Load file
</UploadButton> </UploadButton>
<Button onClick={() => handleLanguageDownload()}>
Download current
</Button>
</Col> </Col>
</Row> </Row>

View File

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