As a picture is worth a thousand words, sometimes putting a GIF in Reamde is more useful than writing a bunch of instructions. This short article explains how to use Xcode to record iOS video and turn it into a GIF.

The tools you need to use

  1. XCode
  2. ffmpeg

FFMPEG can be installed using BREW.

1. Use Xcode to record the screen

First open the emulator, and then run the following command to start recording (you can use Control + C to end playback) :

xcrun simctl io booted recordVideo --code=h264 --mask=black --force 

Parameter description:

  • --codec: you can useh264orhevcTwo formats
  • --mask:blackYou can put a border around it, or you can useignoredDon’t show borders

Second, use FFMPG to convert the format

To convert video to output.gif, execute the following command.

ffmpeg -i -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1]; [s0]palettegen[p]; [s1][p]paletteuse" -loop 0 output.gif

Parameter description:

  • fps=10: 10 frames per second, can be adjusted according to their own needs.
  • scale=320:-1: Used to output GIF file width to height, where the value 320 refers to the screen width when the iPhone portrait screen, -1 means to keep the width ratio to zoom.
  • -loop 0: This controls whether the output GIF is looped

    1. -loop 0: Infinite Loop
    2. -loop -1: Do not loop, only play once
    3. -loop 1: Loops once, will play twice

Three, another way to beat a dead horse

You can also use FFPMG to generate PNG image sequences, and then use ImageMagick to generate GIFs.

ffmpeg -i -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1]; [s0]palettegen[p]; [s1][p]paletteuse" output/out%3d.png

This command will generate a bunch of PNG files under the output folder, such as out001.png, out002.png, out003.png…

Then use ImageMagick to generate the GIF

convert -delay 10 -loop 0 -layers optimize output/out*.png output.gif

This method is mainly needed in the middle of the adjustment to edit the PNG file useful, only need GIF words or recommended only to use FFPMG to generate GIF simpler.