我正在尝试制作一首歌曲的滚动卡拉OK歌词。歌词软件输出一个代码,其中包含我需要转换的以秒为单位的时间。
我有同一首歌的两个版本。一个运行在52 bpm (节拍每分钟),这是一个较慢的版本,另一个运行在104 bpm,这是一个更快的版本。
我已经完成了第一个版本,这花了我很多时间。我想以这样的方式转换代码,以便我可以自动获得更快的版本。
以下是示例代码
<item dStartTime="4" dEndTime="8" n3DRhythm="2" str3DSceneLayoutFile="">
<text>Batti Gul Meter Chalu</text>
</item>
<item dStartTime="9.52" dEndTime="14.47" n3DRhythm="2" str3DSceneLayoutFile="">
<text>rajj<10.44> ke<10.99> rulaya</text>
</item>
<item dStartTime="14.47" dEndTime="19.06" n3DRhythm="2" str3DSceneLayoutFile="">
<text>rajj ke<15.94> hansaya</text>
</item>
我想转换相同的代码,如下所示,以秒为单位的时间应该正好是两个小数点的一半
<item dStartTime="2" dEndTime="4" n3DRhythm="2" str3DSceneLayoutFile="">
<text>Batti Gul Meter Chalu</text>
</item>
<item dStartTime="4.76" dEndTime="7.24" n3DRhythm="2" str3DSceneLayoutFile="">
<text>rajj<5.22> ke<5.50> rulaya</text>
</item>
<item dStartTime="7.24" dEndTime="9.53" n3DRhythm="2" str3DSceneLayoutFile="">
<text>rajj ke<7.97> hansaya</text>
</item>
我使用MS Excel中的代码,并尝试使用替换、拼接和其他一些公式来实现我所需要的,但我无法做到这一点,虽然excel vba可以帮助我,但我不能在vba中做到这一点。请求你们帮我解决这个问题,因为我需要转换很多歌曲。你的好心帮助会节省很多时间。任何帮助在excel vba,javascript或任何其他方式替换的代码。
在<item>
部分中,我需要将dStartTime
和dEndTime
在<text>
部分中,我需要转换任何看起来像数字的内容
谢谢
发布于 2018-10-07 06:21:02
在Javascript中,可以使用DOMParser
将字符串转换为文档,之后可以遍历每个<item>
,更改dStartTime
和dEndTime
属性,然后更改子<text>
节点中的任何数字:
const str = `<item dStartTime="4" dEndTime="8" n3DRhythm="2" str3DSceneLayoutFile="">
<text>Batti Gul Meter Chalu</text>
</item>
<item dStartTime="9.52" dEndTime="14.47" n3DRhythm="2" str3DSceneLayoutFile="">
<text>rajj<10.44> ke<10.99> rulaya</text>
</item>
<item dStartTime="14.47" dEndTime="19.06" n3DRhythm="2" str3DSceneLayoutFile="">
<text>rajj ke<15.94> hansaya</text>
</item> `;
const doc = new DOMParser().parseFromString(str, 'text/html');
const halve = num => Math.round(100 * num / 2) / 100;
const halveAttrib = (node, name) => {
const newVal = halve(node.getAttribute(name))
node.setAttribute(name, newVal);
};
doc.querySelectorAll('item').forEach((item) => {
halveAttrib(item, 'dStartTime');
halveAttrib(item, 'dEndTime');
item.children[0].textContent = item.children[0].textContent
.replace(/\d+(\.\d+)/g, num => halve(num).toFixed(2));
});
const output = doc.body.innerHTML
.replace(/dstarttime/g, 'dStartTime')
.replace(/dendtime/g, 'dEndTime')
.replace(/n3drhythm/g, 'n3DRhythm')
.replace(/str3dscenelayoutfile/g, 'str3DSceneLayoutFile');
console.log(output);
https://stackoverflow.com/questions/52683756
复制相似问题