Making my own HEIF/HEIC files

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


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:

  • Compare and convert HEIF, JPEG and WebP images with rokka
  • GPAC support for HEIF.

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.


Install ImageMagick and use basic covert format to go from one format to another.

I know this is an old thread, but posting my two cents that in 2020 it is far easier to bulk convert images to HEIC nowadays than it was in the past.

The first step is to get ImageMagick and ExifTool installed via Homebrew like this:

brew install imagemagick exiftool

Once you have ImageMagick installed, converting to HEIC is as simple as this:

convert image.jpg image.heic

That’s it! ImageMagick converts files from one format to another based the file extension of the destination file. And all metadata from the source file is cleanly transferred from the old file to the new file by using ExifTool after the image conversion has happened.

So in this example just naming the destination file image.heic is enough to get an HEIC file generated from the source file image.jpg.

But since I had to bulk convert some directories filled with images, I wrote this simple Bash script that searches a directory for JPG, PNG and TIFF images and then runs them through ImageMagick and saves an HEIC file with the same filename in the same directory.

Just take this script and change /path/to/images to match your source directory and you are all set!

find -E '/path/to/images' -type f -iregex '.*\.(JPG|JPEG|PNG|TIF|TIFF)$' |\
  while read full_image_path
  do
    path_sans_extension="${full_image_path%.*}"
    convert "${full_image_path}" "${path_sans_extension}".heic
    exiftool -overwrite_original_in_place -tagsFromFile "${full_image_path}" "${path_sans_extension}".heic
  done