Batch convert incompatible video for OSX Apple Photos app using FFMPEG and Bash

A Little Context/Rant

In 2012 Apple began changing their business model. For years they had strived to be an innovative company, consistently one-upping themselves in product usability and design. Then in 2012 it seemed that Apply quietly began a slow battle for absolute control of how we used their products. Maybe it was before that with the whole Flash business. For me it truly hit home in 2012

Once welcoming, easy to use, and interoperable with devices, software, and formats outside of the Apple ecosystem, the company began to produce and revise products that took on a posture of exclusivity and dominance.

There was a time when you could run a program like Perian that allowed Quicklook to be extended to view a plethora of video and image formats. Videographers and designers loved it. Then Apple shut that extensibility down. In a single upgrade 1,000’s of professionals were left out in the cold and forced to convert their (most-likely massive) libraries to a more Apple-friendly format.

Even their own photo management software, iPhoto, was forced to become the Photos app and abandon the old video formats. Videos that were previously viewable in iPhoto were now prevented from being imported to the new Photos app. There was no option to convert. Not even a friendly, “Here’s where your old files are located”. You got a list of filenames that Apple didn’t like.

That was me. I had multiple iPhoto libraries that lovingly held old avi, mpg, and 3gp videos. I saved these libraries in a “to convert someday” folder hoping that someday I would know enough to be able to save these relics.

That day has come. It is today.

A little background on the iPhoto and Photos app. Both libraries present on disk as a big ol’ file. This is done to prevent folks from accidentally hosing up the organization or accidentally deleting/overwriting files. This library is a “package” and if you right-click on it and choose “view package contents” you can see the goodies inside (The “Masters” folder is where your original files are located). The iPhotos app, as best as I can tell, kept the “Masters” folder organized by year of creation. So, if your file was created in 2006 it would be in the 2006 folder. The Photos app, again – as best as I can tell, organizes the “Masters” folder by import date. That means your 2006 file may end up in the 2019 folder if that’s when you imported it. However, the app itself presents the photo and video files in proper order/sequence so it’s kind of a non-issue (I guess?).

The Problem

I have to convert all these videos. I have learned to do this with bash scripts using FFMPEG. The problem arrives with the newly created files. They have today’s date. They need to have the date of the original file they were converted from. Otherwise when they are imported to the Photos app they lose their context and connection to the static images taken at the same time. And ain’t nobody got time to track them down and manually update the date (within the app OR terminal).

The Solution

for f in *.avi; do ffmpeg -i "$f" -c:a libfdk_aac -c:v libx264 -profile:v baseline "${f%}new.mp4"; touch -mt $(stat -f "%Sm" -t "%Y%m%d%H%M" "$f") "${f%}new.mp4"; done;

Its elegant. Its messy. It gets the job done. This requires FFMPEG to be installed. I may throw some instructions for that in another article…check back later.

For now, this script will roll through, in the above case, all the .avi files in a folder, and convert them, with as minimal re-sequencing as possible, to a Photos-app-friendly .mp4 container, adding the word “new” into the filename. The slick part is that it retrieves the creation date from the original file and “touches” the new file with the same date!

To convert a different format video file just change the .avi part to whatever format your files are in. This is only half of the solution if you are inside of the iPhoto “Masters” folder. The old files will still be there and may throw off the import. Honestly I haven’t done enough testing to know if this is true or not. It may import everything that’s good and reject the bad. At any rate, I describe my next steps below. If you trust the Photos app importer then go ahead and import away and skip the deleting below 🙂

To delete the original video files that have been converted just issue the following command in Terminal. There is no undo so please delete carefully.

$ rm *.avi