ffmpeg is such an amazing tool, looks like it's for video what ImageMagick is for images. An all-powerful all-formats wicked cool command-line tool.
ffmpeg是一个了不起的工具,它看起来像用于视频的ImageMagick用于图像的视频。 功能强大的全格式邪恶酷命令行工具。
This blog post is an introduction to some of the MP3 capabilities of ffmpeg. I'll use ffmpeg to transform a blog post into a podcast-ready mp3 file. If you continue to read this longish post, here's what you can expect to see:
这篇博客文章介绍了ffmpeg的某些MP3功能。 我将使用ffmpeg将博客文章转换为可播客的mp3文件。 如果您继续阅读这篇冗长的文章,那么可以看到以下内容:
- using PHP DOM 使用PHP DOM
- ffmpeg to convert to MP3 ffmpeg转换为MP3
- ffmpeg to crop (slice) an MP3 ffmpeg裁剪(切片)MP3
- glue together several MP3s 将几个MP3粘合在一起
- Mac's say command for TTS (text-to-speech)Mac的TTS语音指令(文字转语音)
PHP DOM获得一些博客内容 (PHP DOM to get some blog content)
This blog's feed is at http://phpied.com/feed. Let's create a small PHP script to extract the title, date and content of the last blog post. We'll feed these to Mac's say command to read then aloud.
该博客的供稿位于http://phpied.com/feed 。 让我们创建一个小PHP脚本,以提取最后一篇博客文章的标题,日期和内容。 我们会将它们提供给Mac的say命令以大声朗读。
Location of the feed:
提要的位置:
$file = 'http://phpied.com/feed/';
Load the feed into a DOM instance:
将提要加载到DOM实例中:
$dom = new DOMDocument;
$dom->loadXML(file_get_contents($file));
Access the node that contains the first item, i.e. the last blog post
访问包含第一项(即最后一篇博客文章)的节点
$post = $dom->getElementsByTagName('item')->item(0);
The title and a friendly-formatted date:
标题和格式友好的日期:
$title = $post->getElementsByTagName('title')->item(0)->textContent;
$date = $post->getElementsByTagName('pubDate')->item(0)->textContent;
$date = strtotime($date);
$date = date('F jS, Y', $date);
Get the content:
获取内容:
$ns = 'http://purl.org/rss/1.0/modules/content/';
$content = $post->getElementsByTagNameNS($ns,'encoded')->item(0)->textContent;
Strip out HTML tags and entities:
去除HTML标签和实体:
$content = strip_tags($content);
$content = html_entity_decode($content);
(Since this content will be read aloud, HTML tags and entities will make no sense. Here we cound've done better job by doing something more special for list, using ALT tags to replace images and so on...)
(由于将大声读取此内容,因此HTML标签和实体将毫无意义。在这里,我们通过对列表进行更特殊的处理,使用ALT标签替换图像等等来做得更好。)
echo $title, "\n\n", $date, "\n\n", $content;
OK, so now let's call the script from the command line and write the output to a file:
OK,现在让我们从命令行调用脚本并将输出写入文件:
$ php feed.php > thepost.txt
Here's the result - thepost.txt
结果是-thepost.txt
使用Mac的语音进行文字转语音 (Using Mac's say for text-to-speech)
You can make your Mac talk on the command line, like:
您可以在命令行上让Mac说话,例如:
$ say test
and it will say the word "test"
它会说“测试”一词
The say
command can also read text from text files and write to AIFF audio files. Let's read thepost.txt into an audio file.
say
命令还可以从文本文件读取文本并写入AIFF音频文件。 让我们将thepost.txt读入音频文件。
$ say -f thepost.txt -o thepost.aiff
Since I'll make this look like it's a part of an ongoing series of podcasts, I'll add some music and I also need a greeting and goodbye spoken text. So: $ say -o welcome.aiff Welcome to phpied.com podcast $ say -o thatsallfolks.aiff That was all for today, join us next time on... phpied.com
由于我将使它看起来像是正在进行的一系列播客的一部分,因此,我将添加一些音乐,并且还需要问候和再见的口头文字。 所以: $ say -o welcome.aiff Welcome to phpied.com podcast $ say -o thatsallfolks.aiff That was all for today, join us next time on... phpied.com
OK, so now I have three AIFF files:
好,现在我有了三个AIFF文件:
thepost.aiff
Thepost.aiff
welcome.aiff
Welcome.aiff
thatsallfolks.aiff
thatsallfolks.aiff
Now I want to add some music before/after the podcast. I took four loops from Garage Band's library. Here they are:
现在,我想在播客之前/之后添加一些音乐。 我从车库乐队的图书馆走了四个循环。 他们来了:
opener.mp3
opener.mp3
closer.mp3
close.mp3
breaking-news.mp3
Breaking-news.mp3
squeeze-toy.mp3
挤玩具.mp3
下一个? (Next?)
Now I have a bunch of audio files. All I need to do is merge them, glue them together into one MP3. Glueing MP3 will be as easy as simply concatenating the files, using cat
for example and them making a final pass through ffmpeg to correct dates and other meta data, so that the result looks like one single file, and not like a Frankenstein 🙂
现在我有一堆音频文件。 我需要做的就是将它们合并,然后将它们粘贴到一个MP3中。 粘贴MP3就像简单地串联文件一样简单,例如使用cat
,它们最终通过ffmpeg进行更正以更正日期和其他元数据,因此结果看起来像一个文件,而不像科学怪人🙂
In order for the concatenation to work, you only need to make sure all files are the same format, bitrate, etc.
为了使串联起作用,您只需要确保所有文件的格式,比特率等相同即可。
Let's choose 22050 Hz, mono for the result. This means always add the options:
让我们选择22050 Hz,单声道作为结果。 这意味着始终添加选项:
-ar 22050 -ac 1
to all calls to ffmpeg.
所有对ffmpeg的呼叫。
Let's get cracking.
让我们开始吧。
ffmpeg转换几乎任何东西 (ffmpeg to convert just about anything)
The simplest use of ffmpeg is to convert from one file format to another, for example AVI to MPEG, WMV to FLV and what not. This is done like this for example:
ffmpeg的最简单用法是将一种文件格式转换为另一种文件格式,例如AVI转换为MPEG,WMV转换为FLV,而没有转换。 例如,这样做是这样的:
$ ffmpeg -i input.avi output.flv
ffmpeg获取文件信息 (ffmpeg to get file information)
It's useful to know what type of file we're dealing with, you can do this simply by omitting the output file:
知道我们正在处理哪种类型的文件很有用,您只需省略输出文件即可做到这一点:
$ ffmpeg -i input.avi
Let's check out one of the Garage Band loops:
我们来看看车库带循环之一:
$ ffmpeg -i opener.mp3
FFmpeg version..... (more ffmpeg information)
Input #0, mp3, from 'opener.mp3':
Duration: 00:00:12.6, start: 0.000000, bitrate: 191 kb/s
Stream #0.0: Audio: mp3, 44100 Hz, stereo, 192 kb/s
Must supply at least one output file
Pretty good quality, more than I need. Plus, for some reason Garage Band added silence at the end of the files I exported, so let's cut it off.
相当不错的质量,超出了我的需求。 另外,由于某些原因,车库乐队在我导出的文件的末尾添加了静音,因此我们将其切断。
ffmpeg裁剪文件 (ffmpeg to crop files)
I want to remove trailing 5 seconds or so of each Garage Band loop. Here goes:
我想删除每个“车库乐队”循环的后5秒左右。 开始:
$ ffmpeg -i breaking-news.mp3 -ac 1 -ar 22050 -ss 0 -t 6 breaking-news-ok.mp3
$ ffmpeg -i opener.mp3 -ac 1 -ar 22050 -ss 0 -t 8 opener-ok.mp3
$ ffmpeg -i closer.mp3 -ac 1 -ar 22050 -ss 0 -t 33 closer-ok.mp3
$ ffmpeg -i squeeze-toy.mp3 -ac 1 -ar 22050 -ss 0 -t 2 squeeze-toy-ok.mp3
-i
is the input file -ac
is the number of channels (1 for mono) -ar
is the rate, -ss
is start, -t
is length.
-i
是输入文件-ac
是通道数(单声道为1) -ar
是速率, -ss
是开始, -t
是长度。
Now you can see how the meta information for the opener.mp3 has changed:
现在,您可以查看opener.mp3的元信息如何更改:
$ ffmpeg -i opener-ok.mp3
Input #0, mp3, from 'opener-ok.mp3':
Duration: 00:00:08.1, start: 0.000000, bitrate: 63 kb/s
Stream #0.0: Audio: mp3, 22050 Hz, mono, 64 kb/s
将AIFF转换为MP3 (Convert AIFF to MP3)
Now let's convert the AIFF files from our TTS say
command to MP3, keeping the same 22050 mono rate:
现在,将AIFF文件从TTS say
命令转换为MP3,并保持相同的22050单声道速率:
$ ffmpeg -i thepost.aiff -ac 1 -ar 22050 thepost.mp3
$ ffmpeg -i welcome.aiff -ac 1 -ar 22050 welcome.mp3
$ ffmpeg -i thatsallfolks.aiff -ac 1 -ar 22050 thatsallfolks.mp3
Here are the new MP3s:
这是新的MP3:
thepost.mp3
thepost.mp3
welcome.mp3
welcome.mp3
thatsallfolks.mp3
thatsallfolks.mp3
与猫和ffmpeg粘在一起 (Glue the pieces with cat and ffmpeg)
Now, last stage, let's glue all the pieces with cat
which simply means append the next file at the end of the previous.
现在,进入最后一个阶段,让我们用cat
粘合所有片段,这只是意味着将下一个文件追加到前一个文件的末尾。
$ cat breaking-news-ok.mp3 welcome.mp3 opener-ok.mp3 thepost.mp3
squeeze-toy-ok.mp3 thatsallfolks.mp3 closer-ok.mp3 > pieces.together
Then make these pieces a proper MP3 file
然后将这些片段制作为正确的MP3文件
$ ffmpeg -i pieces.together final.mp3
Well, that's all folks, here's the final result: final.mp3
好吧,这就是所有人,这是最终结果: final.mp3
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息
翻译自: https://www.phpied.com/blog-to-podcast-with-ffmpeg/