如何转换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 分钟前
Vue reactive 底层 Proxy 完整流程(依赖收集 + 触发更新)
前端·javascript·vue.js
周万宁.FoBJ6 分钟前
vue源码讲解之 effect解析 (仅包含在effect中使用reacitve情况)
前端·javascript·vue.js
Qlittleboy10 分钟前
<el-form @submit.native.prevent> elementUI的里面的input的元素的回车事件后总是自动提交表单
前端·javascript·elementui
Carsene11 分钟前
Docsify 文档缓存问题终极解决方案:拦截请求自动添加版本号
前端·javascript
周淳APP12 分钟前
【VDOM,Diff算法,生命周期,并发请求】
前端·javascript·vue.js
Linncharm12 分钟前
重写一个「年久失修」的开源项目:把 jQuery + CoffeeScript 的 3D 户型图工具迁移到 TypeScript + Three.js r181
前端
竹林81815 分钟前
从“后端验证”到“前端签名”:我在Web3项目中重构用户身份认证的实战记录
前端·javascript
农夫山泉不太甜16 分钟前
Expo开发App实战指南:从技术选型到架构设计
前端
高桥凉介发量惊人17 分钟前
状态管理与架构篇-异步状态管理:加载、空态、错误态统一处理
前端
清汤饺子17 分钟前
搞懂 Cursor 后,我一行代码都不敲了《进阶篇》
前端·javascript·后端