如何转换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

相关推荐
weixin_4435669819 分钟前
CSS 预处理器
前端·css
excel28 分钟前
webpack 核心编译器 第一节
前端
大怪v39 分钟前
前端佬们!塌房了!用过Element-Plus的进来~
前端·javascript·element
拉不动的猪1 小时前
electron的主进程与渲染进程之间的通信
前端·javascript·面试
软件技术NINI1 小时前
html css 网页制作成品——HTML+CSS非遗文化扎染网页设计(5页)附源码
前端·css·html
fangcaojushi1 小时前
npm常用的命令
前端·npm·node.js
阿丽塔~1 小时前
新手小白 react-useEffect 使用场景
前端·react.js·前端框架
鱼樱前端2 小时前
Rollup 在前端工程化中的核心应用解析-重新认识下Rollup
前端·javascript
m0_740154672 小时前
SpringMVC 请求和响应
java·服务器·前端
加减法原则2 小时前
探索 RAG(检索增强生成)
前端