1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| const fs = require("fs"); const xml2js = require("xml2js");
function formatSecondsToSrtTime(totalSeconds) { const hours = Math.floor(totalSeconds / 3600); const minutes = Math.floor((totalSeconds % 3600) / 60); const seconds = Math.floor(totalSeconds % 60); const milliseconds = Math.floor((totalSeconds - Math.floor(totalSeconds)) * 1000);
const pad2 = (num) => num.toString().padStart(2, "0"); const pad3 = (num) => num.toString().padStart(3, "0");
return `${pad2(hours)}:${pad2(minutes)}:${pad2(seconds)},${pad3(milliseconds)}`; }
function convertFcpxmlTimeToSrt(timeStr, frameRate = 24) { if (timeStr.endsWith("s") && !timeStr.includes("/")) { const seconds = parseFloat(timeStr.replace("s", "")); return formatSecondsToSrtTime(seconds); }
if (timeStr.includes("/")) { const [frames, frameRateStr] = timeStr.replace("s", "").split("/"); const actualFrameRate = parseInt(frameRateStr) || frameRate; const seconds = parseInt(frames) / actualFrameRate; return formatSecondsToSrtTime(seconds); }
return "00:00:00,000"; }
function fcpxmlToSrt(fcpxmlPath, srtOutputPath, frameRate = 24) { const xmlContent = fs.readFileSync(fcpxmlPath, "utf8");
const parser = new xml2js.Parser({explicitArray: true, mergeAttrs: true}); let result; parser.parseString(xmlContent, (err, parsedResult) => { if (err) throw err; result = parsedResult; });
const subtitles = []; let subtitleIndex = 1;
function findSubtitles(node) { if (!node) return;
if (node.title) { node.title.forEach(title => { const offset = title.offset ? title.offset[0] : "0s"; const duration = title.duration ? title.duration[0] : "0s"; const text = title.text ? title.text[0] : "";
if (text) { const startSeconds = parseFloat(offset.replace("s", "")); const durationSeconds = parseFloat(duration.replace("s", "")); const endSeconds = startSeconds + durationSeconds;
const startTime = convertFcpxmlTimeToSrt(offset, frameRate); const endTime = convertFcpxmlTimeToSrt(`${endSeconds}s`, frameRate);
subtitles.push({ index: subtitleIndex++, startTime, endTime, text }); } }); }
for (const key in node) { if (typeof node[key] === "object" && node[key] !== null) { findSubtitles(node[key]); } } }
findSubtitles(result.fcpxml);
let srtContent = ""; subtitles.forEach(sub => { srtContent += `${sub.index}\n`; srtContent += `${sub.startTime} --> ${sub.endTime}\n`; srtContent += `${sub.text}\n\n`; });
fs.writeFileSync(srtOutputPath, srtContent, "utf8"); console.log(`提取字幕条数: ${subtitles.length}`); }
const fcpxmlPath = "./input.fcpxml"; const srtOutputPath = "./output.srt"; const frameRate = 24; fcpxmlToSrt(fcpxmlPath, srtOutputPath, frameRate);
|