{"id":50757907,"url":"https://github.com/bytechaocai/ffmpeg-js","last_synced_at":"2026-06-11T07:01:38.905Z","repository":{"id":361757873,"uuid":"1255619539","full_name":"bytechaocai/ffmpeg-js","owner":"bytechaocai","description":"使用js生成批处理脚本运行ffmpeg","archived":false,"fork":false,"pushed_at":"2026-06-08T18:07:45.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-08T18:25:23.420Z","etag":null,"topics":["batchfile","ffmpeg","ffmpeg-script","javascript","node"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bytechaocai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-06-01T02:49:05.000Z","updated_at":"2026-06-08T18:11:37.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/bytechaocai/ffmpeg-js","commit_stats":null,"previous_names":["bytechaocai/ffmpeg-js"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/bytechaocai/ffmpeg-js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytechaocai%2Fffmpeg-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytechaocai%2Fffmpeg-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytechaocai%2Fffmpeg-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytechaocai%2Fffmpeg-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bytechaocai","download_url":"https://codeload.github.com/bytechaocai/ffmpeg-js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bytechaocai%2Fffmpeg-js/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34186385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["batchfile","ffmpeg","ffmpeg-script","javascript","node"],"created_at":"2026-06-11T07:01:38.049Z","updated_at":"2026-06-11T07:01:38.871Z","avatar_url":"https://github.com/bytechaocai.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ffmpeg-js\n\n## 项目简介\n\n我平时经常转码视频以节省磁盘空间，当文件少的时候可以直接转，但当文件多的时候非常麻烦，需要先用`ffprobe`获取码率，然后用`ffmpeg`转码，每一个文件都需要如此，此外，为了防止网盘窥探视频，我还需要将视频加密打包然后上传，而不是直接上传原始视频。为了解决以上问题，我急需一种自动程序来执行以上步骤，因此诞生了这个仓库。\n\n## 运行环境\n\n为了运行代码，你需要以下环境且可执行文件必须在`PATH`中：\n\n- 任意版本的node\n- 任意版本的ffmpeg\n- 任意版本的7zip\n\n程序未做版本兼容测试，开发过程任何测试都以我自己电脑上的版本为准，由于没有调用一些深层接口，因此在版本问题出现前可以认为没有版本问题，也就是任意版本都能运行。\n\n## 使用方式\n\n假设你要转码的视频目录是`c:/path/of/video`，代码目录是`c:/path/of/code`，则按以下顺序运行命令，js在代码目录运行，bat在视频目录运行，且需要运行`chcp 65001`将命令行编码改为`utf-8`，否则会报错。\n\n- `node ffprobe.js c:/path/of/video`: 在视频目录生成`data.json`、`1_meta.bat`、`2_rename.bat`、`4_undorename.bat`和`5_7zip.bat`。\n- `1_meta.bat`：获取元数据并保存在`meta.txt`中，脚本开头会关闭回显，结束后开启回显，元数据以80个等号分割，方便阅读。\n- `node ffmpeg.js c:/path/of/video`：在视频目录生成`3_ffmpeg.bat`并将码率相关数据写入`data.json`。\n- `node preview.js c:/path/of/video`：生成`preview.html`，展示压缩比。\n- `2_rename.bat`：重命名文件，在文件名后加下划线。\n- `3_ffmpeg.bat`：开始转码，此使只需要等待转码完成或报错就行，视频多总时长多文件大的情况下建议挂机或者睡觉。\n- `4_undorename.bat`：用来撤销重命名，删除文件名后的下划线。\n- `5_7zip.bat password`：加密打包转码后的文件，必须输入密码参数。\n\n当视频目录下有`data.json`时，`ffprobe.js`会读取该文件中的文件名而不是遍历视频目录。`ffmpeg.js`可以有一个额外的基准码率参数，运行时会以该码率为基准计算其他分辨率的码率，如果新码率大于旧码率则跳过该文件。`preview.js`也有一个参数，当压缩率大于此值会输出警告。\n\n`preview.js`可以在转码前运行也可以在转码后运行，当视频目录里没有`data.txt`时视作没有开始转码，此使展示的是预期码率和压缩率，否则展示实际结果。`preview.js`生成以下格式的html：\n\n| 文件名 | 分辨率 | 时长 | 文件大小（字节） | 新文件大小（字节） | 码率（kb/s） | 新码率（kb/s） | 压缩比 |\n| ----- | ----- | --- | ------------ | ---------------- | ----------- | ------------ | ----- |\n| input video.mp4 | 1920x1080 | 01:00:00.00 | 10240 | 1024 | 3000 | 1000 | 10% |\n| 合计   | - | 01:00:00.00 | 10240 | 1024 | - | - | 10 |\n\n没有转码时，压缩率用码率计算，转码后，压缩率用文件大小计算。\n\n所有生成的脚本除了`1_meta.bat`会将日志（结果）输到文件外，其余脚本都不会将日志输出到文件。所有脚本都在`c:/path/of/video`下直接执行，`3_ffmpeg.bat`有可选参数基准码率，后文会介绍，`5_7zip.bat`需要密码参数，比如`5_7zip.bat 123456`，123456就是密码，其余脚本都不需要参数。\n\n## 相关命令\n\n假设文件名是`input video.mp4`，以下是具体执行的命令，为防止文件名中的空格切分命令，所有文件名都是用双引号包裹：\n\n- `1_meta.bat`：`ffprobe -hide_banner \"input video.mp4\"\u003e\u003emeta.txt 2\u003e\u00261`\n- `2_rename.bat`：`rename \"input video.mp4\" \"input video_.mp4\"`\n- `3_ffmpeg.bat`：`ffmpeg -hide_banner -y -i \"input video_.mp4\" -c:a aac -c:v av1_nvenc -b:v 3000k \"input video.mp4\"`\n- `4_undorename.bat`：`rename \"input_video_.mp4\" input_video.mp4\"`\n- `5_7zip.bat`：`7z a \"input video.mp4.7z\" -mx0 -p%1% -mhe \"input video.mp4\"`\n\n若`ffmpeg.js`运行时没有基准码率参数，则转码时不会指定码率而是由ffmpeg自己决定码率。\n\n## 基准码率\n\n`ffmpeg.js`会从`bitrate.json`中引入一个数组，该数组如下：\n\n```json\nconst bitrateData = [\n  { pixels: 102240, magnification: 0.2, diff: 128160 }, // 240P 426x240 200\n  { pixels: 230400, magnification: 0.4, diff: 179520 }, // 360P 640x360 400\n  { pixels: 409920, magnification: 0.5, diff: 511680 }, // 480P 854x480 500\n  { pixels: 921600, magnification: 0.8, diff: 1152000 }, // 720P 1280x720 800\n  { pixels: 2073600, magnification: 1, diff: 1612800 }, // 1080P (基准) 1920x1080 1000\n  { pixels: 3686400, magnification: 1.78, diff: 4608000 }, // 2K 2560x1440 1780\n  { pixels: 8294400, magnification: 4, diff: 24883200 }, // 4K 3840x2160 4000\n  { pixels: 33177600, magnification: 8, diff: null } // 8K 7680x4320 8000\n];\n```\n\n这个json就是码率参考，用来处理同目录下不同尺寸不同尺寸的文件，基准码率就是1080p的码率，然后用基准码率乘以倍率magnification就是对应分辨率的码率。如果视频大小不是标准大小，则使用最近的码率。如果没有加码率参数，则由ffmpeg自己决定分辨率，此使，`5_7zip.bat`会变成`ffmpeg -hide_banner -y -i \"input video_.mp4\" -c:a aac -c:v av1_nvenc \"input video.mp4\"`。\n\n上面的倍率是我问ai得到的，目前我没有一个很好的方法来根据分辨率来计算能保留画质的最小码率，只能使用分段函数做简单计算。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytechaocai%2Fffmpeg-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbytechaocai%2Fffmpeg-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbytechaocai%2Fffmpeg-js/lists"}