{"id":1180,"date":"2020-11-03T15:57:38","date_gmt":"2020-11-03T20:57:38","guid":{"rendered":"https:\/\/blog.lufamily.ca\/kang\/?p=1180"},"modified":"2020-11-03T16:13:19","modified_gmt":"2020-11-03T21:13:19","slug":"converting-iphone-12-hdr-videos-to-sdr-with-ffmpeg","status":"publish","type":"post","link":"https:\/\/blog.lufamily.ca\/kang\/2020\/11\/03\/converting-iphone-12-hdr-videos-to-sdr-with-ffmpeg\/","title":{"rendered":"Converting iPhone 12 HDR Videos to SDR with FFMpeg"},"content":{"rendered":"\n<p>In a <a href=\"https:\/\/blog.lufamily.ca\/kang\/2020\/10\/28\/best-way-to-play-dolby-vision-videos-from-your-iphone-12\/\" data-type=\"post\" data-id=\"1177\" target=\"_blank\" rel=\"noreferrer noopener\">previous post<\/a>, I talked about how to view HDR (High Dynamic Range) videos from the iPhone 12 on my OLED TV. However, sometimes I like to take that HDR video and converted into SDR (Standard Dynamic Range) videos for posting or distribution.<\/p>\n\n\n\n<p>During the course of experimenting with FFMpeg, as the primary tool for this purpose, I found out that it is not always necessary to convert the video depending on where you are going to use the video. For example, uploading the raw HDR footage from the iPhone 12 to YouTube works just fine. However posting HDR video footage to Instagram currently yields a very washed out result.<\/p>\n\n\n\n<p>I personally prefer to store all my raw footage in its high resolution 4K HDR goodness. However, I also keep a rendered down SDR version for practical use. How does one get an SDR video from an HDR source? This is what FFMpeg is for.<\/p>\n\n\n\n<p>All the command line instructions below have been tested on the macOS, and it is assumed that you already have <a href=\"https:\/\/docs.brew.sh\/Installation\" target=\"_blank\" rel=\"noreferrer noopener\">brew installed<\/a>.<\/p>\n\n\n\n<p>You will need to install a version of FFMpeg that has the <code>zscale<\/code> filter. If you have a previous version installed without this filter, then you will have to uninstall it first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew uninstall --ignore-dependencies ffmpeg<\/code><\/pre>\n\n\n\n<p>And then we install the version with the filter from the <code>homebrew-ffmpeg<\/code> tap.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew install homebrew-ffmpeg\/ffmpeg\/ffmpeg --with-fdk-aac --with-libbluray --with-libbs2b --with-libcaca --with-libgsm --with-libmodplug --with-librsvg --with-libsoxr --with-libssh --with-libvidstab --with-libvmaf --with-libxml2 --with-opencore-amr --with-openh264 --with-openjpeg --with-openssl --with-openssl@1.1 --with-rav1e --with-rtmpdump --with-rubberband --with-speex --with-srt --with-tesseract --with-two-lame --with-wavpack --with-webp --with-xvid --with-zeromq --with-zimg<\/code><\/pre>\n\n\n\n<p>I recorded a sample video from my iPhone 12 Pro, below is the raw footage.<\/p>\n\n\n\n<p><span style=\"font-size:80%; font-style:italic; color:#777\">The included videos in this article are all HEVC encoded. If your browser does not support this encoding, then you will not be able to play the videos. Safari has no issues. If you have Windows 10, then you can install an <a rel=\"noreferrer noopener\" href=\"https:\/\/www.howtogeek.com\/680690\/how-to-install-free-hevc-codecs-on-windows-10-for-h.265-video\/\" target=\"_blank\">extension<\/a>. How the videos are displayed also depend on the quality and capability of your monitor.<\/span><\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"2160\" style=\"aspect-ratio: 3840 \/ 2160;\" width=\"3840\" controls src=\"https:\/\/blog.lufamily.ca\/kang\/wp-content\/uploads\/sites\/3\/2020\/11\/raw.mov\"><\/video><figcaption>Raw footage from iPhone 12 (4K HDR 60fps) &#8211; 92M in size<\/figcaption><\/figure>\n\n\n\n<p>If you just perform a simple conversion, you will get the washed up version:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ffmpeg -y -i raw.mov -map v:0 -map 0:a -c:v hevc -preset veryfast -tag:v hvc1 -c:a copy sdr_washed_out.mp4<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"2160\" style=\"aspect-ratio: 3840 \/ 2160;\" width=\"3840\" controls src=\"https:\/\/blog.lufamily.ca\/kang\/wp-content\/uploads\/sites\/3\/2020\/11\/sdr_washed_out.mp4\"><\/video><figcaption>Simple conversion without filters gets a bland result &#8211; 5.8M in size<\/figcaption><\/figure>\n\n\n\n<p>The HDR colours have to be appropriately mapped using some filter trickery with FFMpeg. I found these filter settings about two years ago when trying to convert HDR videos from YouTube in the BT2020 space to BT709. Below is the set of filters used:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ffmpeg -y -i raw.mov -filter_complex \"&#91;0:v]zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p&#91;v]\" -map \"&#91;v]\" -map 0:a -c:v hevc -preset veryfast -tag:v hvc1 -c:a copy sdr.mp4<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"2160\" style=\"aspect-ratio: 3840 \/ 2160;\" width=\"3840\" controls src=\"https:\/\/blog.lufamily.ca\/kang\/wp-content\/uploads\/sites\/3\/2020\/11\/sdr.mp4\"><\/video><figcaption>SDR result with filter &#8211; 5.6M in size<\/figcaption><\/figure>\n\n\n\n<p>As you can see the SDR version with filter is a lot closer to the original one.<\/p>\n\n\n\n<p>These FFMpeg conversions require a lot of CPU horse power, so beware that they will take a long time. Let me know if there is a better way, as I&#8217;m always open to optimize this workflow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post, I talked about how to view HDR (High Dynamic Range) videos from the iPhone 12 on my OLED TV. However, sometimes I like to take that HDR video and converted into SDR (Standard Dynamic Range) videos for posting or distribution. During the course of experimenting with FFMpeg, as the primary tool &hellip; <a href=\"https:\/\/blog.lufamily.ca\/kang\/2020\/11\/03\/converting-iphone-12-hdr-videos-to-sdr-with-ffmpeg\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Converting iPhone 12 HDR Videos to SDR with FFMpeg&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1192,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_featured_media_url":"https:\/\/blog.lufamily.ca\/kang\/wp-content\/uploads\/sites\/3\/2020\/11\/Screen-Shot-2020-11-03-at-4.03.10-PM.png","jetpack_shortlink":"https:\/\/wp.me\/p7V6i8-j2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/posts\/1180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/comments?post=1180"}],"version-history":[{"count":10,"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/posts\/1180\/revisions"}],"predecessor-version":[{"id":1195,"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/posts\/1180\/revisions\/1195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/media\/1192"}],"wp:attachment":[{"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/media?parent=1180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/categories?post=1180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lufamily.ca\/kang\/wp-json\/wp\/v2\/tags?post=1180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}