-->
为五月的纽约流媒体保留座位吧. Register Now!

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

文章特色图片

4. Run the Script.

You run the Bash script from Terminal using the syntax shown on the top line of Figure 3 (./crf.sh). 如图所示, however, 如果您试图在未授权的情况下运行脚本, 你会得到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.

授权并运行批处理文件

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

该脚本在不到一个小时的时间内完成了所有13个文件. Figure 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%

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

Working With Bento4

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

Bento4 Script

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

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

Lines 1–4. As before, nothing new.

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

创建文件夹 mkdir 命令,并命名为"${file%.*},即不带扩展名的源文件名. 这个文件夹名称位于第6-15行创建的所有文件之前, 告诉FFmpeg和Bento4将这些文件保存到该文件夹中.

Lines 6–10.  在这里,我创建了一个具有三种视频分辨率的编码阶梯, 1080p, 720p, and 360p, 和一个单独的音频文件使用两段编码. 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命令很简单. 

请注意,该脚本将完全遍历每个文件, 所以如果你在文件夹里有5个MP4文件当你运行脚本的时候, 最后将得到五个输出文件夹, all containing three-rung encoding ladders formatted for HLS/DASH distribution.

As above, the "$file“输入”指定输入的每个文件,而“$[file%.*}_*" .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.

Lines 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.

Line 15. This line calls the 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.

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

Line 17. Done. 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.

To run the script, 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 watch folder. 在操作上,图5 (ffmpeg_d.sh) runs on all MP4 files located in the folder when the script is run. The script in Figure 6 (watch.sh)监视文件夹并运行ffmpeg_d.sh 将新文件复制到文件夹中时.

创建监视文件夹

Figure 6.  创建监视文件夹

监视文件夹脚本使用了一个名为 inotifywait to watch the folder. To deploy the script, insert the folder to be watched 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 (Figure 7). The script remains running until you cancel it using the CTRL + C keystroke combination. 

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

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

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

Using these scripts, 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.

Streaming Covers
Free
合资格订户
Subscribe Now Current Issue Past Issues
Related Articles

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

FFmpeg was designed as a cross-platform solution for video and audio recording, conversion, 以及使用简单的静态命令行进行流式传输. Using variables and "for loops" in a command string simplifies the reuse of existing scripts and helps automate their operation. 虽然你不能在Windows命令窗口中使用这些脚本, 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 and Adds AV1 Support

AV1提供与HEVC相同的质量,但数据速率较低. 但就目前而言,进展缓慢. 一段5秒的视频编码耗时23小时46分钟.

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

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

评价加冕了H的国王.264 Encoders

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.