From ce1e6195612c04a5608d3e5095a0396495b9fd2a Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Sat, 5 Oct 2019 16:16:54 -0400 Subject: [PATCH 01/11] Write basics of H.264 encoding guide --- SUMMARY.md | 1 + encoding/codecs/h264.md | 232 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 233 insertions(+) create mode 100644 encoding/codecs/h264.md diff --git a/SUMMARY.md b/SUMMARY.md index 7f5fe83..127a455 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -19,6 +19,7 @@ - [Descaling](encoding/descaling.md) - [Resampling](encoding/resampling.md) - [Codecs](encoding/video-encoding.md) + - [H.264](encoding/codecs/h264.md) ## Typesetting diff --git a/encoding/codecs/h264.md b/encoding/codecs/h264.md new file mode 100644 index 0000000..180dc07 --- /dev/null +++ b/encoding/codecs/h264.md @@ -0,0 +1,232 @@ +# Encoding with x264 + +H.264 has been the de facto standard video codec +across the internet for the past decade. +It is widely supported for playback in all modern browsers +and many hardware devices such as gaming consoles. +It provides better video quality at smaller file sizes +compared to its predecessors. + +x264 is a mature, free, open-source encoder for the H.264 codec. +This guide will focus on x264. + +## Prerequisites + +To get started, you'll need two things: + +- A video to encode--for the examples, +we will pipe in a video from VapourSynth, +which you should be able to do if you've been following +the previous sections of this guide +- The x264 encoder + +Here's how do get a copy of the x264 encoder: + +### Windows + +TODO + +### Linux/OS X + +Generally, x264 will be available +through your distribution's package manager. +Here are a few examples: + +#### Ubuntu/Debian + +`sudo apt-get install x264` + +#### Arch Linux + +`sudo pacman -S x264` + +#### OS X + +`brew install x264` + +## Getting Started + +x264 is very configurable, and the options may seem overwhelming. +But you can get started encoding by using the presets x264 provides +and understanding a few basic concepts. +We'll walk through those concepts with the following examples. + +## Example 1 + +Go ahead and open up a terminal window, +and navigate to the folder where your VapourSynth script lives. +Let's run the following command: + +`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --tune animation --crf 24 -o x264output.mkv -` + +Let's run through what each of these options means: + +#### `vspipe --y4m myvideo.vpy -` + +This portion loads your VapourSynth script and pipes it to stdout, +adding y4m headers that x264 can decode. +If you use Linux, you're probably familiar with how piping works. +If you're not, it's basically a way of chaining two commands together. +In this case, we want to chain `vspipe`, +the program that reads VapourSynth scripts, +with x264, our encoder. + +#### `--demuxer y4m` + +This tells x264 that we're providing it with a y4m file. +This matches up with the `--y4m` flag that we gave to the `vspipe` command. + +#### `--preset veryfast` + +x264 has a set of presets to switch between faster encoding, or higher quality. +The full list of presets, from fastest to slowest, is: + +- ultrafast +- superfast +- veryfast +- faster +- fast +- medium +- slow +- slower +- veryslow +- placebo + +You will almost never want to use the very extreme settings, +but generally, if you want good quality +and don't care about how long the encode takes, +`slower` or `veryslow` are recommended. +In this example, because we are just demonstrating how x264 works, +we want a fast encode and have chosen `veryfast`. + +For the curious, you can see a full list of the settings +enabled by each preset by running `x264 --fullhelp | less`. +However, this probably won't mean much at the moment. +Don't worry, this guide wil explain later what all of those settings mean. + +#### `--tune animation` + +Beyond the preset chosen, +x264 allows us to further tune the encoding settings +for the type of content we're working with. +The following tunings are generally the most useful: + +- `film`: Recommended for live action videos. +- `animation`: Recommended for anime or cartoons with flat textures. +For 3D animation (e.g. modern Pixar movies), +you may find better results with `film`. +- `grain`: Recommended for particularly grainy films. + +You don't need to use a tuning, +but it generally helps to produce a better-looking video. + +#### `--crf 24` + +CRF is a constant-quality, 1-pass encoding mode. +In layman's terms, +this means that we don't need the output to meet a specific filesize, +we just want the output to meet a certain quality level. +CRF ranges from 0 to 51, +with 0 being the best quality and 51 being the smallest filesize, +but there is a certain range of CRF settings +that are generally most useful. +Here are some guidelines: + +- CRF 13: This is considered visually lossless to videophiles. +This can produce rather large files, +but is a good choice if you want high quality videos. +Fansubbing groups often use this for Blu-ray encodes. +- CRF 16-18: This is considered visually lossless to most viewers, +and leans toward high quality while still providing a reasonable filesize. +This is a typical range for fansub encodes. +- CRF 21-24: This provides a good balance between quality and filesize. +Some quality loss becomes visible, but this is generally a good choice +where filesize becomes a concern, such as for videos viewed over the internet. +- CRF 26-30: This prioritizes filesize, and quality loss becomes more obvious. +It is generally not recommended to go higher than CRF 30 in any real-world +encoding scenario, unless you want your videos to look like they were made +for dial-up. + +#### `-o x264output.mkv -` + +This last portion tells which files to use for the input and output. +We use `-o` to tell which filename to write the encoded file to. +In this case, x264 will write a file at `x264output.mkv` +in the current directory. + +The last argument to x264 is always the input file. +In this case, we pass `-` for the input file, +which tells x264 to use the piped output from vspipe. + +## Example 2 + +For the next example, let's say we want to make sure our encode +fits onto a single 4.7 GB DVD. How would we do that in x264? + +First, we'll need to figure out what bitrate our encode should be, +in **kilobits per second**. This means we'll need to know a couple of things: + +- The length of our video, in seconds. +For this example, let's say our movie is 2 hours (120 minutes) long. +We'll convert that to seconds: 120 \* 60 = **7200 seconds**. +- Our target filesize. +We know that this is 4.7GB, +but we need to convert it to kilobits. +We can do this with the following steps: + - 4.7GB \* 1024 = 4812.8MB + - 4812.8MB \* 1024 = 4928307.2KB (uppercase B = bytes) + - 4928307.2KB \* 8 = **39426457.6Kb** (lowercase b = bits) + +Now we divide the kilobit size we calculated by our video length, +to find our kilobit per second target bitrate: + +39426457.6Kb / 7200 seconds = **5475 Kbps** + +And here's how we could add that to our x264 command: + +`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --bitrate 5475 -o x264output.mkv -` + +The `--bitrate` option, by itself, +says that we want to do a 1-pass, average-bitrate encode. +In other words, the encoder will still give more bits +to sections of the video that have more detail or motion, +but the average bitrate of the video will be close to what we requested. + +## Example 3 + +So far, we've only done 1-pass encodes. +While using CRF 1-pass is great when you don't have a target bitrate, +it's recommended not to use 1-pass for targeted-birate encodes, +because the encoder can't know +what's coming ahead of the current section of video. +This means it can't make good decisions +about what parts of the video need the most bitrate. + +How do we fix this? x264 supports what is known as 2-pass encoding. +In 2-pass mode, x264 runs through the video twice, +the first time analyzing it to determine where to place keyframes +and which sections of video need the most bitrate, +and the second time performing the actual encode. +2-pass mode is highly recommended if you need to target a certain bitrate. + +Here's how we would run our first pass: + +`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 1 --bitrate 5475 -o x264output.mkv -` + +This creates a stats file in our current directory, +which x264 will use in the second pass: + +`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 2 --bitrate 5475 -o x264output.mkv -` + +You'll notice all we had to change was `--pass 1` to `--pass 2`. Simple! + +Although x264 will automatically use faster settings for the first pass, +it should be no surprise that 2-pass encoding is slower than 1-pass encoding. +Therefore, there are still certain use cases where 1-pass video is a good fit, +such as streaming. +However, if you can use CRF in these situations, +which works well in 1-pass mode, you get the best of both worlds. + +## Advanced Configuration + +Coming Soon \ No newline at end of file From 262a859c80aad839da112afc6c77741e78f20c96 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Sat, 5 Oct 2019 16:35:47 -0400 Subject: [PATCH 02/11] Fix typos --- encoding/codecs/h264.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/encoding/codecs/h264.md b/encoding/codecs/h264.md index 180dc07..0213659 100644 --- a/encoding/codecs/h264.md +++ b/encoding/codecs/h264.md @@ -20,7 +20,7 @@ which you should be able to do if you've been following the previous sections of this guide - The x264 encoder -Here's how do get a copy of the x264 encoder: +Here's how we get a copy of the x264 encoder: ### Windows @@ -102,7 +102,7 @@ we want a fast encode and have chosen `veryfast`. For the curious, you can see a full list of the settings enabled by each preset by running `x264 --fullhelp | less`. However, this probably won't mean much at the moment. -Don't worry, this guide wil explain later what all of those settings mean. +Don't worry, this guide will explain later what all of those settings mean. #### `--tune animation` @@ -161,7 +161,7 @@ which tells x264 to use the piped output from vspipe. ## Example 2 For the next example, let's say we want to make sure our encode -fits onto a single 4.7 GB DVD. How would we do that in x264? +fits onto a single 4.7GB DVD. How would we do that in x264? First, we'll need to figure out what bitrate our encode should be, in **kilobits per second**. This means we'll need to know a couple of things: @@ -196,7 +196,7 @@ but the average bitrate of the video will be close to what we requested. So far, we've only done 1-pass encodes. While using CRF 1-pass is great when you don't have a target bitrate, -it's recommended not to use 1-pass for targeted-birate encodes, +it's recommended not to use 1-pass for targeted-bitrate encodes, because the encoder can't know what's coming ahead of the current section of video. This means it can't make good decisions @@ -222,10 +222,8 @@ You'll notice all we had to change was `--pass 1` to `--pass 2`. Simple! Although x264 will automatically use faster settings for the first pass, it should be no surprise that 2-pass encoding is slower than 1-pass encoding. -Therefore, there are still certain use cases where 1-pass video is a good fit, -such as streaming. -However, if you can use CRF in these situations, -which works well in 1-pass mode, you get the best of both worlds. +Therefore, there are still certain use cases where 1-pass, bitrate-targeted video +is a good fit, such as streaming. ## Advanced Configuration From 6b6cd586f41e7874b2b384be52d9f905c16109f2 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Sun, 6 Oct 2019 10:02:30 -0400 Subject: [PATCH 03/11] Add recap section --- encoding/codecs/h264.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/encoding/codecs/h264.md b/encoding/codecs/h264.md index 0213659..c5cf348 100644 --- a/encoding/codecs/h264.md +++ b/encoding/codecs/h264.md @@ -225,6 +225,23 @@ it should be no surprise that 2-pass encoding is slower than 1-pass encoding. Therefore, there are still certain use cases where 1-pass, bitrate-targeted video is a good fit, such as streaming. +## Recap + +We covered the basics of how to encode in x264, +including speed presets, tunings, and three different encoding modes. + +Here is a summary of when to use each encoding mode: + +- 1-pass Constant Quality (CRF): + - Good for: General-purpose encoding + - Bad for: Streaming; obtaining a certain file size +- 1-pass Average Bitrate: + - Good for: Streaming + - Bad for: Everything else +- 2-pass Average Bitrate: + - Good for: Obtaining a certain file size + - Bad for: Streaming + ## Advanced Configuration Coming Soon \ No newline at end of file From e25873ae2702a47275e04e0803875b4dd7119e54 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Mon, 7 Oct 2019 08:56:49 -0400 Subject: [PATCH 04/11] Fix the name of Apple's proprietary operating system --- encoding/codecs/h264.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/encoding/codecs/h264.md b/encoding/codecs/h264.md index c5cf348..8dbd484 100644 --- a/encoding/codecs/h264.md +++ b/encoding/codecs/h264.md @@ -26,7 +26,7 @@ Here's how we get a copy of the x264 encoder: TODO -### Linux/OS X +### Linux/macOS Generally, x264 will be available through your distribution's package manager. @@ -40,7 +40,7 @@ Here are a few examples: `sudo pacman -S x264` -#### OS X +#### macOS `brew install x264` @@ -244,4 +244,4 @@ Here is a summary of when to use each encoding mode: ## Advanced Configuration -Coming Soon \ No newline at end of file +Coming Soon From 06c55f5f9eab7eb1e40cb32de78197f1562db727 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Mon, 7 Oct 2019 18:56:41 -0400 Subject: [PATCH 05/11] Part 1 of PR fixes --- SUMMARY.md | 2 +- encoding/codecs/{h264.md => x264.md} | 61 ++++++++++++---------------- 2 files changed, 28 insertions(+), 35 deletions(-) rename encoding/codecs/{h264.md => x264.md} (84%) diff --git a/SUMMARY.md b/SUMMARY.md index 127a455..e219b4a 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -19,7 +19,7 @@ - [Descaling](encoding/descaling.md) - [Resampling](encoding/resampling.md) - [Codecs](encoding/video-encoding.md) - - [H.264](encoding/codecs/h264.md) + - [x264](encoding/codecs/x264.md) ## Typesetting diff --git a/encoding/codecs/h264.md b/encoding/codecs/x264.md similarity index 84% rename from encoding/codecs/h264.md rename to encoding/codecs/x264.md index 8dbd484..7e9d10a 100644 --- a/encoding/codecs/h264.md +++ b/encoding/codecs/x264.md @@ -1,13 +1,14 @@ # Encoding with x264 -H.264 has been the de facto standard video codec +H.264 has been the de facto standard video format across the internet for the past decade. It is widely supported for playback in all modern browsers and many hardware devices such as gaming consoles. It provides better video quality at smaller file sizes compared to its predecessors. -x264 is a mature, free, open-source encoder for the H.264 codec. +x264 is a mature, free, open-source encoder +for the H.264 video format. This guide will focus on x264. ## Prerequisites @@ -15,9 +16,9 @@ This guide will focus on x264. To get started, you'll need two things: - A video to encode--for the examples, -we will pipe in a video from VapourSynth, -which you should be able to do if you've been following -the previous sections of this guide + we will pipe in a video from VapourSynth, + which you should be able to do if you've been following + the previous sections of this guide - The x264 encoder Here's how we get a copy of the x264 encoder: @@ -32,17 +33,9 @@ Generally, x264 will be available through your distribution's package manager. Here are a few examples: -#### Ubuntu/Debian - -`sudo apt-get install x264` - -#### Arch Linux - -`sudo pacman -S x264` - -#### macOS - -`brew install x264` +- **Ubuntu/Debian**: `sudo apt install x264` +- **Arch Linux**: `sudo pacman -S x264` +- **macOS**: `brew install x264` ## Getting Started @@ -53,7 +46,7 @@ We'll walk through those concepts with the following examples. ## Example 1 -Go ahead and open up a terminal window, +Open up a terminal window, and navigate to the folder where your VapourSynth script lives. Let's run the following command: @@ -113,8 +106,8 @@ The following tunings are generally the most useful: - `film`: Recommended for live action videos. - `animation`: Recommended for anime or cartoons with flat textures. -For 3D animation (e.g. modern Pixar movies), -you may find better results with `film`. + For 3D animation (e.g. Pixar movies), + you may find better results with `film`. - `grain`: Recommended for particularly grainy films. You don't need to use a tuning, @@ -133,19 +126,19 @@ that are generally most useful. Here are some guidelines: - CRF 13: This is considered visually lossless to videophiles. -This can produce rather large files, -but is a good choice if you want high quality videos. -Fansubbing groups often use this for Blu-ray encodes. + This can produce rather large files, + but is a good choice if you want high quality videos. + Fansubbing groups often use this for Blu-ray encodes. - CRF 16-18: This is considered visually lossless to most viewers, -and leans toward high quality while still providing a reasonable filesize. -This is a typical range for fansub encodes. + and leans toward high quality while still providing a reasonable filesize. + This is a typical range for fansub encodes. - CRF 21-24: This provides a good balance between quality and filesize. -Some quality loss becomes visible, but this is generally a good choice -where filesize becomes a concern, such as for videos viewed over the internet. + Some quality loss becomes visible, but this is generally a good choice + where filesize becomes a concern, such as for videos viewed over the internet. - CRF 26-30: This prioritizes filesize, and quality loss becomes more obvious. -It is generally not recommended to go higher than CRF 30 in any real-world -encoding scenario, unless you want your videos to look like they were made -for dial-up. + It is generally not recommended to go higher than CRF 30 in any real-world + encoding scenario, unless you want your videos to look like they were made + for dial-up. #### `-o x264output.mkv -` @@ -167,12 +160,12 @@ First, we'll need to figure out what bitrate our encode should be, in **kilobits per second**. This means we'll need to know a couple of things: - The length of our video, in seconds. -For this example, let's say our movie is 2 hours (120 minutes) long. -We'll convert that to seconds: 120 \* 60 = **7200 seconds**. + For this example, let's say our movie is 2 hours (120 minutes) long. + We'll convert that to seconds: 120 \* 60 = **7200 seconds**. - Our target filesize. -We know that this is 4.7GB, -but we need to convert it to kilobits. -We can do this with the following steps: + We know that this is 4.7GB, + but we need to convert it to kilobits. + We can do this with the following steps: - 4.7GB \* 1024 = 4812.8MB - 4812.8MB \* 1024 = 4928307.2KB (uppercase B = bytes) - 4928307.2KB \* 8 = **39426457.6Kb** (lowercase b = bits) From 947fac7480a533bb85ebc41ede21953abf813da2 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Mon, 7 Oct 2019 19:05:27 -0400 Subject: [PATCH 06/11] Split lines better --- encoding/codecs/x264.md | 113 ++++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 40 deletions(-) diff --git a/encoding/codecs/x264.md b/encoding/codecs/x264.md index 7e9d10a..a4c08a9 100644 --- a/encoding/codecs/x264.md +++ b/encoding/codecs/x264.md @@ -3,7 +3,7 @@ H.264 has been the de facto standard video format across the internet for the past decade. It is widely supported for playback in all modern browsers -and many hardware devices such as gaming consoles. +and many hardware devices such as gaming consoles and phones. It provides better video quality at smaller file sizes compared to its predecessors. @@ -17,8 +17,8 @@ To get started, you'll need two things: - A video to encode--for the examples, we will pipe in a video from VapourSynth, - which you should be able to do if you've been following - the previous sections of this guide + which you should be able to do + if you've been following the previous sections of this guide - The x264 encoder Here's how we get a copy of the x264 encoder: @@ -39,15 +39,19 @@ Here are a few examples: ## Getting Started -x264 is very configurable, and the options may seem overwhelming. -But you can get started encoding by using the presets x264 provides +x264 is very configurable, +and the options may seem overwhelming. +But you can get started encoding +by using the presets x264 provides and understanding a few basic concepts. -We'll walk through those concepts with the following examples. +We'll walk through those concepts +with the following examples. ## Example 1 Open up a terminal window, -and navigate to the folder where your VapourSynth script lives. +and navigate to the folder +where your VapourSynth script lives. Let's run the following command: `vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --tune animation --crf 24 -o x264output.mkv -` @@ -56,10 +60,13 @@ Let's run through what each of these options means: #### `vspipe --y4m myvideo.vpy -` -This portion loads your VapourSynth script and pipes it to stdout, +This portion loads your VapourSynth script +and pipes it to stdout, adding y4m headers that x264 can decode. -If you use Linux, you're probably familiar with how piping works. -If you're not, it's basically a way of chaining two commands together. +If you use Linux, +you're probably familiar with how piping works. +If you're not, +it's basically a way of chaining two commands together. In this case, we want to chain `vspipe`, the program that reads VapourSynth scripts, with x264, our encoder. @@ -67,11 +74,13 @@ with x264, our encoder. #### `--demuxer y4m` This tells x264 that we're providing it with a y4m file. -This matches up with the `--y4m` flag that we gave to the `vspipe` command. +This matches up with the `--y4m` flag +that we gave to the `vspipe` command. #### `--preset veryfast` -x264 has a set of presets to switch between faster encoding, or higher quality. +x264 has a set of presets +to switch between faster encoding, or higher quality. The full list of presets, from fastest to slowest, is: - ultrafast @@ -89,13 +98,17 @@ You will almost never want to use the very extreme settings, but generally, if you want good quality and don't care about how long the encode takes, `slower` or `veryslow` are recommended. -In this example, because we are just demonstrating how x264 works, +In this example, +because we are just demonstrating how x264 works, we want a fast encode and have chosen `veryfast`. -For the curious, you can see a full list of the settings -enabled by each preset by running `x264 --fullhelp | less`. +For the curious, +you can see a full list of the settings enabled by each preset +by running `x264 --fullhelp | less`. However, this probably won't mean much at the moment. -Don't worry, this guide will explain later what all of those settings mean. +Don't worry, +this guide will explain later +what all of those settings mean. #### `--tune animation` @@ -111,7 +124,8 @@ The following tunings are generally the most useful: - `grain`: Recommended for particularly grainy films. You don't need to use a tuning, -but it generally helps to produce a better-looking video. +but it generally helps +to produce a better-looking video. #### `--crf 24` @@ -120,7 +134,8 @@ In layman's terms, this means that we don't need the output to meet a specific filesize, we just want the output to meet a certain quality level. CRF ranges from 0 to 51, -with 0 being the best quality and 51 being the smallest filesize, +with 0 being the best quality +and 51 being the smallest filesize, but there is a certain range of CRF settings that are generally most useful. Here are some guidelines: @@ -128,17 +143,21 @@ Here are some guidelines: - CRF 13: This is considered visually lossless to videophiles. This can produce rather large files, but is a good choice if you want high quality videos. - Fansubbing groups often use this for Blu-ray encodes. + Some fansubbing groups use this for Blu-ray encodes. - CRF 16-18: This is considered visually lossless to most viewers, - and leans toward high quality while still providing a reasonable filesize. + and leans toward high quality + while still providing a reasonable filesize. This is a typical range for fansub encodes. - CRF 21-24: This provides a good balance between quality and filesize. - Some quality loss becomes visible, but this is generally a good choice - where filesize becomes a concern, such as for videos viewed over the internet. -- CRF 26-30: This prioritizes filesize, and quality loss becomes more obvious. - It is generally not recommended to go higher than CRF 30 in any real-world - encoding scenario, unless you want your videos to look like they were made - for dial-up. + Some quality loss becomes visible, + but this is generally a good choice + where filesize becomes a concern, + such as for videos viewed over the internet. +- CRF 26-30: This prioritizes filesize, + and quality loss becomes more obvious. + It is generally not recommended to go higher than CRF 30 + in any real-world encoding scenario, + unless you want your videos to look like they were made for dial-up. #### `-o x264output.mkv -` @@ -153,15 +172,21 @@ which tells x264 to use the piped output from vspipe. ## Example 2 -For the next example, let's say we want to make sure our encode -fits onto a single 4.7GB DVD. How would we do that in x264? +For the next example, +let's say we want to make sure our encode +fits onto a single 4.7GB DVD. +How would we do that in x264? -First, we'll need to figure out what bitrate our encode should be, -in **kilobits per second**. This means we'll need to know a couple of things: +First, we'll need to figure out +what bitrate our encode should be, +in **kilobits per second**. +This means we'll need to know a couple of things: - The length of our video, in seconds. - For this example, let's say our movie is 2 hours (120 minutes) long. - We'll convert that to seconds: 120 \* 60 = **7200 seconds**. + For this example, + let's say our movie is 2 hours (120 minutes) long. + We'll convert that to seconds: + 120 \* 60 = **7200 seconds**. - Our target filesize. We know that this is 4.7GB, but we need to convert it to kilobits. @@ -183,24 +208,30 @@ The `--bitrate` option, by itself, says that we want to do a 1-pass, average-bitrate encode. In other words, the encoder will still give more bits to sections of the video that have more detail or motion, -but the average bitrate of the video will be close to what we requested. +but the average bitrate of the video +will be close to what we requested. ## Example 3 So far, we've only done 1-pass encodes. -While using CRF 1-pass is great when you don't have a target bitrate, -it's recommended not to use 1-pass for targeted-bitrate encodes, +While using CRF 1-pass is great +when you don't have a target bitrate, +it's recommended not to use 1-pass +for targeted-bitrate encodes, because the encoder can't know what's coming ahead of the current section of video. This means it can't make good decisions about what parts of the video need the most bitrate. -How do we fix this? x264 supports what is known as 2-pass encoding. +How do we fix this? +x264 supports what is known as 2-pass encoding. In 2-pass mode, x264 runs through the video twice, -the first time analyzing it to determine where to place keyframes +the first time analyzing it +to determine where to place keyframes and which sections of video need the most bitrate, and the second time performing the actual encode. -2-pass mode is highly recommended if you need to target a certain bitrate. +2-pass mode is highly recommended +if you need to target a certain bitrate. Here's how we would run our first pass: @@ -214,8 +245,10 @@ which x264 will use in the second pass: You'll notice all we had to change was `--pass 1` to `--pass 2`. Simple! Although x264 will automatically use faster settings for the first pass, -it should be no surprise that 2-pass encoding is slower than 1-pass encoding. -Therefore, there are still certain use cases where 1-pass, bitrate-targeted video +it should be no surprise +that 2-pass encoding is slower than 1-pass encoding. +Therefore, there are still certain use cases +where 1-pass, bitrate-targeted video is a good fit, such as streaming. ## Recap From 3b60e394d2d0147fa15df7d6d8dc22b8a51fd865 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Mon, 7 Oct 2019 19:31:35 -0400 Subject: [PATCH 07/11] More PR fixes --- encoding/codecs/x264.md | 83 ++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/encoding/codecs/x264.md b/encoding/codecs/x264.md index a4c08a9..3823f98 100644 --- a/encoding/codecs/x264.md +++ b/encoding/codecs/x264.md @@ -25,7 +25,12 @@ Here's how we get a copy of the x264 encoder: ### Windows -TODO +Official Windows builds are available +[here](https://download.videolan.org/pub/x264/binaries/win64/). +For these examples, +you'll want the latest version +of the standard 8-bit build, +which is the version without "10b" (for 10-bit) in the name. ### Linux/macOS @@ -35,7 +40,7 @@ Here are a few examples: - **Ubuntu/Debian**: `sudo apt install x264` - **Arch Linux**: `sudo pacman -S x264` -- **macOS**: `brew install x264` +- **macOS**: `brew install x264`https://download.videolan.org/pub/x264/binaries/win64/ ## Getting Started @@ -47,14 +52,16 @@ and understanding a few basic concepts. We'll walk through those concepts with the following examples. -## Example 1 +## Example 1: General-Purpose Encoding Open up a terminal window, and navigate to the folder where your VapourSynth script lives. Let's run the following command: -`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --tune animation --crf 24 -o x264output.mkv -` +``` +vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --tune animation --crf 24 -o x264output.mkv - +``` Let's run through what each of these options means: @@ -69,7 +76,7 @@ If you're not, it's basically a way of chaining two commands together. In this case, we want to chain `vspipe`, the program that reads VapourSynth scripts, -with x264, our encoder. +with `x264`, our encoder. #### `--demuxer y4m` @@ -83,18 +90,18 @@ x264 has a set of presets to switch between faster encoding, or higher quality. The full list of presets, from fastest to slowest, is: -- ultrafast -- superfast -- veryfast -- faster -- fast -- medium -- slow -- slower -- veryslow -- placebo - -You will almost never want to use the very extreme settings, +1. ultrafast +1. superfast +1. veryfast +1. faster +1. fast +1. medium +1. slow +1. slower +1. veryslow +1. placebo + +You will almost never want to use the extreme settings, but generally, if you want good quality and don't care about how long the encode takes, `slower` or `veryslow` are recommended. @@ -104,7 +111,8 @@ we want a fast encode and have chosen `veryfast`. For the curious, you can see a full list of the settings enabled by each preset -by running `x264 --fullhelp | less`. +by running `x264 --fullhelp | less` (Linux/Mac) +or `x264 --fullhelp | more` (Windows). However, this probably won't mean much at the moment. Don't worry, this guide will explain later @@ -133,7 +141,7 @@ CRF is a constant-quality, 1-pass encoding mode. In layman's terms, this means that we don't need the output to meet a specific filesize, we just want the output to meet a certain quality level. -CRF ranges from 0 to 51, +CRF ranges from 0 to 51 (for 8-bit encoding), with 0 being the best quality and 51 being the smallest filesize, but there is a certain range of CRF settings @@ -166,11 +174,15 @@ We use `-o` to tell which filename to write the encoded file to. In this case, x264 will write a file at `x264output.mkv` in the current directory. -The last argument to x264 is always the input file. +The last argument we are passing to x264 is the input file. In this case, we pass `-` for the input file, which tells x264 to use the piped output from vspipe. +The input argument is the only positional argument, +so it does not need to be last; +x264 will recognize it +as the only argument without a `--` flag before it. -## Example 2 +## Example 2: Targeted File Size For the next example, let's say we want to make sure our encode @@ -186,23 +198,30 @@ This means we'll need to know a couple of things: For this example, let's say our movie is 2 hours (120 minutes) long. We'll convert that to seconds: - 120 \* 60 = **7200 seconds**. + 120 minutes \* 60 minutes/second = **7200 seconds**. - Our target filesize. We know that this is 4.7GB, but we need to convert it to kilobits. We can do this with the following steps: - - 4.7GB \* 1024 = 4812.8MB - - 4812.8MB \* 1024 = 4928307.2KB (uppercase B = bytes) - - 4928307.2KB \* 8 = **39426457.6Kb** (lowercase b = bits) + +``` +4.7GiB * 1024MiB/GiB = 4812.8MiB +4812.8MiB * 1024KiB/MiB = 4928307.2KiB +4928307.2KiB * 8Kbits/KiB = 39426457.6Kbits +``` Now we divide the kilobit size we calculated by our video length, to find our kilobit per second target bitrate: -39426457.6Kb / 7200 seconds = **5475 Kbps** +``` +39426457.6Kbits / 7200 seconds = 5475 Kbps +``` And here's how we could add that to our x264 command: -`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --bitrate 5475 -o x264output.mkv -` +``` +vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --bitrate 5475 -o x264output.mkv - +``` The `--bitrate` option, by itself, says that we want to do a 1-pass, average-bitrate encode. @@ -211,7 +230,7 @@ to sections of the video that have more detail or motion, but the average bitrate of the video will be close to what we requested. -## Example 3 +## Example 3: 2-Pass Encoding So far, we've only done 1-pass encodes. While using CRF 1-pass is great @@ -235,12 +254,16 @@ if you need to target a certain bitrate. Here's how we would run our first pass: -`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 1 --bitrate 5475 -o x264output.mkv -` +``` +vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 1 --bitrate 5475 -o x264output.mkv - +``` This creates a stats file in our current directory, which x264 will use in the second pass: -`vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 2 --bitrate 5475 -o x264output.mkv -` +``` +vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --pass 2 --bitrate 5475 -o x264output.mkv - +``` You'll notice all we had to change was `--pass 1` to `--pass 2`. Simple! From 71c5889cb82b131d46142524f09a86024f3ae10c Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Mon, 7 Oct 2019 19:32:40 -0400 Subject: [PATCH 08/11] Fix weird copy paste mistake --- encoding/codecs/x264.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/encoding/codecs/x264.md b/encoding/codecs/x264.md index 3823f98..32adb65 100644 --- a/encoding/codecs/x264.md +++ b/encoding/codecs/x264.md @@ -40,7 +40,7 @@ Here are a few examples: - **Ubuntu/Debian**: `sudo apt install x264` - **Arch Linux**: `sudo pacman -S x264` -- **macOS**: `brew install x264`https://download.videolan.org/pub/x264/binaries/win64/ +- **macOS**: `brew install x264` ## Getting Started From e88ef98b6133053c34108710b431c1edbe8cc0d6 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Tue, 8 Oct 2019 10:08:40 -0400 Subject: [PATCH 09/11] Remove outdated x264 build info --- encoding/codecs/x264.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/encoding/codecs/x264.md b/encoding/codecs/x264.md index 32adb65..c968a73 100644 --- a/encoding/codecs/x264.md +++ b/encoding/codecs/x264.md @@ -27,10 +27,6 @@ Here's how we get a copy of the x264 encoder: Official Windows builds are available [here](https://download.videolan.org/pub/x264/binaries/win64/). -For these examples, -you'll want the latest version -of the standard 8-bit build, -which is the version without "10b" (for 10-bit) in the name. ### Linux/macOS From 0c2da86f1de75bd67fb31bee76b2159b71f7332a Mon Sep 17 00:00:00 2001 From: Dave Date: Wed, 9 Oct 2019 10:04:05 -0400 Subject: [PATCH 10/11] Fix spacing before headers --- encoding/codecs/x264.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/encoding/codecs/x264.md b/encoding/codecs/x264.md index c968a73..2c6ad23 100644 --- a/encoding/codecs/x264.md +++ b/encoding/codecs/x264.md @@ -11,6 +11,7 @@ x264 is a mature, free, open-source encoder for the H.264 video format. This guide will focus on x264. + ## Prerequisites To get started, you'll need two things: @@ -23,11 +24,13 @@ To get started, you'll need two things: Here's how we get a copy of the x264 encoder: + ### Windows Official Windows builds are available [here](https://download.videolan.org/pub/x264/binaries/win64/). + ### Linux/macOS Generally, x264 will be available @@ -38,6 +41,7 @@ Here are a few examples: - **Arch Linux**: `sudo pacman -S x264` - **macOS**: `brew install x264` + ## Getting Started x264 is very configurable, @@ -48,6 +52,7 @@ and understanding a few basic concepts. We'll walk through those concepts with the following examples. + ## Example 1: General-Purpose Encoding Open up a terminal window, @@ -61,6 +66,7 @@ vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --tune animati Let's run through what each of these options means: + #### `vspipe --y4m myvideo.vpy -` This portion loads your VapourSynth script @@ -74,12 +80,14 @@ In this case, we want to chain `vspipe`, the program that reads VapourSynth scripts, with `x264`, our encoder. + #### `--demuxer y4m` This tells x264 that we're providing it with a y4m file. This matches up with the `--y4m` flag that we gave to the `vspipe` command. + #### `--preset veryfast` x264 has a set of presets @@ -114,6 +122,7 @@ Don't worry, this guide will explain later what all of those settings mean. + #### `--tune animation` Beyond the preset chosen, @@ -131,6 +140,7 @@ You don't need to use a tuning, but it generally helps to produce a better-looking video. + #### `--crf 24` CRF is a constant-quality, 1-pass encoding mode. @@ -163,6 +173,7 @@ Here are some guidelines: in any real-world encoding scenario, unless you want your videos to look like they were made for dial-up. + #### `-o x264output.mkv -` This last portion tells which files to use for the input and output. @@ -178,6 +189,7 @@ so it does not need to be last; x264 will recognize it as the only argument without a `--` flag before it. + ## Example 2: Targeted File Size For the next example, @@ -226,6 +238,7 @@ to sections of the video that have more detail or motion, but the average bitrate of the video will be close to what we requested. + ## Example 3: 2-Pass Encoding So far, we've only done 1-pass encodes. @@ -270,6 +283,7 @@ Therefore, there are still certain use cases where 1-pass, bitrate-targeted video is a good fit, such as streaming. + ## Recap We covered the basics of how to encode in x264, @@ -287,6 +301,7 @@ Here is a summary of when to use each encoding mode: - Good for: Obtaining a certain file size - Bad for: Streaming + ## Advanced Configuration Coming Soon From cc752bc0b6685bf6ff5312104338b56e20f90d4b Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Wed, 9 Oct 2019 20:43:27 -0400 Subject: [PATCH 11/11] More PR fixes --- encoding/codecs/x264.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/encoding/codecs/x264.md b/encoding/codecs/x264.md index 2c6ad23..c54eb9a 100644 --- a/encoding/codecs/x264.md +++ b/encoding/codecs/x264.md @@ -9,14 +9,13 @@ compared to its predecessors. x264 is a mature, free, open-source encoder for the H.264 video format. -This guide will focus on x264. ## Prerequisites To get started, you'll need two things: -- A video to encode--for the examples, +- A video to encode—for the examples, we will pipe in a video from VapourSynth, which you should be able to do if you've been following the previous sections of this guide @@ -53,7 +52,7 @@ We'll walk through those concepts with the following examples. -## Example 1: General-Purpose Encoding +### Example 1: General-Purpose Encoding Open up a terminal window, and navigate to the folder @@ -67,7 +66,7 @@ vspipe --y4m myvideo.vpy - | x264 --demuxer y4m --preset veryfast --tune animati Let's run through what each of these options means: -#### `vspipe --y4m myvideo.vpy -` +##### `vspipe --y4m myvideo.vpy -` This portion loads your VapourSynth script and pipes it to stdout, @@ -81,14 +80,14 @@ the program that reads VapourSynth scripts, with `x264`, our encoder. -#### `--demuxer y4m` +##### `--demuxer y4m` This tells x264 that we're providing it with a y4m file. This matches up with the `--y4m` flag that we gave to the `vspipe` command. -#### `--preset veryfast` +##### `--preset veryfast` x264 has a set of presets to switch between faster encoding, or higher quality. @@ -119,11 +118,14 @@ by running `x264 --fullhelp | less` (Linux/Mac) or `x264 --fullhelp | more` (Windows). However, this probably won't mean much at the moment. Don't worry, -this guide will explain later +this page will explain later what all of those settings mean. +*Disclaimer: +x264's `fullhelp` is not guaranteed to be up-to-date.* -#### `--tune animation` + +##### `--tune animation` Beyond the preset chosen, x264 allows us to further tune the encoding settings @@ -141,7 +143,7 @@ but it generally helps to produce a better-looking video. -#### `--crf 24` +##### `--crf 24` CRF is a constant-quality, 1-pass encoding mode. In layman's terms, @@ -174,7 +176,7 @@ Here are some guidelines: unless you want your videos to look like they were made for dial-up. -#### `-o x264output.mkv -` +##### `-o x264output.mkv -` This last portion tells which files to use for the input and output. We use `-o` to tell which filename to write the encoded file to. @@ -190,7 +192,7 @@ x264 will recognize it as the only argument without a `--` flag before it. -## Example 2: Targeted File Size +### Example 2: Targeted File Size For the next example, let's say we want to make sure our encode @@ -239,7 +241,7 @@ but the average bitrate of the video will be close to what we requested. -## Example 3: 2-Pass Encoding +### Example 3: 2-Pass Encoding So far, we've only done 1-pass encodes. While using CRF 1-pass is great