javascript - 打开字幕字符编码

标签 javascript node.js

var request = require("request");

var options = { method: 'GET', url:'http://dl.opensubtitles.org/en/download/filead/src-api/vrf-19af0c55/sid-ste0uene5gb0jh8dsrma7tcq15/1955127527.srt'};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

使用浏览器进行正常下载会产生正确的文件编码。使用 postman 下载文件时,我得到了编码不正确的损坏文档。

示例:Je suis d�sol�e。我说

没有成功,我尝试将“Content-Type” header 设置为 utf8...

最佳答案

    http.get(sub.url, function(res) {
      res.pipe(iconv.decodeStream('win1252')).collect(function(err, decodedBody) {
        srt2vtt(decodedBody, function(err, vttData) {
          if (err) throw new Error(err)

          let filename = 'sub-' + sub.lang + 'vtt'
          let vttPath = path.join(dir, filename)
          fs.writeFileSync(vttPath, vttData)
          cb(vttPath)
    })
  });
});

https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding 我使用流支持来使其正常工作。

关于javascript - 打开字幕字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36954866/

相关文章:

Javascript worker.postMessage 缩小空数组

javascript - 如何规划一个新的JS应用的结构

javascript - 将 javascript 中的值引入单选按钮

forEach 循环中的 JavaScript 变量作用域

javascript - const {something} = require (something) 在 Express 中不起作用

node.js - 在哪里可以找到固定成本的 Node.js 托管?

javascript - JS 中的 window.innerWidth 不等于 css 中的 `width:100%`

javascript - Mongoose populate() 返回对象数组而不是单个对象

node.js - Twilio 函数 : Sending WhatsApp message when incoming call is received

node.js - npm 审计不起作用并且没有列出依赖项的描述