Making the own HEIF/HEIC files

file conversionheicheif

I've been looking around for a while for a way to start packing image bytestreams into HEIF files, since macOS High Sierra and and iOS 11 are now out. Haven't had much luck, just large numbers of people converting .heic files to .jpg, and the Nokia JS viewer on GitHub

To start, I am not looking to convert my existing Photo Library all to HEVC encoded .heic files, there's diminishing returns to be had there since they're already jpegged and have had quality loss from the original.
So this question is not a duplicate of the "Convert whole library to new .HEIC format" question

My goal:

  • Take both a mpeg bytestream and a PNG or JPG bytestream and pack them into a .heic file

  • Take multiple PNG and/or JPG bytestreams and make a .heic file of them (like a burst series of photos)

HEIF is just a container, so while it was designed for HEVC use, it does support other data types like jpg, png, pretty much anything that the viewing applications support in their natives file formats.

I just haven't been able to find any software that will let me do it. Everything I search for to do with HEIF or HEIC in the term just pumps me with tech news headlines about Apple trying to replace jpeg, and people converting their .heic files once their imported from their computer (which seems like it would take effort to do since iOS defaultly will convert them for you if you try to import them or share them anywhere it can't explicitly confirm the reaching end supports them…)

So I'm hoping someone here knows a thing or two and could point me in the right direction at least.

I'm not adverse to command line tools, or even doing some development myself if I can get pointed at a library I can import into a Swift application (I'm a late bloomer in programming, and while I could say the same for Javascript, if I was to build an app around a library to do this work, I'd much rather use Swift than use a scripting language)

So hopefully someone has thought the same thing and had better luck than me finding this! 🙂

Edit 1, Nov 10:

I've found a site by a bloke called Ben Gotow that talks about converting existing JPEGs into HEVC bitstreams, and then putting them into a .heic or .heif container. It's close, but he hadn't been able to find a lot of detail for doing more than that. I'm not adding this as an answer, just as an edit since it is relevant but doesn't actually achieve my goal. hope anyone else finding this question will find his site useful!
jpgtoheif.com

Best Answer

This use of GPAC may move closer to the goals you desire:

What is GPAC?

GPAC is an Open Source multimedia framework. GPAC is used for research and academic purposes and beyond through industrial collaborations! The project covers different aspects of multimedia, with a focus on presentation technologies (graphics, animation and interactivity) and on multimedia packaging formats such as MP4.

More details:

A little more research and an answer here provides the way! We need several things installed, ideally with homebrew:

  • x265 and GPAC brew install x265 gpac

  • FFmpeg with x265 support

    brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-libvorbis --with-libvpx --with-opus --with-x265
    

Reference

Now that you have the tools, try a script like this to convert images in a folder to HEIF/HEIC:

for F in *.jpg *.png *.tif *.tiff; \
  do ffmpeg -i "$F" -crf 23 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -preset slower -pix_fmt yuv420p -f hevc "${F%.*}.hvc" && \
     MP4Box -add-image "${F%.*}.hvc" -ab heic -new "${F%.*}.heic" && \
     rm "${F%.*}.hvc";
  done;

I tried it with a folder of images and it works. It turns images with odd dimensions to even dimensions, for instance, but besides a few caveats it works.