Skip to main content

Audio Conversion Examples

SwissKnife supports high-quality audio conversion between popular formats using FFmpeg.

MP3 to FLAC (Lossless)

Convert compressed MP3 to lossless FLAC format:
python solution.py convert song.mp3 lossless.flac
Expected Output:
Converting: /path/to/song.mp3 to /path/to/lossless.flac
Success: Audio conversion successful: /path/to/lossless.flac
Info: Conversion completed in 3.21 seconds.
FLAC (Free Lossless Audio Codec) provides perfect audio quality preservation without compression artifacts, ideal for archiving or audiophile use.

WAV to AAC (Compression)

Compress uncompressed WAV files to efficient AAC format:
python solution.py convert podcast.wav compressed.aac
AAC encoding uses 192k bitrate for high-quality compression suitable for podcasts and music streaming.

M4A to OGG

Convert Apple’s M4A format to open-source OGG Vorbis:
python solution.py convert recording.m4a universal.ogg
OGG Vorbis offers excellent compression and is widely supported across platforms.

Video Conversion Examples

MP4 to WebM (Web Optimization)

Convert videos to WebM format optimized for web streaming:
python solution.py convert movie.mp4 optimized.webm
Expected Output:
Converting: /path/to/movie.mp4 to /path/to/optimized.webm
Success: Video conversion successful: /path/to/optimized.webm
Info: Conversion completed in 45.67 seconds.
WebM uses VP9 video codec and Opus audio codec for excellent compression and quality.

AVI to MOV (Apple Format)

Convert AVI files to Apple’s MOV format:
python solution.py convert presentation.avi portable.mov
MOV files use H.264 video codec with faststart flag for quick loading on Apple devices.

MKV to GIF (Animation)

Create optimized GIF animations from video files:
python solution.py convert tutorial.mkv social.gif
GIF conversion automatically optimizes for social media: 480px width, 10 fps, optimized color palette. Perfect for short clips and demonstrations.

Audio Codec Settings

SwissKnife uses optimized codec settings for each audio format (from solution.py:76):
codec_map = {
    ".mp3": ("libmp3lame", "192k"),  # MP3 with LAME encoder
    ".m4a": ("aac", "192k"),         # AAC in M4A container
    ".aac": ("aac", "192k"),         # AAC format
    ".wav": ("pcm_s16le", None),     # Uncompressed PCM
    ".ogg": ("libvorbis", "192k"),  # Vorbis in OGG container
    ".flac": ("flac", None)          # Lossless FLAC
}

Audio Quality Levels

FormatCodecBitrateQualityUse Case
FLACflacLosslessPerfectArchival, audiophile
WAVPCMUncompressedPerfectProfessional audio
MP3libmp3lame192kHighGeneral music
AACaac192kHighStreaming, podcasts
M4Aaac192kHighApple devices
OGGlibvorbis192kHighOpen-source preference

Video Codec Settings

Video conversions use format-specific optimizations (from solution.py:80):
fmt_map = {
    ".webm": (
        "libvpx-vp9",           # VP9 video codec
        ["-crf", "30", "-b:v", "0"],  # Quality settings
        "libopus",              # Opus audio codec
        ["-b:a", "128k"]
    ),
    ".mp4": (
        "libx264",              # H.264 video codec
        ["-crf", "23", "-preset", "ultrafast"],
        "aac",                  # AAC audio codec
        ["-b:a", "128k"],
        ["-movflags", "+faststart"]  # Fast start for streaming
    )
}

Video Quality Parameters

CRF (Constant Rate Factor):
  • Lower CRF = Higher quality, larger file size
  • WebM uses CRF 30 for web optimization
  • MP4/MKV/AVI/MOV use CRF 23 for balanced quality
Presets:
  • ultrafast - Fast encoding, larger files
  • Used for quick conversions and preview generation
Audio Bitrate:
  • 128k for video files (sufficient for most content)
  • 192k for audio-only files (higher quality)

GIF Creation from Video

GIF conversion uses a two-pass process for optimal quality (from solution.py:71-74):
python solution.py convert video.mp4 animation.gif
Process:
  1. Palette Generation: Creates optimized color palette
    • fps=10 - 10 frames per second
    • scale=480:-1 - 480px width, proportional height
    • palettegen=stats_mode=diff - Optimized for differences
  2. GIF Encoding: Uses generated palette
    • paletteuse=dither=bayer:bayer_scale=5 - Bayer dithering
    • Results in smooth color transitions
GIF files can become very large. The 480px width and 10 fps limits keep file sizes manageable for social media and web use. For longer videos, consider using WebM instead.

Batch Media Conversion

Convert entire directories of media files:
# Convert all MP3s to FLAC
python solution.py batch-convert ./music ./flac_archive .mp3 .flac

# Convert all MP4 videos to WebM
python solution.py batch-convert ./videos ./web_videos .mp4 .webm

# Create GIFs from all video files
python solution.py batch-convert ./tutorials ./gifs .mkv .gif

Supported Audio Formats

  • MP3 - Most compatible format
  • WAV - Uncompressed, professional use
  • FLAC - Lossless compression
  • AAC - Modern, efficient compression
  • OGG - Open-source, good compression
  • M4A - Apple ecosystem

Supported Video Formats

  • MP4 - Universal compatibility
  • AVI - Legacy format support
  • MKV - High-quality container
  • MOV - Apple QuickTime format
  • WMV - Windows Media Video
  • FLV - Flash Video (legacy)
  • WebM - Modern web format
  • GIF - Animated images (output only)

Best Practices

For Audio

Archive in lossless: Use FLAC or WAV for master copies, then convert to compressed formats as needed.
192k bitrate is the sweet spot: For MP3, AAC, and OGG, 192k provides excellent quality while keeping file sizes reasonable.

For Video

Use WebM for web: WebM provides better compression than MP4 for web streaming with similar quality.
MP4 for compatibility: When you need maximum device compatibility, use MP4 with H.264 codec.
Keep GIFs short: Due to large file sizes, limit GIFs to 5-10 seconds of content. Use the split command first if needed.

Performance Considerations

  • Video conversion is slow: Expect 30-60 seconds per minute of video
  • Audio conversion is fast: Most audio files convert in 2-5 seconds
  • GIF creation takes time: Two-pass encoding means longer processing
  • File sizes: Video conversions can produce large files (100MB+ for HD content)

Common Use Cases

1. Music Library Conversion

# Convert entire music library to FLAC for archival
python solution.py batch-convert ./Music ./FLAC_Archive .mp3 .flac

2. Podcast Production

# Convert WAV recordings to AAC for distribution
python solution.py convert raw_recording.wav episode_01.aac

3. Social Media Clips

# Create GIF from video for Twitter/Discord
python solution.py convert highlight.mp4 share.gif

4. Web Video Optimization

# Convert videos to WebM for website embedding
python solution.py batch-convert ./raw_videos ./optimized .mp4 .webm

5. Apple Ecosystem

# Convert audio to M4A for iTunes/Apple Music
python solution.py convert song.flac itunes.m4a

# Convert video to MOV for iMovie/Final Cut
python solution.py convert footage.mkv editable.mov

Build docs developers (and LLMs) love