ソフトウェア
 Computer >> コンピューター >  >> ソフトウェア >> ソフトウェア

ffmpeg のガイド - マルチメディアのワンストップ ショップ

何年にもわたって、何らかの形でマルチメディア編集を行う必要がある機会が何度もありました。オーディオ、ビデオ、名前を付けます。 Youtube チャンネル用の面白くないクリップの作成、音楽の抽出または変換、字幕の埋め込みなど、何度も何度もコマンドラインで ffmpeg を使用し、この気取らないプログラムの機能に微妙に驚かされながら、常にそのプロセスを楽しんでいます。 .

そこで、さまざまなタスクと機能をリストした ffmpeg に関するより包括的なガイドを作成し、それらを実際の例と使用例にラップする必要があるのではないかと考えました。 2008 年の Flash 編集チュートリアルで初めて ffmpeg について言及しましたが、新鮮で最新の記事を書くときが来ました。これはコマンド ラインになり、オタクっぽくなりますが、楽しいものになるはずです。私に従ってください。

基本

Ffmpeg は、ほぼすべての Linux ディストリビューションで利用でき、Windows および Mac 用のビルドもあります。何ができるか知りたい場合は、引数なしで、またはオプションの -hide_banner フラグを指定して ffmpeg を実行することで、その機能を確認できます。また、メイン プログラムとその多くのサブコマンドのいずれかに対して help オプションを使用することもできます。目もくらむばかりの特徴に圧倒されることもあるので、ゆっくりと、しかし確実に進めていきます。それから、かなりきちんとした実際のドキュメントを忘れないでください。

変換とコード変換

最も一般的なタスクの 1 つは、ファイルをある形式から別の形式に変換することです。 FLV クリップからオーディオ ファイルを抽出する方法の例を覚えていますか?同じこと。ただし、カメラで録画したビデオを MP4 などに変換したり、OGG ファイルを MP3 などに変換したりすることもできます。無限のさまざまなシナリオがあり、ffmpeg でサポートされているモードとフォーマットも同様に無限にあります。通常、次のことを行います:

  • 入力ファイルを指定してください。
  • 目的のビデオおよびオーディオ コーデックを指定します。
  • 出力ファイルを指定してください。

ビデオ ストリームを操作したくない場合は、コピー コーデックを使用できます。同じことがオーディオにも当てはまります。実際のエンコーディングを変更せずに MOV から MP4 に移行するなど、コンテナを変更する場合は、このオプションを使用できます。

ffmpeg -i MVI_1348.MOV -c:v コピー -c:a コピー new.mp4

注:すべての形式がすべてのコーデックとオプションをサポートしているわけではないため、これがすべての場合に機能するとは限りません。いいね:

[mp4 @ 0x55a21519fb20] ストリーム #0 でコーデック vp8 のタグが見つかりませんでした。コーデックは現在コンテナーでサポートされていません
出力ファイル #0 のヘッダーを書き込めませんでした (コーデック パラメータが正しくありませんか?):引数が無効です
ストリームマッピング:
ストリーム #0:0 -> #0:0 (コピー)
ストリーム #0:1 -> #0:1 (コピー)
最後のメッセージを 1 回繰り返し

特定のストリーム (オーディオ、ビデオ、字幕、またはデータ) のみを抽出する場合は、他のストリームの処理を抑制することができます (操作に必要な時間と CPU サイクルを節約するため)。たとえば、結合されたオーディオ ビデオ メディア ファイルからのオーディオのみが必要な場合は、-vn (ビデオなし) オプションを使用できます。

ffmpeg -i MVI_1348.MOV -vn -ar 44100 -ab 192k サウンドのみ.mp3

この優れたユーティリティにより、物事を行う方法に多くの柔軟性がもたらされます。たとえば、codec:v、vcodec、および c:v は同じもので、どれでも使用できます。上記のようにオーディオ出力のみが必要な場合、ffmpeg は推測し、-vn も意図していると想定します。 -f フラグを使用して出力形式を強制することもできますが、ツールはコマンドの最終結果を推測しようとします。たとえば、-f mp3 または acodec mp3 を使用できます。

コーデックが異なれば、オプションも異なります。たとえば、オーディオ ファイルの場合、オーディオ サンプリング レートとオーディオ ビットレート、チャンネル数などを指定できます。一部のオプションをスキップすると、コマンドが失敗するか、デフォルトが使用されます。

規模と品質

もう 1 つの一般的なタスクは、ビデオのサイズ (解像度) とストリームの品質を変更することです。 Ffmpeg では、さまざまな方法でこれを行うことができます。比率を強制する (そしてデフォルトの縦横比を破る) か、縦または横の値とマジック修飾子を指定して維持することができます。ビデオの境界線をトリミングしたり、ストリームを再エンコードしたりできます。この段階では例が保証されます。

ffmpeg -i input.avi -c:v mpeg4 -vtag xvid -qscale:v 3 -c:a libmp3lame -qscale:a 4 output.avi

ここには何がありますか?品質を 3 (qscale) に設定した MPEG4 ビデオ ストリームを使用して、AVI 入力ファイルをトランスコードしています。一部のコーデックでは、品質の範囲は 1 ~ 31 で、1 が最高です。いくつかの一般的な入力ファイルでテストしたところ、最高の品質では通常、サイズが約 2 倍になりましたが、3 または 4 では約 20 ~ 30% 削減されました。 Vtag xvid は MPEG-4 固有のオプションです。同様に、オーディオには LAME を使用しています。

ffmpeg -i MVI_1348.MOV -vfilters scale=1280:-1 -c:a copy scaled.mp4

This example shows video scaling (you can use -vf or -vfilters), with the aspect ratio being preserved through the -1 qualifier. We don't change the quality of the stream. The audio stream is simply being copied into the output container. If your original video has a higher resolution than the stated one, then you will be downscaling, and if it's lower, then you will be upscaling.

Image extraction, combination, GIFs

Ffmpeg can also manipulate pictures, even though it's a media utility. But hey, pictures are media, and essentially, videos are moving pictures, right! Some of the tasks you'd want - extract frames from a video in order to create thumbnails (high-quality ones, too) rather than manually pausing video or creating hectic screenshots. You can also do the opposite - combine stills into a stopmotion video - or a full one. You can also convert videos to animated GIFs, which can be handy for dank memeism around the Web. Once again, we shall demonstrate with some ... dank examples.

ffmpeg -i input.mp4 -r 1 -f image2 thumb-%3d.png

We specify an input file, we specify the rate (in this case we want 1 image per 1 second of video), we specify the format (image2), and we specify the output format. In this case, we're doing something clever. If you've ever seen C language code, this will look familiar. We want the numbering sequence for the thumbnails to have three digits. You can choose any amount of digits, but ideally, you want more than the expected number of extracted images.

ffmpeg のガイド - マルチメディアのワンストップ ショップ

Then, combining images into a video is very similar:

ffmpeg -f image2 -i thumb-%3d.png stopmotion.mp4

You can also specify the rate (you want your video to be something like 24 or 30 FPS), but we will touch on this a bit more later in the guide, when we talk about making video playback slower and/or faster. And since we're talking about images, you can resize them too:

ffmpeg -i thumb-004.png -vf scale=400:-1 test.png

Animated GIFs are really interesting. Also rather simple to create. Just specify an input file and set the output to have a gif extension. Ffmpeg will auto-guess the rest and create nice, high-quality animated GIF for you.

ffmpeg -i MVI_6654.MOV test.gif

ffmpeg のガイド - マルチメディアのワンストップ ショップ

Finally, the opposite of GIFs, you may also want to have a video composed of a single image - a constant preview but then have something like music or a podcast playing in the background. Ffmpeg allows you to do this with a clever trick.

ffmpeg -loop 1 -i thumb-001.png -i sound-only.mp3 -c:v libx264 -shortest video-static-image.mp4

We provide two inputs, with the first being an image set to loop. We're also re-encoding the video. But since the image is set to loop indefinitely, we also need the shortest option, so the operation ends when the shortest clip ends, which would be the video in this case.

Splitting and joining videos

This is another useful scenario. If you have a very big clip, but you only need a few minutes or seconds, ffmpeg can cut out the necessary pieces. Likewise, you can combine multiple files into one stream - both video and audio. Ffmpeg lets you choose the desired start and end timestamps, so you have a lot of leeway in how you do handle the task.

-t duration record or transcode "duration" seconds of audio/video
-to time_stop record or transcode stop time
-fs limit_size set the limit file size in bytes
-ss time_off set the start time offset
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the
current time)

So, you may want to try something like:

ffmpeg -i MVI_1348.MOV -ss 4 -t 4 spliced.mp4

This command will extract 4 seconds from the input clip starting at time offset of 4 seconds into the clip. You can use short notation (4 indicates seconds) or use the full one hh:mm:ss.mmm. This gives you accuracy down to one millisecond, which ought to be more than enough for all practical usecases.

Joining files is a bit more complicated. Technically termed concatenation (concat), it works differently from the cutting option. You do not specify the desired parts you want to join on the command line, you put them into a file, using a very specific notation, and then read that file with ffmpeg.例えば:

ffmpeg -f concat -i list.txt -codec copy joined.mp4

The contents of the list.txt file will be:

file 'part-1.mp4'
file 'part-2.mp4'
file 'part-3.mp4'

Please note the usage of the word file and single quotation marks. You can use both absolute and relative paths. If you make a mistake in the file format, you will see an error:

[concat @ 0x555ca89c38c0] Line 1:unknown keyword 'part-1.mp4'
list.txt:Invalid data found when processing input

If you try to provide the list of files on the command line (rather than the text file):

part-1.mp4:Invalid data found when processing input

Embed and extract subtitles

Remember my VLC &subtitles article? Well, if you're not intimidated by the command line, you can do the same thing with ffmpeg. As a trivial example, say you have separate audio and subtitle files. Without any additional processing, you can just use the video filtering option for this:

ffmpeg -i input.avi -vf subtitles=subtitle.srt output.avi

A more complicated use case, the command below will take separate MP4 and subtitle files, and combine them into an output file with video transcoding. The -map options (showing twice, not an error) define the order the streams will be mapped into the output. But then there's a clever and somewhat confusing listing of codecs. Because we have multiple streams, we only want to manipulate the video part - but not audio or subtitles.

To that end, -c copy specifies all codecs should be copied, but using -c:v provides an override to this option, and the video stream will be transcoded. Since we're using the libx264 codec, we're also using Constant Rate Factor (CRF) option for variable bitrate, set to 23 (ranges from lossless 0 to lowest-quality 51) and the veryfast preset, which creates a relatively small output.

ffmpeg -i input.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mp4

Extracting subtitles takes the following form:

ffmpeg -txt_format text -i input.file out.srt

Speed up and slow videos

This is another interesting usecase. Like any human being, you may want to take any video, speed it up 2x and then add Benny Hill music, and there should be much rejoicing. You could also slow down action, and then, to spice things up, also add smoothing so the slowed sequence does not feel stilted.

For example, we may have our stop motion video - with only say 15-20 thumbnails, it takes only a second to play. So you may want to stretch it to say 5 seconds, but that means each frame will show for five times as long. Instead you could potentially "insert" extra frames to make it look like a smooth slo-mo. We did this with an interior design gallery back in the day. We use AviSynth and VirtualDub for this task.

The video speed filter works by changing the presentation timestamp (PTS) of each video frame. Simply put, fractional values speed up the video, multipliers higher than 1 slow it down. When you speed it up, ffmpeg will drop frames, but you can specify a higher framerate. To preserve the original, you can multiply the FPS in your input by the 1/PTS you're using. So if you double the speed of your 24FPS clip, you should use 48FPS rate.

ffmpeg -i input.mp4 -filter:v "setpts=0.5*PTS" output.mp4

ffmpeg -i input.mp4 -r 96 -filter:v "setpts=0.25*PTS" output.mp4

ffmpeg -i input.mp4 -filter:v "setpts=2*PTS" output.mp4

Smoothing videos is more complicated. I did have to consult the documentation. Essentially, for a video that's slowed down 5 times (and 5 times longer than the original), I'm using video interpolation to create the smoothing effect. Here, too, you need to add FPS, so that ffmpeg has sufficient data to guess the frames in between. In my example, 5x slowed down, you need 5x24 FPS (original speed), ergo 120 FPS.

ffmpeg -i input.mp4 -filter:v "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=120'" output.mp4

This is a CPU-intensive task, and it can take a while to finish. I used ffplay to preview videos, and I found the results reasonable, but you can't really magic something from nothing, so while this works, it works well when there's a small amount of data missing (not one frame per second every two seconds or such).

For audio, you can use atempo. This option has a range of 0.5-2.0, which means half or double the speed at most, however, it is possible to chain multiple atempo filters to get additional speed changes. Something like the example below:

ffmpeg -i input.mp4 -filter:a "atempo=2.0,atempo=2.0" -vn output.mp4

And that brings us to the end of this tutorial.

結論

I hope you found this fruitful, enjoyable and relevant. Ffmpeg is the workhorse - no, it's a whole cavalry - of audio and video tweaking in Linux, and it has so many practical tools, options and configurations that getting started can be confusing and maybe even intimidating. Those emotions should have evaporated by now as you've read through this tutorial.

Anyway, we talked about conversion and transcoding, scaling and quality, image extraction, combination and GIF creation, splitting and joining of videos, subtitle extraction and embedding, video speedup and slowdown, and then some. That should keep you happy and busy for a while. If you have any ffmpeg-related requests or questions, fire away, the email is only a click away. Or something. Finally, if this tutorial was truly useful, do give some love back, because why not.

乾杯。


  1. 修正 – オーディオ レンダラ エラー、コンピュータを再起動してください

    YouTube 動画の再生中に Audio Renderer エラーが発生して困っていませんか? 答えをイエスとして、Windows マシンでオーディオ レンダラーのエラーを解決する方法を学びましょう。 複数の Windows ユーザーから、YouTube エラー「オーディオ レンダラー エラーです。コンピューターを再起動してください。ただし、この問題は Windows のバージョンやブラウザに固有のものではなく、iTunes や組み込みのオーディオ プレーヤーを聴いている場合でも発生する可能性があります。したがって、Windows でのオーディオ レンダラーの問題を解決するには、さま

  2. システム上のオーディオ ファイルを検索する方法 (2022 年更新ガイド)

    すべてがデジタル化され、画像、書籍、ドキュメント、オーディオ、ビデオがすべてコンピューターに保存されるようになりました。これらの何万ものファイルの中で、オーディオ /mp3 ファイルを見つけるのが困難になることがあります。このガイドでは、PC で Disk Analyzer Pro を使用してオーディオ ファイルを検索するプロセスについて説明します。 . Disk Analyzer Pro:オーディオ ファイルを簡単に検索 Disk Analyzer Pro は Systweak Software によって作成された素晴らしいソフトウェアで、ユーザーは自分のコンピューターに接続されたハー