Audio over GIFs

Abdulrahman Alomairah

For multiple reasons, I decided to code my own Bash script that records your microphone and then it plays it over a GIF loop for the entire duration of the audio. This is to be able to send it as a .mp4 video. The reasons behind that is that as an Andriod user, Apps like Twitter (X) don’t implement features to Andriod users like the Audio tweet. Yes! This was my main motivation for this code. Furthermore, sometimes Iphone users are unable to play uncommon? extensions like .webm,.mkv,.mov,.wav. Thus, I decided to just share files that have common extensions like .mp3,.mp4 to my Iphone friends instead. It’s a shame that they still use the Apple Eco-system… Anyhow, Here’s the code

Code

#!/bin/bash

# Set variables
audio_file="output_audio.wav"
gif_file="gif.gif"
output_file="twt_audio_$(date +"%Y%m%d_%H%M%S").mp4"

# Record the audio from the USB microphone
ffmpeg -f alsa -i hw:2,0 "$audio_file"

# Get the duration of the recorded audio
duration=$(ffprobe -i "$audio_file" -show_entries format=duration -v quiet -of csv="p=0")

# Create the video by combining the audio and GIF
ffmpeg -i "$audio_file" -ss 0 -to "$duration" -ignore_loop 0 -i "$gif_file" -filter_complex "[1:v]loop=0:1:0,setpts=N/(FRAME_RATE*TB)" -c:v libx264 -profile:v baseline -level:v 3.0 -pix_fmt yuv420p -c:a aac -b:a 192k -movflags +faststart -shortest "$output_file"

# Cleanup temporary audio file
rm "$audio_file"

echo "Video created: $output_file"

You can use explainshell.com to further explain every command if you want. And as have you noticed, the video creating part is extensive. Because Twitter does not accept all types of video encoding. This was a trial and error method. I haven’t tried to lookup any documentation from Twitter’s side. If you have any suggestions, feel free to contact me