Encode audio with cURL and open-source tools
Audio encoding on the command line can be powerful and efficient when you combine cURL with open-source tools. This guide shows you how to create robust audio processing workflows using cURL and FFmpeg.
Set up your environment
First, ensure you have the necessary tools installed. You'll need cURL (typically pre-installed on
most systems), Bash, and FFmpeg for audio processing. The MP3 and Opus examples require an FFmpeg
build with libmp3lame and libopus, respectively.
Install FFmpeg:
# macOS
brew install ffmpeg
# Debian/Ubuntu
sudo apt-get update
sudo apt-get install ffmpeg
# Fedora
sudo dnf install ffmpeg
# Arch Linux
sudo pacman -S ffmpeg
# Windows
winget install Gyan.FFmpeg
Basic audio encoding with FFmpeg and cURL
Let's start with a simple example of downloading and encoding an audio file:
# Download an audio file
set -euo pipefail
mkdir audio-example
curl -fsSL -o audio-example/input.mp3 https://example.com/audio.mp3
# Encode AAC audio in an M4A container
ffmpeg -nostdin -n -i audio-example/input.mp3 -map 0:a:0 -c:a aac -b:a 192k audio-example/output.m4a
# Upload the encoded file
curl -fsS -F "file=@audio-example/output.m4a" https://example.com/upload
Create an audio encoding script
Save this as encode_audio.sh and run it with Bash. It accepts aac, m4a, mp3, or opus as
the output format and chooses the corresponding encoder and container. Each invocation creates a
unique output directory, so concurrent runs and repeated URLs cannot overwrite earlier results.
#!/bin/bash
set -euo pipefail
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <input_url> <output_format> <output_bitrate>"
echo "Example: $0 https://example.com/audio.mp3 aac 192k"
exit 1
fi
INPUT_URL=$1
OUTPUT_FORMAT=$2
BITRATE=$3
case "$OUTPUT_FORMAT" in
aac) CODEC=aac; CONTAINER=adts ;;
m4a) CODEC=aac; CONTAINER=ipod ;;
mp3) CODEC=libmp3lame; CONTAINER=mp3 ;;
opus) CODEC=libopus; CONTAINER=ogg ;;
*) echo "Unsupported output format: $OUTPUT_FORMAT" >&2; exit 1 ;;
esac
if [[ ! "$BITRATE" =~ ^[1-9][0-9]*k$ ]]; then
echo "Bitrate must be a positive integer followed by k, such as 192k" >&2
exit 1
fi
WORK_DIR=$(mktemp -d ./audio-encode.XXXXXX)
INPUT_FILE="$WORK_DIR/input.audio"
OUTPUT_FILE="$WORK_DIR/output.$OUTPUT_FORMAT"
# Successful runs retain their output; failed runs leave no empty work directory.
trap 'rm -f -- "$INPUT_FILE"; rmdir -- "$WORK_DIR" 2>/dev/null || true' EXIT
# Download the input file
echo "Downloading input file..."
if ! curl -fsSL --retry 3 --proto '=http,https' --proto-redir '=http,https' \
-o "$INPUT_FILE" -- "$INPUT_URL"; then
echo "Error: Failed to download input file" >&2
exit 1
fi
# Encode the audio
echo "Encoding to ${OUTPUT_FORMAT} format..."
if ! ffmpeg -nostdin -n -xerror -i "$INPUT_FILE" -map 0:a:0 -vn \
-c:a "$CODEC" -b:a "$BITRATE" -f "$CONTAINER" "$OUTPUT_FILE"; then
echo "Error: Failed to encode audio" >&2
rm -f -- "$OUTPUT_FILE"
exit 1
fi
echo "Successfully encoded to: ${OUTPUT_FILE}"
Batch processing audio files
Save this batch script beside encode_audio.sh and run it from that directory. Each nonempty line
of the list contains a URL, output format, and bitrate separated by spaces. Percent-encode spaces
inside URLs. Downloads finish before encoding, allowing FFmpeg to seek within the input file.
#!/bin/bash
set -euo pipefail
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <input_file_list.txt>"
echo "File list format: <input_url> <output_format> <bitrate>"
exit 1
fi
INPUT_LIST=$1
failed=0
while IFS=' ' read -r url format bitrate extra || [[ -n "$url" ]]; do
[[ -z "$url" ]] && continue
echo "Processing: ${url}"
if [[ -n "$extra" || -z "$format" || -z "$bitrate" ]]; then
echo "Invalid list entry: $url" >&2
failed=1
elif bash ./encode_audio.sh "$url" "$format" "$bitrate"; then
echo "Success: ${url}"
else
echo "Failed: ${url}" >&2
failed=1
fi
done < "${INPUT_LIST}"
exit "$failed"
Security considerations
When working with remote files and APIs:
# Use environment variables for sensitive data
export API_TOKEN="your_secret_token"
curl -H "Authorization: Bearer ${API_TOKEN}" https://api.example.com/upload
# Verify SSL certificates
curl --cacert /path/to/certificate.pem https://api.example.com/audio
# Rate limiting for batch operations
sleep 1 # Add delay between requests
Error handling and validation
Implement proper error checking in your workflows:
# Check audio file validity
if ! ffmpeg -nostdin -v error -xerror -i input.mp3 -map 0:a:0 -f null -; then
echo "Audio validation failed" >&2
exit 1
fi
# Verify successful upload
if ! HTTP_STATUS=$(curl -sS -o /dev/null -w "%{http_code}" \
-F "file=@output.m4a" https://example.com/upload); then
echo "Upload request failed" >&2
exit 1
fi
if [ "$HTTP_STATUS" -lt 200 ] || [ "$HTTP_STATUS" -ge 300 ]; then
echo "Upload failed with status: ${HTTP_STATUS}"
exit 1
fi
Conclusion
Combining cURL with FFmpeg provides a powerful foundation for audio processing workflows. These tools enable you to create efficient, automated solutions for audio encoding tasks.
For more advanced audio processing capabilities, consider exploring Transloadit's /audio/encode Robot, which provides similar functionality with additional features and scalability.
