-->
保存您的免费座位流媒体连接今年八月. 现在注册!

如何自动化FFmpeg和Bento4与Bash脚本

文章特色图片

4. 运行脚本.

You run the Bash script from Terminal using the syntax shown on the top line of 图3 (./ crf.sh). 如图所示, 然而, 如果您试图在未授权的情况下运行脚本, 你会得到Permission denied错误.

As shown on the next input line, you make the file executable using this command:

 Chmod +x CRF.sh

然后,再次运行该脚本 (./ crf.sh), and it will start FFmpeg and iterate through all the files in the folder. You’ll have to make each Bash script executable using this command sequence.

授权并运行批处理文件

图3. 授权并运行批处理文件

该脚本在不到一个小时的时间内完成了所有13个文件. 图4 shows two of the CRF files compared to the original with a data rate reduction as high as 83% with no impact on quality. 对于本质上的两行代码来说还不错.

创建了13个文件,文件大小减少了83%

图4. The fruits of our Bash script: 13 files created with up to an 83% file size reduction with no impact on visual quality

使用Bento4

现在你已经掌握了基础知识, let’s take it up a notch and encode and package output for HLS and DASH distribution using FFmpeg and Bento4. 我将使用的脚本显示在 图5. As you can see on line 3, the script implements another "for" loop, so it processes all .Mp4文件位于它运行的文件夹中.

Bento4脚本

图5.  这是我们的第二个剧本,它更有野心.

作为概述, the most complicated part of the script relates to naming the output files and then recalling them for later use. 就主要部分而言,情况如下:

行1 - 4. 和以前一样,没什么新东西.

第5行. 我正在为每个输入文件创建一个单独的输出文件夹, which is necessary for the proper operation of the 看 folder function implemented next.

创建文件夹 mkdir 命令,并命名为"${文件%.*},即不带扩展名的源文件名. This folder designation precedes all files created in lines 6–15, 告诉FFmpeg和Bento4将这些文件保存到该文件夹中.

行6 - 10.  Here I’m creating an encoding ladder with three video resolutions, 1080p, 720p, 和360 p, 和一个单独的音频文件使用两段编码. You can insert as many additional FFmpeg commands as you would like and add additional rungs as needed on separate lines. 如果你习惯使用Windows,那么 / dev / null && \ ending for the first pass is a bit different, but pretty easy to mimic. 否则,FFmpeg命令很简单. 

请注意,该脚本将完全遍历每个文件, so if you have five MP4 files in the folder when you run the script, 最后将得到五个输出文件夹, all containing three-rung encoding ladders formatted for HLS/DASH distribution.

如上所述,美元的文件“输入”指定输入的每个文件,而“美元(文件%.*}_*" .mp4在末尾指定每个文件的输出文件名. Getting the names right is critical because the files created in lines 6–10 feed into the fragmented MP4 files created in lines 11–14, 哪一个输入到第15行创建的包装中. Again, note that I’m sending all files into the folder created in line 5.

11 - 14行. This series of commands uses the Bento4 mp4fragment utility to convert the MP4 files created by FFmpeg into fragmented MP4 files required for the mp4-dash 第15行中使用的实用程序. 语法很简单 Mp4fragment inputname输出名称; the hard part is pointing to the files in the subfolder for both input and output. 您可以阅读更多关于mp4fragment实用程序的信息 go2sm.com/bentomp4.

第15行. 这条线叫mp4-dash 应用程序,包括 ——hlsswitch 来创建HLS输出,并使用 ——output-dir 切换到将最终文件存储在适当的子文件夹中. Then you simply list the files created in lines 11–14 in the order you want them placed in the manifest file. 你可以阅读更多关于 mp4-dash at go2sm.com/mp4dash. Note that the applications in Bento4 are very functional and can do a lot more, including adding captions and DRM into your HLS/DASH outputs; as with FFmpeg, you can add as many switches as you need to implement the required features.

线16. 这将源文件移动到我创建的文件夹中, and it’s also necessary for the proper operation of the 看 folder function implemented next.

第17行. 完成. This tells the script that this "for" loop is complete and to start the operation on line 5 for any additional files in the folder.

要运行脚本, save it to the folder containing the MP4 files and run the script as you learned above. 将打包的文件输出上传到HTTP web服务器, and it should be immediately distributable to both DASH and HLS clients.

添加监视文件夹功能

So, batch encoding is nice, but you have to be there to execute the script. Now we’ll learn how to automate the encoding performed above via a 看 folder. 在操作上,图5 (ffmpeg_d.sh) runs on all MP4 files located in the folder when the script is run. 的脚本 图6 (看.sh)监视文件夹并运行ffmpeg_d.sh 将新文件复制到文件夹中时.

创建监视文件夹

图6.  创建监视文件夹

监视文件夹脚本使用了一个名为 inotifywait 来观察文件夹. To deploy the script, insert the folder to be 看ed in line 3 and the script to run in line 5. 第6行关闭在第3行创建的“while循环”.

You run the script as normal, first authorizing the script and then running it (图7). The script remains running until you cancel it using the CTRL + C keystroke combination. 

运行监视脚本并创建监视文件夹

图7. 运行监视脚本并创建监视文件夹

在我的测试中,唯一的主要异常是ffmpeg_d.shscript didn’t encode files added to the folder when it was already running. 如果你把视频A添加到观看文件夹,然后 ffmpeg_d.sh 开始跑步, 然后在编码完成之前添加了视频B, 视频B不会立即开始编码. When you add video C to the folder, 然而, the 看 folder script will execute ffmpeg_d.sh 它会对视频B和视频C进行编码.

使用这些脚本, it should be relatively easy to create your own in-house encoding station simply by installing Ubuntu. 在本版本的单独教程中, I detail how to start an Amazon Machine Image running Ubuntu in Amazon Web Services. 这些脚本也应该在这些机器上运行, allowing you to create an ad hoc encoding farm to encode any number of files quickly and cheaply.

流媒体覆盖
免费的
合资格订户
现在就订阅 最新一期 过去的问题
相关文章

如何使用PowerShell和BASH为FFmpeg编写脚本

FFmpeg was designed as a cross-platform solution for video and audio recording, 转换, 以及使用简单的静态命令行进行流式传输. Using variables and "for loops" in a command string simplifies the reuse of existing scripts and helps automate their operation. While you can't use these scripts in the Windows Command window, you can use Microsoft PowerShell in Windows and Bash on Linux and the Mac. In this tutorial, you'll learn how to create and run such scripts with PowerShell and Bash.

如何编码与FFmpeg 5.0

而不是专注于随机的任务, this tutorial will walk you through the fundamentals of encoding with the latest version of FFmpeg.

发现你不能没有的六个FFmpeg命令

任何进行性能或基准测试的人, please take a look: The six commands in this article help with essential tasks that crop up in any studio or encoding facility.

开始测试的时间:FFmpeg第4轮.0和增加AV1支持

AV1 delivers equivalent quality to HEVC, but with a lower data rate. 但就目前而言,进展缓慢. 一段5秒的视频编码耗时23小时46分钟.

DIY:现场音频流使用冰播与FFmpeg

以以前的DIY文章为基础, 本文将引导您完成音频捕获, 用FFmpeg编码和包装, then playing out through the Icecast server you set up in the last article

评价加冕了H的国王.264编码器

FFmpeg might be a free download, but there's still a cost that goes with it. Robert Reinhardt explains why this encoder isn't right for every company.