How to read Simplified Mandarin Chinese characters From text file and save mp3 file using say command on Mac

huangapple go评论101阅读模式
英文:

How to read Simplified Mandarin Chinese characters From text file and save mp3 file using say command on Mac

问题

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]);
}
英文:

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 = &quot;path to directory&quot;;
var inputPath = basePath + &quot;/input.txt&quot;;
var text = app.read(inputPath).split(&quot;\n&quot;);

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

答案1

得分: 1

这似乎是一个编码问题。脚本不知道它试图读取什么,所以你必须告诉它以&#171;class utf8&#187;编码。我不使用JavaScript,所以无法帮助你处理它的语法,但在AppleScript中,它会如下所示:

read file inputPath as &#171;class utf8&#187;
--&gt; &quot;他是一位非常著名的演员&quot;

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

英文:

It looks like an encoding issue. The script doesn't know what it's trying to read so you have to tell it that it's encoded as &#171;class utf8&#187;. I don't use javascript so I can't help you with its syntax but in applescript, it would look like this.

read file inputPath as &#171;class utf8&#187;
--&gt; &quot;他是一位非常著名的演员&quot;

The audio stuff is separate but even if you don't know how to record to mp3, you have some conversion options. itunes can convert AIFF to mp3. I'm not sure whether the shell command afconvert can convert to mp3 (doesn't look promising) but it can convert to m4a.

huangapple
  • 本文由 发表于 2023年3月4日 07:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定