How to make Handbrake preserve capture time / creation time?

You can copy the existing metadata from one file to another without needing to re-encode the video using FFmpeg. It basically takes one second. To do this, let's assume three files:

  • in.mp4 – the original file before conversion
  • out.mp4 – the file after Handbrake conversion
  • fixed.mp4 – the file with "corrected" metadata

The FFmpeg command to copy the complete metadata record to the new file would then be:

ffmpeg -i in.mp4 -i out.mp4 -map 1 -map_metadata 0 -c copy fixed.mp4

Explanation of syntax:

To break it down, this does the following:

  • Take two input files (in.mp4 and out.mp4), which are assigned the IDs 0 and 1, respectively.
  • Map only the video/audio/subtitle streams from file 1 to the output (-map 1), so we take the bitstreams that are already converted
  • Map only the metadata from file 0 to the output (-map_metadata 0)
  • Use the copy codec (-c copy) to copy all the bitstreams instead of re-encoding the video.

After that, you could obviously rename fixed.mp4 to out.mp4.


Proof:

As an example, here's part of the metadata record of my original file:

$ mediainfo in.mp4 | grep "Encoded date" | head -n 1
Encoded date : UTC 2012-01-08 11:16:19

Here's the file after Handbrake conversion:

$ mediainfo out.mp4 | grep "Encoded date" | head -n 1
Encoded date : UTC 2012-12-24 11:39:35

Here's the final file after mapping the metadata:

$ ffmpeg -i in.mp4 -i out.mp4 -map 1 -map_metadata 0 -c copy fixed.mp4
[…]

$ mediainfo fixed.mp4 | grep "Encoded date" | head -n 1
Encoded date : UTC 2012-01-08 11:16:19    

If you want to do all with FFmpeg:

Actually, you don't really need to use Handbrake if you can use FFmpeg, which Handbrake relies on anyway. In the simplest case you can do your conversion like this:

ffmpeg -i in.mp4 -c:v libx264 -crf 23 -c:a aac -map_metadata 0 out.mp4

This will convert the input with the x264 encoder and AAC audio to an output file, copying the original metadata. In order to change the quality of the output, you can:

  • Change the CRF value for video. Lower means better quality. 23 is default, and anything below 18 will probably be visually lossless.
  • Change the bitrate for audio. See the AAC encoding guide for more info.

Read the x264 encoding guide on the FFmpeg wiki for more.


Unfortunately it seems handbrake can't do it on its own, but similarly to the ffmpeg example, the timestamps can be copied from the original after compression by using the touch unix command:

touch -r MVI_1234.MOV compressed_MVI_1234.m4v

this will set the timestamp on the compressed file to the same as the given reference file.


I found an easier way to do this, using a different software called Adapter: http://www.macroplant.com/adapter/

It doesn't have all the advanced settings like HandBrake but it does the job (also using ffmpeg) and retains the metadata I need.