Skip to content

Instantly share code, notes, and snippets.

@terwey
Created November 29, 2025 16:40
Show Gist options
  • Select an option

  • Save terwey/22fe4579db75ef317595dc8f2327c09d to your computer and use it in GitHub Desktop.

Select an option

Save terwey/22fe4579db75ef317595dc8f2327c09d to your computer and use it in GitHub Desktop.
A simple shell script that sends an audio file to OpenAI’s transcription API and saves the result as JSON. Supports both in-script and environment-based API keys, with minimal setup for non-technical users.

README

Overview

This script sends an audio file to OpenAI for transcription and stores the result in a JSON file. It works on macOS and Linux and doesn't require programming knowledge.

Requirements

Requirement Description
OpenAI API key Can be stored in the script or set in the environment.
macOS or Linux terminal Needed to run the script.
curl Usually preinstalled.

Setup

  1. Download the script Save transcribe.sh anywhere you like.

  2. Make it executable

    chmod +x transcribe.sh
    
  3. Provide your OpenAI API key You have two options:

    Option How
    Set it inside the script Edit the line API_KEY_IN_FILE="" and put your key inside the quotes.
    Use an environment variable Run export OPENAI_API_KEY="your_key" in the terminal.

    If both are provided, the key inside the script takes priority.

How to Use

./transcribe.sh <audio-file> <output-json-file>

Example:

./transcribe.sh "Downloads/New Recording 32.m4a" transcription.json

A JSON file will be created with the transcription text and metadata.

Output

The output file is standard JSON and can be opened with any text editor.

#!/usr/bin/env sh
set -eu
# Optional: set your API key here instead of using an environment variable.
# Leave it empty ("") to force the script to use $OPENAI_API_KEY.
API_KEY_IN_FILE=""
if [ "$#" -ne 2 ]; then
echo "usage: $0 <input-audio-file> <output-json-file>" >&2
exit 1
fi
INPUT="$1"
OUTPUT="$2"
if [ ! -f "$INPUT" ]; then
echo "error: input file not found: $INPUT" >&2
exit 1
fi
# Select the key: file override > env var
API_KEY="${API_KEY_IN_FILE:-}"
if [ -z "$API_KEY" ]; then
API_KEY="${OPENAI_API_KEY:-}"
fi
if [ -z "$API_KEY" ]; then
echo "error: no API key provided. Set it in the script or via OPENAI_API_KEY." >&2
exit 1
fi
curl https://api.openai.com/v1/audio/transcriptions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: multipart/form-data" \
-F "file=@${INPUT}" \
-F "model=gpt-4o-transcribe" \
-o "$OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment