Skip to content

Instantly share code, notes, and snippets.

@magic-akari
Forked from sinoru/ConvertToHEIC.swift
Last active September 12, 2019 13:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magic-akari/d59efdaf9298ad20f0b5d05eb7d7cef9 to your computer and use it in GitHub Desktop.
Save magic-akari/d59efdaf9298ad20f0b5d05eb7d7cef9 to your computer and use it in GitHub Desktop.
ConvertToHEIC
#!/usr/bin/env osascript -l JavaScript
ObjC.import("Foundation");
ObjC.import("ImageIO");
ObjC.import("AVFoundation");
const quality = 0.8;
function run(input) {
for (const file of input) {
const path = file.toString();
console.log(`Converting ${path}`);
const sourceFileURL = $.NSURL.fileURLWithPath(path);
const imageSource = $.CGImageSourceCreateWithURL(sourceFileURL, $());
if (imageSource.isNil()) {
console.log(`Can not load ${sourceFileURL.path.js} . Skip`);
continue;
}
const imageProperties = $.CGImageSourceCopyProperties(imageSource, $());
const imageCount = $.CGImageSourceGetCount(imageSource);
const destinationFileURL = sourceFileURL.URLByDeletingPathExtension.URLByAppendingPathExtension(
"heic"
);
const imageDestination = $.CGImageDestinationCreateWithURL(
destinationFileURL,
$.AVFileTypeHEIC,
imageCount,
$()
);
if (imageDestination.isNil()) {
console.log(`Can not write ${destinationFileURL.path.js} . Skip`);
continue;
}
$.CGImageDestinationSetProperties(imageDestination, imageProperties);
for (let index = 0; index < imageCount; index++) {
$.CGImageDestinationAddImageFromSource(
imageDestination,
imageSource,
index,
{ kCGImageDestinationLossyCompressionQuality: quality }
);
}
const result = $.CGImageDestinationFinalize(imageDestination);
if (!result) {
console.log(`Can not finalize ${destinationFileURL.path.js} . Skip`);
continue;
}
}
}
#!/usr/bin/env swift
//
// ConvertToHEIC.swift
// ConvertToHEIC
//
// Created by Sinoru on 2018. 4. 28..
//
import Foundation
import ImageIO
import AVFoundation
for argument in CommandLine.arguments.dropFirst() {
print("Converting \(argument).")
let sourceFileURL = URL(fileURLWithPath: argument)
guard let imageSource = CGImageSourceCreateWithURL(sourceFileURL as CFURL, nil) else {
print("Can't not load \(sourceFileURL). Skip")
continue
}
let imageProperties = CGImageSourceCopyProperties(imageSource, nil)
let imageCount = CGImageSourceGetCount(imageSource)
let destinationFileURL = sourceFileURL.deletingPathExtension().appendingPathExtension("heic")
guard let imageDestination = CGImageDestinationCreateWithURL(destinationFileURL as CFURL, AVFileType.heic as CFString, imageCount, nil) else {
print("Can't not write \(destinationFileURL). Skip")
continue
}
CGImageDestinationSetProperties(imageDestination, imageProperties)
for index in 0..<imageCount {
CGImageDestinationAddImageFromSource(imageDestination, imageSource, index, [kCGImageDestinationLossyCompressionQuality: 0.8] as CFDictionary)
}
guard CGImageDestinationFinalize(imageDestination) else {
print("Can't not finalize \(destinationFileURL). Skip")
continue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment