javascript 如何阅读简体中文字符从文本文件和保存mp3文件使用说命令在Mac上

inn6fuwd  于 2023-03-06  发布在  Java
关注(0)|答案(1)|浏览(120)

I am using Javascript. I have little experience with JavaScript and this is my first time trying to write a script. The Chinese text is not properly read in using this method. For example "他是一位非常著名的演员" gets saved as "‰ªñÊò؉∏ĉΩçÈùûÂ∏∏ËëóÂêçÁöÑʺîÂëò.aiff". I also know the pronunciation is not correct using this method. I've read this documentation and have searched around but can't find anything about this issue. I also have no idea how to write an mp3 file.

var app = Application.currentApplication();
app.includeStandardAdditions = true;

var basePath = "path to directory";
var inputPath = basePath + "/input.txt";
var text = app.read(inputPath).split("\n");

for (let i = 0; i < text.length; i++) {
    var recordingPath = basePath + "/" + text[i] + ".aiff";
    app.say(text[i], {savingTo: recordingPath});
    app.say(text[i]);
  }
l3zydbqr

l3zydbqr1#

这看起来像是编码问题。脚本不知道它要读什么,所以你必须告诉它它被编码为«class utf8»。我不使用javascript,所以我不能帮助你了解它的语法,但在applescript中,它看起来像这样。

read file inputPath as «class utf8»
--> "他是一位非常著名的演员"

音频是独立的,但即使你不知道如何录制到mp3,你也有一些转换选项。itunes可以将AIFF转换为mp3。我不确定shell命令afconvert是否可以转换为mp3(看起来不太有希望),但它可以转换为m4a。

相关问题