简体   繁体   English

从 S3 下载 mp3 文件并对其进行操作会导致文件损坏

[英]Downloading an mp3 file from S3 and manipulating it results in bad file

I did a script that downloads a MP3 file from my S3 bucket and then manipulates in before download (Adding ID3 Tags).我做了一个脚本,从我的 S3 存储桶下载 MP3 文件,然后在下载前进行操作(添加 ID3 标签)。

It's working and the tags are injected properly, but the files corrupts as it seems and unplayable.它正在工作并且标签已正确注入,但文件看起来已损坏且无法播放。 I still can see my tags trough MP3tag so it has data in it, but no audio is playing trough the file.我仍然可以通过 MP3tag 看到我的标签,因此其中包含数据,但没有通过文件播放音频。

Heres my code, Trying to figure it what went wrong这是我的代码,试图找出问题所在

 const downloadFileWithID3 = async (filename, downloadName, injectedEmail) => {
  try {
    const data = await s3Client.send(
      new GetObjectCommand({
        Bucket: "BUCKETNAME",
        Key: filename,
      })
    );

    const fileStream = streamSaver.createWriteStream(downloadName);
    const writer = fileStream.getWriter();
    const reader = data.Body.getReader();

    const pump = () =>
      reader.read().then(({ value, done }) => {
        if (done) writer.close();
        else {
          const arrayBuffer = value;
          const writerID3 = new browserId3Writer(arrayBuffer);
          const titleAndArtist = downloadName.split("-");
          const [artist, title] = titleAndArtist;

          writerID3.setFrame("TIT2", title.slice(0, -4));
          writerID3.setFrame("TPE1", [artist]);
          writerID3.setFrame("TCOM", [injectedEmail]);
          writerID3.addTag();
          let taggedFile = new Uint8Array(writerID3.arrayBuffer);

          writer.write(taggedFile).then(pump);
        }
      });

    await pump()
      .then(() => console.log("Closed the stream, Done writing"))
      .catch((err) => console.log(err));
  } catch (err) {
    console.log(err);
  }
};

Hope you can help me solve this wierd bug, Thanks in advance!希望您能帮我解决这个奇怪的错误,在此先感谢!

Ok so i've figured it out, instead of using chunks of the stream itself i've used getSignedUrl from the s3 bucket it works.好的,所以我已经想通了,而不是使用 stream 本身的块,我使用了它工作的 s3 存储桶中的 getSignedUrl。

Thanks everyone for trying to help out!感谢大家尝试提供帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM