
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
node-format-converter
Advanced tools
一个使用 Node.js 构建的通用文件格式转换库,支持多种文件类型的转换,无需 Express 即可使用。
npm install node-format-converter
最简单的使用方式,直接调用转换函数:
const converter = require('node-format-converter');
// 初始化(可选)
converter.init();
// 直接转换文件 - 最简单的 API
async function convertFile() {
try {
const result = await converter.convert(
'/path/to/image.png', // 源文件路径
'jpg', // 目标格式
{ // 选项(可选)
quality: 90, // 图片质量
width: 800, // 输出宽度
type: 'image' // 可选,如不提供会自动判断
}
);
console.log(`转换成功! 输出文件路径: ${result.outputPath}`);
return result;
} catch (error) {
console.error('转换失败:', error);
}
}
你也可以直接使用特定的转换器模块:
const { imageConverter, pdfConverter } = require('node-format-converter');
// 使用图片转换器
async function convertImage() {
try {
const result = await imageConverter.convertImage(
'/path/to/image.png', // 文件路径
'jpg', // 目标格式
{ // 选项(可选)
width: 800,
height: 600,
quality: 90
}
);
console.log(`图片转换成功! 输出文件路径: ${result.outputPath}`);
return result;
} catch (error) {
console.error('图片转换失败:', error);
}
}
如果你想在 Express 应用中使用,需要安装额外的依赖:
npm install express multer
然后可以轻松将转换服务集成到 Express 应用中:
const express = require('express');
const converter = require('node-format-converter');
// 初始化应用
const app = express();
converter.init(); // 确保目录存在
// 使用转换路由中间件
app.use('/convert', converter.getExpressRouter({
maxFileSize: 100 * 1024 * 1024, // 100MB 限制
}));
// 启动服务器
app.listen(3000, () => {
console.log('转换服务已启动,访问: http://localhost:3000/convert');
});
convert(filePath, targetFormat, options)
最简单的转换 API,自动处理文件类型检测和转换。
const result = await converter.convert('/path/to/file.jpg', 'png', {
quality: 90,
type: 'image' // 可选,如不提供会自动检测
});
init(options)
初始化转换器,创建必要的目录。
converter.init({
uploadsDir: '/custom/uploads/path', // 可选
convertedDir: '/custom/converted/path' // 可选
});
每个转换器模块都提供了相应的转换方法:
const { outputPath } = await imageConverter.convertImage(file, targetFormat, options);
const { outputPath } = await videoConverter.convertVideo(file, targetFormat, options);
const { outputPath } = await audioConverter.convertAudio(file, targetFormat, options);
const { outputPath } = await docConverter.convertDocument(file, targetFormat, options);
const { outputPath } = await excelConverter.convertExcel(file, targetFormat, options);
const { outputPath } = await pdfConverter.convertPdf(file, targetFormat, options);
{
type: 'image', // 文件类型,可选(自动检测)
cleanupAfterConversion: true, // 转换后是否删除源文件
outputDir: '/custom/output' // 自定义输出目录
}
{
width: 800, // 输出宽度
height: 600, // 输出高度
quality: 90, // 质量 (1-100)
fit: 'cover' // 调整大小方式: cover, contain, fill 等
}
{
resolution: '1280x720', // 分辨率
videoBitrate: '2000k', // 视频比特率
audioBitrate: '128k', // 音频比特率
fps: 30 // 帧率
}
{
audioBitrate: '192k', // 音频比特率
audioChannels: 2, // 音频通道数
sampleRate: 44100 // 采样率
}
{
sheetName: 'Sheet1', // 工作表名称
delimiter: ',' // CSV分隔符
}
{
density: 300, // DPI
quality: 100, // 图像质量
pages: [1, 2, 3] // 要转换的页码 (或 ["all"] 表示全部)
}
如果你安装了 Express 和 Multer,可以使用 getExpressRouter()
创建的路由:
端点:POST /convert
请求格式:multipart/form-data
参数说明:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
file | File | 是 | 要转换的文件 |
type | String | 否 | 文件类型,如不提供,将根据文件后缀自动判断 |
format | String | 是 | 目标格式,如 jpg, mp4, pdf 等 |
options | String (JSON) | 否 | 转换参数,根据不同类型有不同选项 |
cleanupAfterConversion: false
选项禁用)type
参数MIT
FAQs
Universal file format conversion service for Node.js
The npm package node-format-converter receives a total of 192 weekly downloads. As such, node-format-converter popularity was classified as not popular.
We found that node-format-converter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.