We are still actively working on the spam issue.

Difference between revisions of "User:Ergopon/WebM Tutorial"

From InstallGentoo Wiki
Jump to: navigation, search
m (merge suggestion)
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
shouldn't this be merged with [[WebM]]? [[User:Mrsnooze|Mrsnooze]] ([[User talk:Mrsnooze|talk]]) 06:57, 16 April 2015 (EDT)
 +
 
This guide assumes that you have installed [[gentoo]].
 
This guide assumes that you have installed [[gentoo]].
  
Line 46: Line 48:
  
 
You should always set both a bitrate and a CRF or else it will default to something really shitty. You will want to experiment with different values before settling on something you like. Personally, I set CRF to 4 and try to set a bitrate that hits just under the filesize limit. Optimizing these two settings will make your webm pretty decent already.
 
You should always set both a bitrate and a CRF or else it will default to something really shitty. You will want to experiment with different values before settling on something you like. Personally, I set CRF to 4 and try to set a bitrate that hits just under the filesize limit. Optimizing these two settings will make your webm pretty decent already.
 +
 +
== Manipulating individual video, audio and subtitle streams ==
 +
 +
Note: This part is a bit tricky. Skip it if you just want to make a simple webm already.
 +
 +
Each file is made up of 1 or more streams: video, audio. or subtitle stream(s). They's sometimes also called channels or tracks.
 +
Each stream in a file is numbered from 0 upwards. Stream types can also be accessed via the letters v/a/s for video/audio/subtitle respectively.
 +
Ffmpeg can also take multiple input files:
 +
 +
:<code>ffmpeg -i FILE.mkv -i OTHERFILE.mkv</code>
 +
 +
Numbered in order from 0 upwards. By default, if you only use one file, it will be numbered 0.
 +
 +
ffmpeg uses the -map option to map multiple inputs to the one output. For example, if you want to map the audio and video stream of a file to the output file, you would do this:
 +
 +
:<code>ffmpeg -i FILE.mkv -map 0:a -map 0:v OUTPUT.webm</code>
 +
 +
-map 0:a means "use the audio stream of the first file. -map 0:v says you want to use the video stream of the first file
 +
 +
But what if there are multiple streams of the same type? i.e. multiple subtitle streams? By default, ffmpeg takes the first of each type of stream, but you can specifiy what you want:
 +
 +
:<code>ffmpeg -i FILE.mkv -map 0:a -map 0:v -map 0:1:s OUTPUT.webm</code>
 +
 +
This next command tells ffmpeg to take the audio stream and the video stream of the first file, and the subtitle stream of the second file:
 +
 +
:<code>ffmpeg -i FILE.mkv -i FILE2.mkv -map -0:a 0:v 1:s OUTPUT.webm</code>
 +
 +
[https://trac.ffmpeg.org/wiki/How%20to%20use%20-map%20option Here's the ffmpeg guide on using -map]
  
 
== 2 pass encoding ==
 
== 2 pass encoding ==
Line 71: Line 101:
  
  
4chan also limits files to 3MB. To find the maximum bitrate that will keep your webm within 3MB, you can do a simple calculation. (3MB/seconds)*8 = bitrate.
+
4chan also limits files to 3MB. To find the maximum bitrate that will keep your webm within 3MB, you can do a simple calculation.
 +
 
 +
:(3MB/seconds)*8 = bitrate.
 +
 
 +
For example, let's say you have a 10 second clip you want to post.  
  
For example, let's say you have a 10 second clip you want to post. 3MB/10 seconds = .3MB/s. Since bitrate is measured in bits(b) and not bytes (B), multiply by 8 to get the final bitrate: 2.4M.  
+
:3MB/10 seconds = .3MB/s.  
 +
 
 +
Since bitrate is measured in bits(b) and not bytes (B), multiply by 8 to get the final bitrate:
 +
 
 +
:2.4M.  
  
 
However, the encoder is not always exact, so you may end up going over or under. Just adjust your bitrate and try again.
 
However, the encoder is not always exact, so you may end up going over or under. Just adjust your bitrate and try again.
Line 108: Line 146:
 
== Ffmpeg Tools ==
 
== Ffmpeg Tools ==
 
In addition to encoding, ffmpeg has numerous other capabilities such as cutting, resizing, hardsubbing, and cropping.
 
In addition to encoding, ffmpeg has numerous other capabilities such as cutting, resizing, hardsubbing, and cropping.
 +
 +
 +
== Cut ==
 +
To cut a clip from a long video, all you need to do is input the time the clip begins and the length.
 +
:<code>-ss START -t DURATION</code>
 +
 +
Or, you can specify the end time instead of the duration:
 +
 +
:<code>-ss START -to END</code>
 +
 +
Time format of start and end is generally hh:mm:ss.
 +
 +
ffmpeg has two seeking modes:
 +
 +
* input file seeking (put the -ss START before the -i input file)
 +
 +
:<code>ffmpeg -ss START -i "INPUT.mkv" output.webm</code>
 +
 +
This will take all data from the time specified at START. It starts transcoding from the specified time instantly, but the timestamps in the output file will be reset to 0, so if you attach subtitles, it will transcode them '''from the beginning of the subtitle file'''. You can also add the -t/-to options to specify an endpoint of transcoding, too.
 +
 +
* ouput file seeking (put the -ss START after the -i input file and before the output file)
 +
 +
:<code>ffmpeg -i "INPUT.mkv" -ss START output.webm</code>
 +
 +
This will also take all data from the time specified at START, however, it will first transcode everything up to that point, but throw it away. This is slower than the previous option, because of that seeking, but it does keep the timestamps in the output, so if you add subtitles, it will add the ones at the right time. Again, you can use the -t/-to options to specify an endpoint of transcoding.
 +
 +
[https://trac.ffmpeg.org/wiki/Seeking Here's the ffmpeg tutorial on using the cut option]
  
 
== Resize ==
 
== Resize ==
 
:<code>-vf scale=-1:HEIGHT</code>
 
:<code>-vf scale=-1:HEIGHT</code>
 +
 +
== Hardsub ==
 +
First, extract the sub file.
 +
:<code>ffmpeg -i "INPUT.mkv" -an -vn -c copy "SUB.ass"</code>
 +
Add this to your options
 +
:<code> -vf subtitles="SUB.ass"</code>
 +
Unfortunately, if you use input seeking it fucks up. Use output seeking only.
 +
 +
== Crop ==
 +
:<code>-vf "crop=WIDTH:HEIGHT:TOP:LEFT"</code>

Latest revision as of 11:57, 16 April 2015

shouldn't this be merged with WebM? Mrsnooze (talk) 06:57, 16 April 2015 (EDT)

This guide assumes that you have installed gentoo.


The basic command

ffmpeg -i input.mkv output.webm

Wow! Is it really that easy? Yes. Yes it is. However, this will give you a really shitty webm that everyone will mock you for. You might as well be posting gifs. In order to make good quality webms, we need to add extra options to fine-tune it.

First, let's examine the structure of an ffmpeg command:

ffmpeg
Starts the ffmpeg program you have on your computer. Pretty straightforward.
-i input.mkv
The file you want to convert. You always need an -i in front. If the filename has spaces you need to enclose it in quotes: -i "my file.mkv"
output.webm
The name of the converted webm.

Basic options

Look at your webm. It is really shitty. How are you going to make it less shitty? By increasing the quality.

For webms, quality is controlled by 2 general settings:

Bitrate

Bitrate is simply the amount of data that goes into each second. Higher bitrates mean higher quality, but also with a bigger filesize.

-b:v BITRATE
"BITRATE" can be values such as 320K, 2M, and so on.

CRF

CRF is a setting that tries to make each second meet a certain amount of "quality." Some scenes need less data to look good, so the extra data can be used for other scenes.

-crf NUMBER
"NUMBER" can be a value from 4 - 63, where lower is better quality.

Place your settings after your input. In general, this is where your options should always go. For example:

ffmpeg -i input.mkv -b:v 2M -crf 10 output.webm

You should always set both a bitrate and a CRF or else it will default to something really shitty. You will want to experiment with different values before settling on something you like. Personally, I set CRF to 4 and try to set a bitrate that hits just under the filesize limit. Optimizing these two settings will make your webm pretty decent already.

Manipulating individual video, audio and subtitle streams

Note: This part is a bit tricky. Skip it if you just want to make a simple webm already.

Each file is made up of 1 or more streams: video, audio. or subtitle stream(s). They's sometimes also called channels or tracks. Each stream in a file is numbered from 0 upwards. Stream types can also be accessed via the letters v/a/s for video/audio/subtitle respectively. Ffmpeg can also take multiple input files:

ffmpeg -i FILE.mkv -i OTHERFILE.mkv

Numbered in order from 0 upwards. By default, if you only use one file, it will be numbered 0.

ffmpeg uses the -map option to map multiple inputs to the one output. For example, if you want to map the audio and video stream of a file to the output file, you would do this:

ffmpeg -i FILE.mkv -map 0:a -map 0:v OUTPUT.webm

-map 0:a means "use the audio stream of the first file. -map 0:v says you want to use the video stream of the first file

But what if there are multiple streams of the same type? i.e. multiple subtitle streams? By default, ffmpeg takes the first of each type of stream, but you can specifiy what you want:

ffmpeg -i FILE.mkv -map 0:a -map 0:v -map 0:1:s OUTPUT.webm

This next command tells ffmpeg to take the audio stream and the video stream of the first file, and the subtitle stream of the second file:

ffmpeg -i FILE.mkv -i FILE2.mkv -map -0:a 0:v 1:s OUTPUT.webm

Here's the ffmpeg guide on using -map

2 pass encoding

You should use 2 pass encoding whenever possible. During 2 pass encoding, ffmpeg scans the file once before encoding it. This makes the encoding faster and more efficient.

You need to use two commands to tell ffmpeg to use 2 pass encoding.

ffmpeg -i input.mkv -pass 1 output.webm
ffmpeg -i input.mkv -pass 2 output.webm
Enter the second after the first has finished. Answer "yes" when ffmpeg asks to overwrite. It will generate a log file that you can delete after your webm has finished encoding.

The rule for 2 pass encodings is that all of your options must be the same except for -pass. If you don't want to keep answering "yes," add -y to your options. This will stop ffmpeg from prompting you.

Multithreading

-threads 0

Simple! Makes encoding faster. You should always include this in your options.

Posting on 4chan

4chan doesn't allow sound. Any webms that still have sound cannot be uploaded.

-an

will create a webm without sound.


4chan also limits files to 3MB. To find the maximum bitrate that will keep your webm within 3MB, you can do a simple calculation.

(3MB/seconds)*8 = bitrate.

For example, let's say you have a 10 second clip you want to post.

3MB/10 seconds = .3MB/s.

Since bitrate is measured in bits(b) and not bytes (B), multiply by 8 to get the final bitrate:

2.4M.

However, the encoder is not always exact, so you may end up going over or under. Just adjust your bitrate and try again.

Summary

ffmpeg -i input.mkv -b:v X -crf X -an -threads 0 -pass 1 -y output.webm
ffmpeg -i input.mkv -b:v X -crf X -an -threads 0 -pass 2 -y output.webm

Simply insert your filenames and choose a bitrate with a crf, and you will have a good quality webm to post.

Additional quality options

Combining bitrate, crf, and 2 pass encoding will already take you 90% of the way to the best you can get. Read on to squeeze out the extra 10%.

### I AM TOO LAZY TO DO THIS RIGHT NOW

#THIS SHIT WORKS

-auto-alt-ref 1

-quality good

-cpu-used 0

#THIS SHIT DOESN't

-bufsize 99M

-lag-in-frames 25

-arnr-maxframes 25

-arnr-strength 6

Ffmpeg Tools

In addition to encoding, ffmpeg has numerous other capabilities such as cutting, resizing, hardsubbing, and cropping.


Cut

To cut a clip from a long video, all you need to do is input the time the clip begins and the length.

-ss START -t DURATION

Or, you can specify the end time instead of the duration:

-ss START -to END

Time format of start and end is generally hh:mm:ss.

ffmpeg has two seeking modes:

  • input file seeking (put the -ss START before the -i input file)
ffmpeg -ss START -i "INPUT.mkv" output.webm

This will take all data from the time specified at START. It starts transcoding from the specified time instantly, but the timestamps in the output file will be reset to 0, so if you attach subtitles, it will transcode them from the beginning of the subtitle file. You can also add the -t/-to options to specify an endpoint of transcoding, too.

  • ouput file seeking (put the -ss START after the -i input file and before the output file)
ffmpeg -i "INPUT.mkv" -ss START output.webm

This will also take all data from the time specified at START, however, it will first transcode everything up to that point, but throw it away. This is slower than the previous option, because of that seeking, but it does keep the timestamps in the output, so if you add subtitles, it will add the ones at the right time. Again, you can use the -t/-to options to specify an endpoint of transcoding.

Here's the ffmpeg tutorial on using the cut option

Resize

-vf scale=-1:HEIGHT

Hardsub

First, extract the sub file.

ffmpeg -i "INPUT.mkv" -an -vn -c copy "SUB.ass"

Add this to your options

-vf subtitles="SUB.ass"

Unfortunately, if you use input seeking it fucks up. Use output seeking only.

Crop

-vf "crop=WIDTH:HEIGHT:TOP:LEFT"