Search code examples
iosswiftimageffmpegavassetwriter

converting a "gif" to video using swift


I've looked around and found a few things here and there, mainly that I should be using AVAssetWriter to do this but I have 0 experience with this and video editing/creation so it doesn't help me much since I can't seem to find anything that does something I can modify easily (or not at my level of knowledge at least) so that it works as I intend it to.

I have an app which takes n photos every cft (capture frame time which I get from a backend server) seconds (it's a double for obvious reasons) I then display these frames using a UIImageView and the frames change every dft (display frame time which I also get from a backend server and can be different from cft). Up until this point nothing complicated.

now what is currently the workflow is that these frames are sent back to a server with any relevant information I want and then the server would use imagemagick to create a real gif file and ffmpeg to create a 15 seconds video using said gif.

the issue is this makes it so that my heroku server bills aren't as low as I would like because of the limited memory on the dynos and the time it takes to generate these videos is of about 5-10 seconds I believe (not sure but it's longer than I'd like)

So the idea I had was to make the app create the video since he already has all the information he needs for this, and then simply upload it with the rest of the frames and relevant data. Using bandwidth nowadays is much cheaper than buying extra processing power on a server.

  • he has n frames to loop over
  • he has a float value representing how long each frame should last dft
  • he has a gpu or at least a much better cpu than the dynos heroku have to offer

I've also looked around to see if anyone made an extensive tutorial on how to use ffmpeg in swift but I still didn't find anything at my level and I didn't even find a tutorial per se, only some GitHub projects which were partially completed and/or without the original tutorial linked to understand the thought process.

I would appreciate any tips/code sample/tutorials on the subject.

I'm adding the ffmpeg command line equivalent to what I would love to be able to do (if I could use ffmpeg directly with iOS this could be nice too)

ffmpeg -framerate 100/13 -loop 1 -i frame%02d.png -c:v libx264 -r 100/13 -pix_fmt yuv420p -t 0:15 instagram.mp4

where basically I did 100 / (dft * 100) for the input frame rate and just output at the same fps for 15 seconds. by the way if there are any ways to optimise this command to make it run faster without losing quality I might be able to keep the current way of functioning with heroku although I would still prefer some iOS solution.


Solution

  • After some playing around with some gists I managed to modify this solution to handle my image array and it's relative information without freezing my app