Answers.Body: <p>Yes, some encoders allow changing parameters (like target quality or bitrate, or psychovisual tuning options (x264's <code>aq</code> and <code>psy-rd</code> options).</p>
  
  <p><code>x264</code>, the stand-alone command-line frontend for the library, has a <code>--zones</code> parameter to give more bitrate to some parts of the video.  So you could for example reduce the quality for the credits.</p>
  
  <p>The actual x264 library supports changing many parameters on the fly, but I don't know if ffmpeg has a way to specify option changes for ranges of the video.  You could get nearly the same result by segmenting your video, and encoding the different segments with different options, then combining them.</p>
  
  <p>Generally nobody actually does this, because it's a HUGE amount of work, and it's really hard to know which settings are going to help just by looking at a scene.  Usually if you want more quality, you just spend more CPU time on the encode, and that improves everything across the board. (<code>--preset veryslow</code>).  I could imagine getting some benefit out of tweaking AQ settings, though, maybe to address banding in one scene, but changing it for other scenes where that wasn't an issue.</p>
  
  <p>Still, bits are essentially never precious enough for it to be worth paying a human to save them, instead of just electricity and CPU time.  This is more of a hobby thing / thought experiment to see what different codec settings do, IMO.  Maybe when we need to stream video to / from a mars colony, bits will be that precious...</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-03T10:26:48.577Z
Answers.Text:  Thanks! My use case is a screen recording of a click dummy. Quite often while I explain on the audio track, the image is still for 1-5 secs, great reduction of frames/filesize with `-vf mpdecimate -vsync vfr`. Some more dynamic here/then: mouse cursor, UI widgets open/close. All looks great with `-crf 28`. Just in the intro I shortly slide in/out a portrait and say hello. This lookus ugly at CRF 28. So I looked for a way to compress only that intro scene with a higher quality. No I know: Either separate intro + main video concatenated or find a way for `--zones` within `ffmpeg`.
Answers.UserDisplayName: porg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-03T12:01:55.967Z
Answers.Text: @porg: After I wrote this answer, I tried actually using `--zones`.  It didn't work well; the bitrate spiked right *after* a low-bitrate zone IIRC, wasting a lot of the savings, so I think rate-control doesn't interact well with it, at least the way I was doing it where I tried to set a bitrate limit for a short part of a video.  So concatenation with compatible settings is probably good.
Answers.UserDisplayName: Peter Cordes
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-03T18:59:24.257Z
Answers.Text: Generally you deemed "human encoding optimization not worth it". I overally agree. But an optimization for at most 1 or2 scenes per video (intro or outro/credits) to receive a different quality parameter to me seems a legit optimization effort.
  
  So my 30min video "looks really well enough" at CRF 28. But that first 10secs of the intro really need the CRF 18 to look pleasant and not turn off my viewers right at the beginning, but still I want my viewers (also not blessed with broadband!) to benefit from a video otherwise really well compressed.
Answers.UserDisplayName: porg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-03T19:09:04.587Z
Answers.Text: Also regarding buffering the UX should be ok if you have the high quality part at the beginning, then followed by average compression.
  
  It will buffer what's necessary for the high quality start. Sort of "overcompensates", may take longer than necessary for the later median bitrate. When reaching the average quality part it will most likely continue without any hickups.
  
  Whereas starting low, the buffer likely was calculated over-optimistically small, and when reaching the main higher quality part will get overwhelmed and may need to rebuffer then.
Answers.UserDisplayName: porg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-03T19:15:51.583Z
Answers.Text: Regarding concatenation: Could you add to your answer how you did it? Given a concrete input.mov lasting 30m 00s into the target file being a h264 mp4, the audio unprocessed, the video processed. Which command line(s) to create that 10secs intro (from 00:00 to 00:10) at CRF 18. The remainder (00:10-30:00 (ideally not hardcoded but a dynamic "end" syntax) at CRF 28 in which I would like to use `-vf bpdecimate -vsync vfr` (in that pass or on the final outcome file? And then concat them. Ideally the target h.264 MP4 with a VFR video track + the original audio track passed through from the .MOV.
Answers.UserDisplayName: porg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-03T19:36:36.313Z
Answers.Text: @porg: It was a few years ago that I tried it, but in my notes I found I'd used `ffmpeg -i ...  -x265-params "zones=1500,7000,b=0.7/9100,13600,b=1.6" out.mkv` in addition to the usual options.   At that time, it seems the option only overrode bitrate, not modify the CRF like would be much better.
Answers.UserDisplayName: Peter Cordes
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-03T19:36:43.770Z
Answers.Text: Additionally, my notes said *zones only supports q=%d or b=%f sscanf formats.  Others will error and leave qp=0 for that region!!*  *2pass with zones isn't great.  By the end of a b>1 zone, ratecontrol is back down to below the normal rate.*  *And there's a big spike wasting bits when b increases (from 0.7 back to 1, or from 1 to 1.6)* So it seems the bitrate multiplier was being overridden by 2-pass ratecontrol or something.  IDK if I tried it with CRF.  This was I think with libx265, but probably also applies to FFmpeg's `-c:v libx264`.
Answers.UserDisplayName: Peter Cordes
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-03T19:39:43.897Z
Answers.Text: I suspect libx265 and libx264 can handle this well, but unless FFmpeg's driver for it has improved (like making CRF an expression involving `t` or a frame number?), you can't take advantage of the full power.  Or maybe there's some rate-control setting that can make it not suck.
Answers.UserDisplayName: Peter Cordes
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-04T09:17:18.620Z
Answers.Text: Thanks for sharing your `-x265-params "zones=…" out.mkv` experiences. Could you be so kind and add concrete instructions for the concat scenario into your existing answer or a new answer? (For this the comments are likely too limited in syntax and length)
Answers.UserDisplayName: porg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-04T12:21:13.100Z
Answers.Text: @porg: That's not something I've tried recently or maybe ever.  But google finds results like https://ffmpeg.org/faq.html#How-can-I-join-video-files_003f / https://trac.ffmpeg.org/wiki/Concatenate and SO's [How to concatenate two MP4 files using FFmpeg?](https://stackoverflow.com/q/7333232).  I'm not sure how to make sure that two segments are compatible.
Answers.UserDisplayName: Peter Cordes
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-05T14:01:01.697Z
Answers.Text: Was successful eventually! See my full writeup with detailed instructions at: https://video.stackexchange.com/questions/36646/how-to-encode-scenes-with-different-crf-into-a-h-264-mp4-video-with-a-variable-framerate
Answers.UserDisplayName: porg
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-05T18:55:10.783Z
Answers.LastActivityDate: 2015-06-05T18:55:10.783Z
Answers.UserDisplayName: Peter Cordes
Answers.Body: <p>Variable bit rate and adaptive codecs are common in the audio world, and work very well. The most obvious example is VBR MP3s. Encoding video is a fundamentally similar problem, so I would be surprised if adaptive codecs don't exist there too.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-03-14T08:54:22.217Z
Answers.Text: That would be good to know they are exist. However, I wonder if there are semi-automatic applications for professionals who wish to adjust quality of the output to the best.
Answers.UserDisplayName: Kentzo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-03-14T06:55:30.353Z
Answers.LastActivityDate: 2013-03-14T06:55:30.353Z
Answers.UserDisplayName: ObscureRobot
Answers.Body: <p>What you are describing is effectively what 2-pass VBR does for you.  It makes a first path that calculates the level of change for each particular time in the video and then uses this information to make the best possible use of the available storage space.</p>
  
  <p>It is, however, entirely possible to do the process manually by doing multiple encodings with different parameters.  In fact, many formats even support having multiple encodings with different parameters within the same file.  This is how you are able to change the resolution on the fly when streaming videos on YouTube and how Netflix automatically adjusts the quality on the fly as you watch to try to avoid buffering issues.</p>
  
  <p>Any professional level encoder should allow you to specify multiple formats for the file in queue and many will even let you specify multiple outputs for one instance of the file.  (For example, the render queue in After Effects allows for this.)</p>
  
  <p>In general, I would recommend running a 2-pass VBR and then adjusting the keyframe and total bandwidth accordingly.  If you are using a format that works based on the motion in the scene, then generally keyframes need to be more often if the video gets lots of motion artifacts (where it diverges from where it actually should be too much in high motion).  If the video is simply too low quality and "pixelly", then the bandwidth generally needs to be raised or the keyframes lowered.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-03-22T09:45:31.983Z
Answers.Text: Do you know of software that allows scene/regional parameters for MPEG-2 or AVC encoding? I have read that Hollywood DVDs are mastered by a 'compressionist' who uses tools which allow such fine-grained tweaking.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-03-22T13:12:30.960Z
Answers.Text: @Mulvya - I have used such a package once when I was using a hardware encoder and Sonic DVD Producer for MPEG-2.  The software was however very complex, unstable and unwieldy and was, in short, the largest waste of $5 grand I have ever spent.  There may be cheaper options that allow that level of control now, but unless you are thoroughly knowledgeable about the algorithms and looking through for particular artifact problems, you will probably have better luck with 2 pass VBR.
Answers.UserDisplayName: AJ Henderson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-03-14T13:22:16.117Z
Answers.LastActivityDate: 2013-03-14T13:22:16.117Z
Answers.UserDisplayName: AJ Henderson
Body: <p>I know that it's very hard to find out best encoding parameters for a whole video file. Wouldn't it be easier to just go through the video and find out the best parameters for different parts (or scenes).</p>
  
  <p>Does it make sense or probably there is software that already allows that?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2013-03-15T21:34:12.350Z
Comments.Text: No need to use two-passes. See the [FFmpeg and x264 Encoding Guide](https://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide); specifically the section on using a constant rate factor (CRF) for examples and more information. All you need to do is pick your crf value which controls your quality, and your encoding preset which controls your encoding speed/compression efficiency.
Comments.UserDisplayName: llogan
Tags: <codec><encoding>
Title: Is it possible to encode video using the same codec and container but different parameters for different scenes?
UserDisplayName: Kentzo
ThreadText: # Is it possible to encode video using the same codec and container but different parameters for different scenes?
  
  (Asked by: username_0 on 2013-03-14)
  
  I know that it's very hard to find out best encoding parameters for a whole video file. Wouldn't it be easier to just go through the video and find out the best parameters for different parts (or scenes).
  
  Does it make sense or probably there is software that already allows that?
  
  **Tags:** codec, encoding
  
  > **Comments:**
  > **username_1**: No need to use two-passes. See the [FFmpeg and x264 Encoding Guide](https://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide); specifically the section on using a constant rate factor (CRF) for examples and more information. All you need to do is pick your crf value which controls your quality, and your encoding preset which controls your encoding speed/compression efficiency.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_5 on 2013-03-14. Score: 5 | Accepted answer
  
  What you are describing is effectively what 2-pass VBR does for you. It makes a first path that calculates the level of change for each particular time in the video and then uses this information to make the best possible use of the available storage space.
  
  It is, however, entirely possible to do the process manually by doing multiple encodings with different parameters. In fact, many formats even support having multiple encodings with different parameters within the same file. This is how you are able to change the resolution on the fly when streaming videos on YouTube and how Netflix automatically adjusts the quality on the fly as you watch to try to avoid buffering issues.
  
  Any professional level encoder should allow you to specify multiple formats for the file in queue and many will even let you specify multiple outputs for one instance of the file. (For example, the render queue in After Effects allows for this.)
  
  In general, I would recommend running a 2-pass VBR and then adjusting the keyframe and total bandwidth accordingly. If you are using a format that works based on the motion in the scene, then generally keyframes need to be more often if the video gets lots of motion artifacts (where it diverges from where it actually should be too much in high motion). If the video is simply too low quality and "pixelly", then the bandwidth generally needs to be raised or the keyframes lowered.
  
  > **Comments:**
  > **username_6**: Do you know of software that allows scene/regional parameters for MPEG-2 or AVC encoding? I have read that Hollywood DVDs are mastered by a 'compressionist' who uses tools which allow such fine-grained tweaking.  
  > **username_5**: @Mulvya - I have used such a package once when I was using a hardware encoder and Sonic DVD Producer for MPEG-2.  The software was however very complex, unstable and unwieldy and was, in short, the largest waste of $5 grand I have ever spent.  There may be cheaper options that allow that level of control now, but unless you are thoroughly knowledgeable about the algorithms and looking through for particular artifact problems, you will probably have better luck with 2 pass VBR.  
  
  ---
  
  ### Answer by username_2 on 2015-06-05. Score: 2
  
  Yes, some encoders allow changing parameters (like target quality or bitrate, or psychovisual tuning options (x264's `aq` and `psy-rd` options).
  
  `x264`, the stand-alone command-line frontend for the library, has a `--zones` parameter to give more bitrate to some parts of the video. So you could for example reduce the quality for the credits.
  
  The actual x264 library supports changing many parameters on the fly, but I don't know if ffmpeg has a way to specify option changes for ranges of the video. You could get nearly the same result by segmenting your video, and encoding the different segments with different options, then combining them.
  
  Generally nobody actually does this, because it's a HUGE amount of work, and it's really hard to know which settings are going to help just by looking at a scene. Usually if you want more quality, you just spend more CPU time on the encode, and that improves everything across the board. (`--preset veryslow`). I could imagine getting some benefit out of tweaking AQ settings, though, maybe to address banding in one scene, but changing it for other scenes where that wasn't an issue.
  
  Still, bits are essentially never precious enough for it to be worth paying a human to save them, instead of just electricity and CPU time. This is more of a hobby thing / thought experiment to see what different codec settings do, IMO. Maybe when we need to stream video to / from a mars colony, bits will be that precious...
  
  > **Comments:**
  > **username_3**:  Thanks! My use case is a screen recording of a click dummy. Quite often while I explain on the audio track, the image is still for 1-5 secs, great reduction of frames/filesize with `-vf mpdecimate -vsync vfr`. Some more dynamic here/then: mouse cursor, UI widgets open/close. All looks great with `-crf 28`. Just in the intro I shortly slide in/out a portrait and say hello. This lookus ugly at CRF 28. So I looked for a way to compress only that intro scene with a higher quality. No I know: Either separate intro + main video concatenated or find a way for `--zones` within `ffmpeg`.  
  > **username_2**: @username_3: After I wrote this answer, I tried actually using `--zones`.  It didn't work well; the bitrate spiked right *after* a low-bitrate zone IIRC, wasting a lot of the savings, so I think rate-control doesn't interact well with it, at least the way I was doing it where I tried to set a bitrate limit for a short part of a video.  So concatenation with compatible settings is probably good.  
  > **username_3**: Generally you deemed "human encoding optimization not worth it". I overally agree. But an optimization for at most 1 or2 scenes per video (intro or outro/credits) to receive a different quality parameter to me seems a legit optimization effort.  So my 30min video "looks really well enough" at CRF 28. But that first 10secs of the intro really need the CRF 18 to look pleasant and not turn off my viewers right at the beginning, but still I want my viewers (also not blessed with broadband!) to benefit from a video otherwise really well compressed.  
  > **username_3**: Also regarding buffering the UX should be ok if you have the high quality part at the beginning, then followed by average compression.  It will buffer what's necessary for the high quality start. Sort of "overcompensates", may take longer than necessary for the later median bitrate. When reaching the average quality part it will most likely continue without any hickups.  Whereas starting low, the buffer likely was calculated over-optimistically small, and when reaching the main higher quality part will get overwhelmed and may need to rebuffer then.  
  > **username_3**: Regarding concatenation: Could you add to your answer how you did it? Given a concrete input.mov lasting 30m 00s into the target file being a h264 mp4, the audio unprocessed, the video processed. Which command line(s) to create that 10secs intro (from 00:00 to 00:10) at CRF 18. The remainder (00:10-30:00 (ideally not hardcoded but a dynamic "end" syntax) at CRF 28 in which I would like to use `-vf bpdecimate -vsync vfr` (in that pass or on the final outcome file? And then concat them. Ideally the target h.264 MP4 with a VFR video track + the original audio track passed through from the .MOV.  
  > **username_2**: @username_3: It was a few years ago that I tried it, but in my notes I found I'd used `ffmpeg -i ...  -x265-params "zones=1500,7000,b=0.7/9100,13600,b=1.6" out.mkv` in addition to the usual options.   At that time, it seems the option only overrode bitrate, not modify the CRF like would be much better.  
  > **username_2**: Additionally, my notes said *zones only supports q=%d or b=%f sscanf formats.  Others will error and leave qp=0 for that region!!*  *2pass with zones isn't great.  By the end of a b>1 zone, ratecontrol is back down to below the normal rate.*  *And there's a big spike wasting bits when b increases (from 0.7 back to 1, or from 1 to 1.6)* So it seems the bitrate multiplier was being overridden by 2-pass ratecontrol or something.  IDK if I tried it with CRF.  This was I think with libx265, but probably also applies to FFmpeg's `-c:v libx264`.  
  > **username_2**: I suspect libx265 and libx264 can handle this well, but unless FFmpeg's driver for it has improved (like making CRF an expression involving `t` or a frame number?), you can't take advantage of the full power.  Or maybe there's some rate-control setting that can make it not suck.  
  > **username_3**: Thanks for sharing your `-x265-params "zones=…" out.mkv` experiences. Could you be so kind and add concrete instructions for the concat scenario into your existing answer or a new answer? (For this the comments are likely too limited in syntax and length)  
  > **username_2**: @username_3: That's not something I've tried recently or maybe ever.  But google finds results like https://ffmpeg.org/faq.html#How-can-I-join-video-files_003f / https://trac.ffmpeg.org/wiki/Concatenate and SO's [How to concatenate two MP4 files using FFmpeg?](https://stackoverflow.com/q/7333232).  I'm not sure how to make sure that two segments are compatible.  
  > **username_3**: Was successful eventually! See my full writeup with detailed instructions at: https://video.stackexchange.com/questions/36646/how-to-encode-scenes-with-different-crf-into-a-h-264-mp4-video-with-a-variable-framerate  
  
  ---
  
  ### Answer by username_4 on 2013-03-14. Score: 0
  
  Variable bit rate and adaptive codecs are common in the audio world, and work very well. The most obvious example is VBR MP3s. Encoding video is a fundamentally similar problem, so I would be surprised if adaptive codecs don't exist there too.
  
  > **Comments:**
  > **username_0**: That would be good to know they are exist. However, I wonder if there are semi-automatic applications for professionals who wish to adjust quality of the output to the best.  
  
  ---
  
  