Sign inSign up

d9media/audiowaveform-server

By d9media

Updated about 2 years ago

Lightweight Docker image for generating waveform JSON from audio, intended for web applications.

Image
Web servers
0

1.2K

d9media/audiowaveform-server repository overview

Audiowaveform Flask Integration

This Docker image integrates the BBC's audiowaveform tool with a Flask server for generating waveform JSON data from audio files (MP3, WAV, etc.) based on Alpine. It supports passing parameters like resolution and bit depth dynamically via an API, ideal for use with applications such as WordPress plugins.

Features

  • Waveform generation: Extract waveform data from audio files and export it as JSON.
  • Dynamic parameters: Control the resolution (-z) and bit depth (-b) through API requests.
  • Lightweight: Based on Alpine Linux to keep the image size small.
  • HTTP API: Simple Flask-based API to integrate with external services like WordPress.

Usage

1. Run the Docker container

To run the Flask API for generating waveforms, execute the following command:

bash
docker run -p 5000:5000 d9media/audiowaveform-server:latest

This will expose the API on port 5000.

2. Test the API using curl
curl -X POST http://localhost:5000/generate_waveform \
  -F "file=@/path/to/your/audiofile.mp3" \
  -F "resolution=4096" \
  -F "bit_depth=8"

Replace /path/to/your/audiofile.mp3 with the actual path to your audio file.

3. Test with PHP
<?php
$url = 'http://localhost:5000/generate_waveform';

// Prepare the file and data to be sent
$file = '/path/to/your/audiofile.mp3';
$fields = array(
    'resolution' => '4096',
    'bit_depth' => '8',
    'file' => new CURLFile($file)
);

// Initialize curl
$ch = curl_init($url);

// Set curl options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if ($response === false) {
    $error = curl_error($ch);
    curl_close($ch);
    die('Curl error: ' . $error);
}

// Close curl
curl_close($ch);

// Decode the JSON response
$responseData = json_decode($response, true);

if ($responseData['status'] == 'success') {
    // Extract the waveform_data
    $waveformData = $responseData['waveform_data'];
    
    // Save the waveform_data to a JSON file
    $outputFile = 'waveform_data.json';
    file_put_contents($outputFile, json_encode($waveformData, JSON_PRETTY_PRINT));
    
    echo "Waveform data saved to $outputFile\n";
} else {
    // Handle the error in the response
    echo "Error: " . $responseData['message'] . "\n";
}
?>

Make sure to replace /path/to/your/audiofile.mp3 with the actual file path.

Explanation:
  1. Send the File: The file is uploaded using new CURLFile($file).
  2. Capture the Response: The response from the API is captured and decoded as JSON using json_decode().
  3. Save to File: The waveform_data is extracted and saved to a file named waveform_data.json using file_put_contents().
  4. Error Handling: If there’s an error in the API response, it’s displayed.

This script will create a file called waveform_data.json in the current working directory, containing the waveform data in a structured JSON format.

Tag summary

Content type

Image

Digest

sha256:16ed3ac1d

Size

216.9 MB

Last updated

about 2 years ago

docker pull d9media/audiowaveform-server