Repository: yt-dlp/yt-dlp

Issue:
MTime option is ignored when using SponsorBlock
### Checklist

- [X] I'm reporting a bug unrelated to a specific site
- [X] I've verified that I'm running yt-dlp version **2022.02.04**. ([update instructions](https://github.com/yt-dlp/yt-dlp#update))
- [X] I've checked that all provided URLs are alive and playable in a browser
- [X] I've checked that all URLs and arguments with special characters are [properly quoted or escaped](https://github.com/ytdl-org/youtube-dl#video-url-contains-an-ampersand-and-im-getting-some-strange-output-1-2839-or-v-is-not-recognized-as-an-internal-or-external-command)
- [X] I've searched the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues including closed ones. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)

### Description

When downloading a YouTube video that has sponsorblock segments, after it is done cutting and merging the result, the file modification time is not set to what the `Last-modified header` has. 

If there is no sponsorblock segments for the video, or the `--sponsorblock-remove` option is removed, it works as intended and sets the time on the file correctly.

From my quick looking around, I'm guessing something similar to this [commit](https://github.com/yt-dlp/yt-dlp/commit/ca87974543e3409a8a2bcd720305a0094ab4cf9a) might need to be added to [modify_chapters.remove_chapters](https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/postprocessor/modify_chapters.py#L315), but not 100% sure.

### Verbose log

```shell
[debug] Command-line config: ['-vU', '--format-sort', 'res:1080,+codec:avc:m4a', '--merge-output-format', 'mkv', '--mtime', '--sponsorblock-remove', 'all', 'https://www.youtube.com/watch?v=lObhiNlkNdg']
[debug] Encodings: locale UTF-8, fs utf-8, out utf-8, err utf-8, pref UTF-8
[debug] yt-dlp version 2022.02.04 [c1653e9ef] (zip)
[debug] Python version 3.8.10 (CPython 64bit) - Linux-5.4.0-97-generic-x86_64-with-glibc2.29
[debug] exe versions: ffmpeg 5.0 (fdk,setts), ffprobe 5.0
[debug] Optional libraries: Crypto, secretstorage, sqlite
[debug] Proxy map: {}
Latest version: 2022.02.04, Current version: 2022.02.04
yt-dlp is up to date (2022.02.04)
[debug] [youtube] Extracting URL: https://www.youtube.com/watch?v=lObhiNlkNdg
[youtube] lObhiNlkNdg: Downloading webpage
[youtube] lObhiNlkNdg: Downloading android player API JSON
[debug] Sort order given by user: res:1080, +codec:avc:m4a
[debug] Sort order given by extractor: quality, res, fps, hdr:12, source, codec:vp9.2, lang, proto
[debug] Formats sorted by: hasvid, ie_pref, res:1080(1080.0), +vcodec:avc(7), +acodec:m4a(8), quality, fps, hdr:12(7), source, lang, proto, filesize, fs_approx, tbr, vbr, abr, asr, vext, aext, hasaud, id
[SponsorBlock] Fetching SponsorBlock segments
[debug] SponsorBlock query: https://sponsor.ajay.app/api/skipSegments/fa7c?service=YouTube&categories=%5B%22filler%22%2C+%22interaction%22%2C+%22outro%22%2C+%22preview%22%2C+%22music_offtopic%22%2C+%22sponsor%22%2C+%22selfpromo%22%2C+%22intro%22%5D
[SponsorBlock] Found 2 segments in the SponsorBlock database
[debug] Default format spec: bestvideo*+bestaudio/best
[info] lObhiNlkNdg: Downloading 1 format(s): 137+140
[debug] Invoking downloader on "https://rr1---sn-gvbxgn-tm0l.googlevideo.com/videoplayback?expire=1644866471&ei=R1cKYsObOIWH_9EPnZGGyAE&ip=97.108.111.49&id=o-AAQH-HeHm8UZf3ZExo8xFYOYzcBPRCxyHinQsUie-fM-&itag=137&source=youtube&requiressl=yes&mh=KI&mm=31%2C29&mn=sn-gvbxgn-tm0l%2Csn-gvbxgn-tt1s&ms=au%2Crdu&mv=m&mvi=1&pl=24&initcwndbps=2253750&vprv=1&mime=video%2Fmp4&gir=yes&clen=469678065&dur=1781.980&lmt=1642877727540025&mt=1644844378&fvip=1&keepalive=yes&fexp=24001373%2C24007246&c=ANDROID&txp=5535434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRgIhANtQwcAtjxbDs1eHmSFjCS3tTw6yKg4BxmTpiiq6GNR7AiEA4d3ip3lUfjljdQe60z5CzP0ZqgKVjVR8DCb0s1dEngQ%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIgadO53nr_hD3maztizreaVD-kdcAWvwUSolRnKZLtj7oCIQDN-GhgkaooHUo0-AKTxj8SzXkWo9mdBJq8j77HRNy9-A%3D%3D"
[download] Destination: MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].f137.mp4
[download] 100% of 447.92MiB in 00:20
[debug] Invoking downloader on "https://rr1---sn-gvbxgn-tm0l.googlevideo.com/videoplayback?expire=1644866471&ei=R1cKYsObOIWH_9EPnZGGyAE&ip=97.108.111.49&id=o-AAQH-HeHm8UZf3ZExo8xFYOYzcBPRCxyHinQsUie-fM-&itag=140&source=youtube&requiressl=yes&mh=KI&mm=31%2C29&mn=sn-gvbxgn-tm0l%2Csn-gvbxgn-tt1s&ms=au%2Crdu&mv=m&mvi=1&pl=24&initcwndbps=2253750&vprv=1&mime=audio%2Fmp4&gir=yes&clen=28841030&dur=1782.038&lmt=1642838943369778&mt=1644844378&fvip=1&keepalive=yes&fexp=24001373%2C24007246&c=ANDROID&txp=5532434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRQIhAK2VcB7NYYq1yKkpxk9Yz-dCYmH6Ri78elWLBoLjt-vQAiBWAPOpaneZEZwRMqZGycBbRjrSoiQtnUmkYD9kK1Cp1g%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIgadO53nr_hD3maztizreaVD-kdcAWvwUSolRnKZLtj7oCIQDN-GhgkaooHUo0-AKTxj8SzXkWo9mdBJq8j77HRNy9-A%3D%3D"
[download] Destination: MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].f140.m4a
[download] 100% of 27.50MiB in 00:01
[Merger] Merging formats into "MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].mkv"
[debug] ffmpeg command line: ffmpeg -y -loglevel repeat+info -i 'file:MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].f137.mp4' -i 'file:MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].f140.m4a' -c copy -map 0:v:0 -map 1:a:0 -movflags +faststart 'file:MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].temp.mkv'
Deleting original file MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].f140.m4a (pass -k to keep)
Deleting original file MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].f137.mp4 (pass -k to keep)
[debug] ffprobe command line: ffprobe -hide_banner -show_format -show_streams -print_format json 'file:MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].mkv'
[debug] Concat spec = 0.0-459.344000, 534.501000-983.504000, 992.316000-inf
[ModifyChapters] Removing chapters from MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].mkv
[debug] Writing concat spec to MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].temp.mkv.concat
[debug] ffmpeg command line: ffmpeg -y -loglevel repeat+info -hide_banner -nostdin -f concat -safe 0 -i 'file:MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].temp.mkv.concat' -map 0 -dn -ignore_unknown -c copy -movflags +faststart 'file:MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].temp.mkv'
Deleting original file MOVING BACK INTO THE VAN (preparing for winter van life!) [lObhiNlkNdg].uncut.mkv (pass -k to keep)
```



Discussion:

Resolving patch:
diff --git a/yt_dlp/postprocessor/ffmpeg.py b/yt_dlp/postprocessor/ffmpeg.py
--- a/yt_dlp/postprocessor/ffmpeg.py
+++ b/yt_dlp/postprocessor/ffmpeg.py
@@ -384,12 +384,10 @@ def concat_files(self, in_files, out_file, concat_opts=None):
 
         out_flags = list(self.stream_copy_opts(ext=determine_ext(out_file)))
 
-        try:
-            self.real_run_ffmpeg(
-                [(concat_file, ['-hide_banner', '-nostdin', '-f', 'concat', '-safe', '0'])],
-                [(out_file, out_flags)])
-        finally:
-            os.remove(concat_file)
+        self.real_run_ffmpeg(
+            [(concat_file, ['-hide_banner', '-nostdin', '-f', 'concat', '-safe', '0'])],
+            [(out_file, out_flags)])
+        os.remove(concat_file)
 
     @classmethod
     def _concat_spec(cls, in_files, concat_opts=None):
diff --git a/yt_dlp/postprocessor/modify_chapters.py b/yt_dlp/postprocessor/modify_chapters.py
--- a/yt_dlp/postprocessor/modify_chapters.py
+++ b/yt_dlp/postprocessor/modify_chapters.py
@@ -68,9 +68,11 @@ def remove_chapters(file, is_sub):
         # Renaming should only happen after all files are processed
         files_to_remove = []
         for in_file, out_file in in_out_files:
+            mtime = os.stat(in_file).st_mtime
             uncut_file = prepend_extension(in_file, 'uncut')
             os.replace(in_file, uncut_file)
             os.replace(out_file, in_file)
+            self.try_utime(in_file, mtime, mtime)
             files_to_remove.append(uncut_file)
 
         return files_to_remove, info


Test patch: