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

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

文章特色图片

在本教程中, you will learn how to write Bash scripts that run on Ubuntu and encode and package multiple 文件s to HLS/DASH output using open-source tools FFmpeg and Bento4. You’ll learn how to run the scripts directly on folders of content and to set up a watch folder operation. 虽然这是初学者级别的东西, 熟悉FFmpeg和Bento4将有所帮助, as will some experience with batch processing and working on the command line in Linux or Windows.

您可以下载本文中展示的所有脚本 在这里.

的 operative catchphrase for this tutorial is "Don’t let perfect be the enemy of the good,”,, 以各种形式, 是伏尔泰说的吗, 孔子, 莎士比亚, 马克·扎克伯格(“完成比完美好”). This is my way of saying that I know the scripts detailed 在这里in aren’t the most elegant or highly optimized, 特别是图5所示的脚本. 然而, 他们完成工作,应该是相对简单的, 即使是Bash新手, 理解和实施.

为什么Ubuntu?

I spend most of my time on Windows workstations but created this tutorial using Ubuntu because Bash scripting is simpler and more powerful than Windows command-line programming. 我使用Ubuntu是因为它是免费的, 它安装在我办公室的几台电脑上, and it’s easy to create a dual boot to run Windows and Ubuntu on the same computer. Ubuntu also has an easy-to-use GUI that simplifies operations like moving 文件s from one drive or folder to another and a simple text editor that’s ideal for creating Bash scripts.

In this regard, you may be able to run these scripts on the Linux Bash Shell 现在可以在Windows 10上使用, but getting a Linux GUI running on Windows 10 to access 文件 and text editor operations seemed challenging, 所以我没有尝试. 如果你周围有一台旧的工作站, it should be relatively simple to install Ubuntu and have it function as your encoding server.

要安装

Ubuntu有多个LTS(长期支持)版本. 此时,我将安装版本18.04辆轻坦而不是16辆.04 LTS,因为它更新而且通常更快, 尽管这里的指令应该适用于两个版本. 一定要安装桌面版的Ubuntu, 它包含GUI, 而不是服务器版本, 这不会(尽管您可以稍后添加).

在Windows机器上安装Ubuntu, 你将下载一个可启动版本的Ubuntu到一个USB驱动器上, 从那个驱动器启动计算机, 然后安装Ubuntu. 有许多教程向您展示如何使用,其中包括一个 go2sm.com/ubuntuinstall. If you want to create a dual boot system, be sure to find a tutorial that includes this.

安装Ubuntu之后,你必须安装ffmpeg4.X,你可以学习怎么做 go2sm.com/ffmpeginstall和Bento4,记录在 go2sm.com/ubuntubento4. 这两种情况, 一旦你安装了程序, you can call it from a command line or script from any drive location; you don’t need to include the specific address to the executable.

Ubuntu终端导航基础

Though you can perform most of the 文件-related heavy lifting via Ubuntu’s GUI Files application, you’ll have to navigate the Terminal using keyboard commands to execute Bash scripts. 有四个命令你绝对需要知道:

松材线虫病-显示当前工作目录
ls-列出当前工作目录中的文件和文件夹
cd 目录– to change to the listed 目录; capitalization matters
cd ..-导航到较低的目录

中使用了其中几个命令 图1.

几个额外的功能将有助于加快操作速度. 第一个, 和Windows一样, pressing the up and down arrow keys shuttles through batch commands that you’ve used before in the current Terminal session. So, 如果您正在调试批处理文件, you don’t have to type it in each time you want to run it; just press the up arrow until it appears and click Enter.

第二个有用的功能与Tab键有关, 哪个尝试完成您键入的任何命令. 假设你要从根文件夹切换到视频文件夹. Assuming that t在这里’s only one folder that starts with capital V, if you type cd V 并按Tab键,命令将自动补全到 cd /视频. 按Enter键导航到该文件夹. You can use the same procedure to complete the names of batch scripts in Terminal, 节省一些按键并避免错误.  

很明显, 还有许多其他有用的终端命令, 您还可以下载一个方便的命令行参考备忘单 go2sm.com/terminalcheats. 在那个URL上有两个参考表格, 但当我检查时,它们是相反的, so click the Ubuntu Reference to download the Linux Command Reference (which is the one you want) and vice versa.

您的第一个Bash脚本

让我们避开典型 回声= hello world  第一个Bash脚本,然后直接跳转到一个有用的、与ffmpeg相关的脚本. 情况是这样的. I created a 13-video PowerPoint-based tutorial series in Premiere Pro and exported the 720p 文件s at 10Mbps because Adobe Premiere Pro doesn’t offer constant rate factor (CRF) encoding. I want to re-encode these 文件s using CRF encoding because it will dramatically reduce the data rate and upload time with no visible quality hit.

FFmpeg命令很简单:

输入.Mp4 -c:v libx264 -crf 23 input_crf.mp4

This tells FFmpeg to input the 文件, encode using the x264 codec to a CRF value of 23, and to append _crf 到输入文件名,以命名输出文件. 64Kbps单声道AAC音频轨道将自动复制过来, 但我也可以配置音频输出. It would be simple to create a Windows batch 文件 with entries for each 文件, but that’s both time-consuming and error-prone and painful to edit if you want to perform the same operation for different 文件s in the future. 在Bash中要容易得多. 以下是步骤.

1. 打开终端.

有许多不同的方式打开终端应用程序. 例如,在图1中,我在Ubuntu 18中打开终端.在左侧的收藏夹栏中单击它. Once in Terminal, I run through some navigational commands and then create the Bash script.

  • 第一个命令是 松材线虫病, which shows the present working 目录, or the 目录 that I’m currently in.
  • 然后输入lsto列出该目录下的文件和文件夹.
  • To navigate to the Videos folder and perform these batch operations, I type cd /视频 (or cd V 按Tab键自动补全).

Ozer FFmpeg Bash基本命令

图1. 码头工作  在Ubuntu 18中.04

2. 创建Bash文件.

要创建Bash脚本,我输入 触摸crf.sh,它使用该名称创建一个文件. 注意,从技术上讲,您不必使用 .sh 这是Bash文件的分母,但这是常见的做法. 然后我输入 ls 再次列出视频文件夹中的文件, 包括我要转换的视频文件和 crf.sh 我刚刚创建的脚本文件.

3. 创建Bash脚本.

Create the script in a basic text editor since it won’t insert any funky characters like word processors do. In 图2我开口了 crf.sh 在Ubuntu文本编辑器中单击右键 crf.sh 选择“用文本编辑器打开”. 图2的底部显示了Text Editor应用程序.

  1. 第1行—所有Bash文件都需要以所谓的shebang或 #!/bin/bash.这会告诉Ubuntu这是一个Bash脚本.
  2. Line 3 – We’re using a "for loop" for this Bash 文件 that has two components. 首先,确定要处理哪些文件. 第3行显示的命令将这些文件标识为带有 .我将在其中运行脚本的文件夹中设置mp4扩展名.
  3. 的n we add "do" in line 4 and type the FFmpeg command string to run on those 文件s on line 5.
  • -i string identifies the 文件 to be encoded; we add the quotes around the word 文件 and the dollar sign 美元(“文件”) to indicate that we’re retrieving the list of 文件s in the folder, not simply using the word 文件 在脚本中.
  • 在命令字符串末尾的文件名中执行相同的操作 %.* added to strip the extension from the output 文件 name and add _crf to the 文件 name (" $[文件%.*} _crf”.mp4). 如果输入文件是 input文件.mp4,脚本将输出一个名为 input文件_crf.mp4.
  • 完成 第6行结束“for”循环.

工作在文件和Ubuntu文本编辑器

图2. 工作在文件和Ubuntu文本编辑器

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

如何使用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. 虽然你不能在Windows命令窗口中使用这些脚本, 你可以在Windows上使用微软PowerShell,在Linux和Mac上使用Bash. 在本教程中, 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: 的 six commands in this article help with essential tasks that crop up in any studio or encoding facility.

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

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

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

以以前的DIY文章为基础, 本文将引导您完成音频捕获, 用FFmpeg编码和包装, 然后通过您在上一篇文章中设置的Icecast服务器播放

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

FFmpeg might be a free download, but t在这里's still a cost that goes with it. Robert Reinhardt解释了为什么这种编码器并不适合每个公司.