-->
Save your FREE seat for 流媒体 Connect this August. 现在注册!

Discover the Six FFmpeg Commands You Can’t Live Without

文章特色图片

FFMPEG是一个免费的命令行实用程序,它是世界上大多数最大的云编码农场的引擎, 公营及私营. 但它也执行许多简单和必要的任务,突然出现在工作室或编码设施, particularly if you do performance or benchmark testing. In this article, I’ll show you how to perform six of these tasks.

开始

To get started, you can download FFmpeg for free from FFmpeg.org/download.html, 提供Windows可用的软件包, Mac, and multiple Linux flavors including Ubuntu, Debian, Fedora, 和红帽企业Linux. 虽然批处理命令的结构和操作在每个平台上是不同的, the FFmpeg command syntax is very similar for all.

Here’s the basic FFmpeg command line structure:

输入.mp4输出.mp4

Here’s an explanation for the commands:

Ffmpeg         

This calls the program; add drive and folder 信息 for ffmpeg.如果它不在您的路径中.

-i         

这标识输入文件.

input.mp4     

这是输入文件. FFmpeg can input most container formats natively, including MP4, .ts, MOV, AVI, Y4M, MKV, and many others.

output.mp4   

T这是输出文件. No 开关 necessary; just the file name.

Note that unless you identify an audio or video codec via 开关es, FFmpeg将使用每个容器格式的默认编解码器以及合理的编码参数. 对于MP4扩展,如果输入1080p文件,FFmpeg将使用H.264 video codec at about 9 to 10 Mbps, the AAC audio codec at around 130 Kbps, 关键帧间隔为250帧, 高调, 和介质的x264预设. 所有这些都是可定制的, 当然, 但如果你想做的只是创建一个可以从硬盘上播放的视频文件, 上面的命令就足够了.

注意,您可以 download all commands and the input test file 在本教程中使用. (请注意,当您单击此链接时,下载将自动开始.) 的 video is excerpted from a test video provided by Harmonic, Inc. (谐波公司.com).

Changing Container Formats Without Re-Encoding

假设您正在使用一个只接受MOV文件的应用程序,并且源文件位于MP4容器中(反之亦然)。. 您可以将文件输入到Adobe Premiere Pro并根据需要输出, but Premiere Pro can’t do that without re-encoding. This is easily done in FFmpeg with this command string:

输入.Mp4 v - c:副本 -c:拷贝输出.mov

的 new commands here are as follows:

v - c:副本       

视频解码器,收到. 这告诉FFmpeg将压缩的视频流复制到新文件中,而无需重新编码.

c:一份       

音频解码器,收到. 这告诉FFmpeg将压缩的音频流复制到新文件中,而无需重新编码.

类似的场景可能涉及MPEG-2容器格式的文件 .ts扩展. 在这里,您可能需要添加一个开关来处理传输流文件中的MPEG-2/4 ADTS头文件.

输入.ts 净水器:aac_adtstoasc v - c:副本 c:一份 outputfromts.mp4

已添加的交换机如下:

净水器:aac_adtstoasc

管理ADTS转换.

这种特殊的转换节省了大约2天的编码时间,而我不得不转换大约100天 .ts files into MP4 files for testing. 的 aac_ adtstoasc 开关 may not be necessary with more recent versions of FFmpeg, which may insert the 开关 automatically. 如果转换崩溃或转换文件中的音频不能正常播放,您就会知道您需要它. 或者,你可以把它留在里面.

Extracting a Section of a File Without Re-Encoding

Say you have a file and want to eliminate the first 30 seconds, or simply grab a 2-minute excerpt from the middle. 再一次。, 你可以将文件导入Adobe Premiere Pro,然后编辑你想要的内容, but that would involve another rendering and encoding cycle. 您可以使用下面所示的命令在FFmpeg中快速而简单地完成此操作.

的 Harmonic test file is about 12 seconds long. 该命令在文件中查找5秒,并在不重新编码的情况下摘录接下来的3秒.

Ffmpeg -ss 00:00:05 -i input.mp4 -t 00:00:03 v - c:副本 c:一份 excerpt.mp4

-ss 00:00:05

在文件中查找5秒.

-t 00:00:05    

提取文件的持续时间. 如果你把这个开关关着, FFmpeg将在提取的文件中包含从查找点到文件末尾的内容.

请注意,FFmpeg似乎在文件中的关键帧周围工作,因此结果通常不是帧精确的. This doesn’t matter in most instances, 但是,如果您确实需要从您的摘录文件中包含或排除特定的持续时间或特定的框架, 这种方法可能行不通. In these cases, you’re better off using your video editor.

如果您只想提取文件中的视频而不提取音频,请添加 -an 开关. 若要仅提取音频,请添加 -vn 开关. Note that the MP4 container can hold audio-only files, 因此,当从MP4源生成纯音频文件时,您不需要更改容器格式.

转换为原始格式

Some file analysis tools only input raw YUV or Y4M files, so you’ll have to convert your encoded files to YUV for input. 除了, when working with newer codecs like AV1, most tools can’t input the encoded file natively; again, you’ll have to convert to YUV or Y4M.

What’s the difference between YUV and Y4M? 两者都是未压缩格式, but the YUV file header doesn’t contain resolution, 帧率, 或其他与文件相关的信息. 要将YUV文件输入到程序中,通常需要指定一个或多个参数. In contrast, Y4M files contain this data, simplifying input.

For example, to input a YUV file like ConstructionField.yuv into the Moscow State University 视频质量 Measurement Tool, 您必须指定分辨率和颜色信息,如上面所示 图1. 使用Y4M文件, 这个数据在文件头中, 所以你可以跳过这一步, both in the user interface and in the batch command string. Basically, if the tool you’re working with can accept Y4M files, use that format; if not, use YUV.

图1. 输入YUV文件到莫斯科国立大学视频质量测量工具强制您指定分辨率和其他文件相关的数据. Y4M files contain this in the header and input normally

Converting to either format is simple using this command:

输入.mp4 -pix_fmt yuv420p -vsync 1 output.y4m

To produce a YUV file, simply use that extension. Here’s an explanation for the new commands:

pix_fmt yuv420p      

这设置了文件格式,YUV420p是8位文件最常用的格式. If you’re working with 10-bit formats, 来看看这段对话 堆栈交换.

-vsync 1        

这告诉FFmpeg保持输入和输出文件之间的音频/视频同步. I’m not 100% sure it’s necessary in every instance, but long ago it cured a sync issue for me, 从那以后我就一直把它留在家里.

Scaling Compressed Files When Converting to YUV/Y4M

这是一个非常常见的用例,当在低于原始分辨率的压缩文件上计算客观质量度量时. 例如,假设您想计算从1080p文件编码的720p文件的VMAF评级. 使用大多数工具, 您必须将编码文件缩放回1080p,以便与源文件进行逐像素匹配.

为此,使用与上面所示相同的基本命令,添加两个新开关.

输入.mp4 -s 1920x1080 -sws_flags兰索斯 -pix_fmt yuv420p -vsync 1 output.y4m

Here’s an explanation for the new commands:

-s 1920x1080           

This tells FFmpeg to scale back to 1080p.

-sws_flags兰索斯 

This tells FFmpeg to use the Lanczos method of scaling

我使用这种方法是因为它是NVIDIA在将视频从低分辨率缩放到高分辨率时使用的主要缩放技术, and because when performing quality measurements, 我试图预测人们在电脑或智能电视上观看视频时的质量. Though there are higher-quality scaling methods, they’re probably too slow for use on most display devices, 所以我坚持买兰佐斯.

就是这样. If you’ve never worked in the command line, you’ll definitely find yourself at a disadvantage, 但是花一两个小时学习基本的命令行会让你跟上. If you frequently experiment with encoding and quality metrics, you’ll find the command line and FFmpeg absolutely essential, 并且这种组合极大地简化并加速了您的测试和分析工作流程.

[This article appears in the June 2019 issue of 流媒体杂志 as "Six FFmpeg Commands You Can’t Live Without."]

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

How to Script for FFmpeg Using PowerShell and BASH

FFmpeg被设计为视频和音频录制的跨平台解决方案, 转换, and streaming using simple static command lines. 在命令字符串中使用变量和“for循环”简化了现有脚本的重用,并有助于自动化它们的操作. While you can't use these scripts in the Windows Command window, 你可以在Windows上使用微软PowerShell,在Linux和Mac上使用Bash. 在本教程中,您将学习如何使用PowerShell和Bash创建和运行这样的脚本.

如何编码与FFmpeg 5.0

Rather than focusing on random tasks, 本教程将带您了解使用最新版本的FFmpeg进行编码的基础知识.

How to Automate FFmpeg and Bento4 With Bash Scripts

With just a few beginner-level scripts, 您可以使用开源工具将多个字段编码和打包为HLS和DASH输出.

Time to Start Testing: FFmpeg Turns 4.0和增加AV1支持

AV1 delivers equivalent quality to HEVC, but with a lower data rate. 但就目前而言,进展缓慢. A five-second clip took 23 hours and 46 minutes to encode.

DIY: Live Audio Streaming Using Icecast with FFmpeg

以以前的DIY文章为基础, this installment will walk you through capturing audio, 用FFmpeg编码和包装, 然后通过您在上一篇文章中设置的Icecast服务器播放

Evaluation Crowns FFmpeg the King of the H.264编码器

FFmpeg可能是免费下载的,但仍然需要付费. Robert Reinhardt解释了为什么这种编码器并不适合每个公司.

提及的公司及供应商