打开字幕字符编码

Open subtitles characters encoding

本文关键字:编码 字符 字幕      更新时间:2023-09-26
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);
});

使用浏览器正常下载会产生正确的文件编码。使用邮递员下载文件,我得到编码不正确的文档损坏。

示例:Je suis d sol e。Je sais

没有成功,我试图将"内容类型"标头设置为 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我使用流媒体支持来让它工作。

这就是你可以做到的。 你需要做npm install --save iconv-lite

var iconv = require('iconv-lite');
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);
    var formattedData = iconv.decode(body, 'iso-8859-1');
    console.log(formattedData);
});

你可以查看 https://github.com/request/request/issues/118,他建议使用iconv-lite