如何转换PPT里的音视频格式

如何获取 ppt 内的所有文件

  • 可以看下microsoft中的介绍,以下为其中一段话
sh 复制代码
If you want to separately use files or objects from a PowerPoint presentation,
such as videos, photos, or sounds,
you can extract them by converting the presentation to a "zipped" file folder.
Note, however, that you can't extract PDFs or .dotx files.

就是说,如果你想看ppt内的所有文件,你可以将ppt的后缀名改为 .zip, 然后解压就可以看到所有内容了

详情截图

如何修改 ppt

  1. 解压 zip 包
  2. 修改文件
  3. 压缩成 zip
  4. 修改文件后缀

代码示列

typescript 复制代码
import path from "path";
import fs from "fs";
import ffmpeg from "fluent-ffmpeg";
import { path as ffmpegPath } from "@ffmpeg-installer/ffmpeg";
import decompress from "decompress";
import archiver from "archiver";

ffmpeg.setFfmpegPath(ffmpegPath);

const VIDEO_TYPE = new Array(
  "avi",
  "wmv",
  "mpg",
  "mpeg",
  "mov",
  "rm",
  "ram",
  "swf",
  "flv",
  "mp4",
  "wma",
  "rm",
  "rmvb",
  "flv",
  "mpg",
  "mkv"
);

export class ParseFile {
  // 压缩
  private zipDirectory(sourceDir: string, outPath: string) {
    const archive = archiver("zip", { zlib: { level: 9 } });
    const stream = fs.createWriteStream(outPath);
    return new Promise((resolve, reject) => {
      archive
        .directory(sourceDir, false)
        .on("error", (err) => reject(err))
        .pipe(stream);
      stream.on("close", () => resolve(true));
      archive.finalize();
    });
  }

  // 将video 转为H.264
  public translateVideo(filePath: string) {
    const inputPath = filePath;
    const fileNameIndex = filePath.lastIndexOf("/");
    const outputPath =
      inputPath.slice(0, fileNameIndex) +
      "/output_" +
      inputPath.slice(fileNameIndex + 1);
    return new Promise((resolve, reject) => {
      ffmpeg(inputPath)
        .videoCodec("libx264")
        .inputFPS(25)
        .videoBitrate(2000)
        .on("error", function (err) {
          console.log("err", err);
          reject(err);
        })
        .on("end", async function () {
          await fs.promises.unlink(inputPath);
          await fs.promises.rename(outputPath, inputPath);
          console.log("Processing finished !");
          resolve("Processing finished !");
        })
        .save(outputPath);
    });
  }

  // 转ppt
  public async translatePPT(filePath: string) {
    const fileBuffer = await fs.promises.readFile(filePath);
    const time = new Date().getTime();
    const temp = path.resolve(__dirname, `./temp/${time}`);

    const files = await decompress(fileBuffer, temp);

    let count = files.length;
    while (count) {
      const file = files[count - 1];
      const fileName = file.path.slice(file.path.lastIndexOf("/") + 1);
      const ext = fileName.slice(fileName.lastIndexOf(".") + 1);
      if (VIDEO_TYPE.includes(ext)) {
        await this.translateVideo(`${temp}/${file.path}`);
      }
      count -= 1;
    }
    console.log(path.resolve(__dirname, `./output/${time}.ppt`));
    await this.zipDirectory(
      path.resolve(temp),
      path.resolve(__dirname, `./output_${time}.ppt`)
    );
    try {
      await fs.promises.rm(temp, { recursive: true });
    } catch (error) {}
  }
}

const parseFile = new ParseFile();

// parseFile.translatePPT(
//   path.resolve(__dirname, "your_ppt_path.ppt")
// );

export default parseFile;

参考文章

mime

相关推荐
小小小小宇5 小时前
虚拟列表兼容老DOM操作
前端
悦悦子a啊5 小时前
Python之--基本知识
开发语言·前端·python
安全系统学习6 小时前
系统安全之大模型案例分析
前端·安全·web安全·网络安全·xss
涛哥码咖6 小时前
chrome安装AXURE插件后无效
前端·chrome·axure
OEC小胖胖6 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
行云&流水6 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
Sally璐璐6 小时前
零基础学HTML和CSS:网页设计入门
前端·css
老虎06277 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
灿灿121387 小时前
CSS 文字浮雕效果:巧用 text-shadow 实现 3D 立体文字
前端·css
烛阴7 小时前
Babel 完全上手指南:从零开始解锁现代 JavaScript 开发的超能力!
前端·javascript