acrcloud / acrcloud_sdk_php

27 stars 18 forks source link

Audio Recognition PHP SDK (php version 5.X)

Overview

ACRCloud provides services such as Music Recognition, Broadcast Monitoring, Custom Audio Recognition, Copyright Compliance & Data Deduplication, Live Channel Detection, and Offline Recognition etc.

This audio recognition PHP SDK support most of audio / video files.

Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
Video: mp4, mkv, wmv, flv, ts, avi ...

Requirements

Follow one of the tutorials to create a project and get your host, access_key and access_secret.

Windows Runtime Library

If you run the SDK on Windows, you must install this library.
X86: download and install Library(windows/vcredist_x86.exe)
x64: download and install Library(windows/vcredist_x64.exe)

Note

  1. If you run the SDK on Windows, you must install library(vcredist).

Install modules

Note: If you use nginx/apache, you can add "phpinfo()" in your code, and find extension dir and the path of "php.ini" from the result info

  1. Find your extension dir, run(this is default extension dir):
    $ php -ini | grep "extension_dir"
    extension_dir => /usr/lib64/php/modules => /usr/lib64/php/modules
  2. Put "acrcloud_extr_tool.so" to /usr/lib64/php/modules;(Your extension dir)
  3. Find your path of php.ini file:
    $ php -ini | grep "php.ini" 
    Loaded Configuration File => /etc/php.ini
    If you use this SDK in Web Server, you need to find "php.ini" by "phpinfo()".

    image

  4. Modify file "/etc/php.ini"(Your php.ini)

    extension=acrcloud_extr_tool.so

Functions

Introduction all API.

test.php

    class ACRCloudRecognizer {
        /**
          *
          *  recognize by file path of (Audio/Video file)
          *          Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
          *          Video: mp4, mkv, wmv, flv, ts, avi ...
          *
          *  Notice: this function read max 12 seconds from "startSeconds of input file" and only recognize once.
          *
          *
          *  @param filePath query file path
          *  @param startSeconds skip (startSeconds) seconds from from the beginning of (filePath)
          *  
          *  @return result metainfos https://docs.acrcloud.com/metadata
          *
          **/
        public function recognizeByFile($filePath, $startSeconds);

       /**
         *
         *  recognize by buffer of (Audio/Video file)
         *          Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
         *          Video: mp4, mkv, wmv, flv, ts, avi ...

         *  Notice: this function read max 12 seconds from "startSeconds of input file" and only recognize once.
         *
         *  @param fileBuffer query buffer
         *  @param startSeconds skip (startSeconds) seconds from from the beginning of fileBuffer
         *  
         *  @return result metainfos https://docs.acrcloud.com/metadata
         *
         **/
         public function recognizeByFileBuffer($fileBuffer, $startSeconds);

        /**
          *
          *  recognize by wav audio buffer(RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 8000 Hz) 
          *
          *  @param wavAudioBuffer query audio buffer
          *  
          *  @return result metainfos https://docs.acrcloud.com/metadata
          *
          **/
        public function recognize($wavAudioBuffer);
    }

Example

run Test: php test.php test.mp3

<?php namespace ACRCloud;
    include_once('acrcloud_recognizer.php');

    // Replace "xxxxxxxx" below with your project's host, access_key and access_secret.
    $config = array(
        'host' => 'XXX',
        'access_key' => 'XXX',
        'access_secret' => 'XXX'
    );

    // recognize by file path, and skip 0 seconds from from the beginning of sys.argv[1].
    // Notice: this function read max 12 seconds from "startSeconds of input file" and only recognize once.
    $re = new ACRCloudRecognizer($config);
    print $re->recognizeByFile($argv[1], 0);

    // recognize by file_audio_buffer that read from file path, and skip 0 seconds from from the beginning of sys.argv[1].
    // Notice: this function read max 12 seconds from "startSeconds of input file" and only recognize once.
    $content = file_get_contents($argv[1]);
    print $re->recognizeByFileBuffer($content, 0);

    // If need scan a audio file, you can refer to this code.
    $file_duration_ms = ACRCloudExtrTool::getDurationFromFile($argv[1]);
    for ($startSeconds=0; $startSeconds<$file_duration_ms/1000; $startSeconds=$startSeconds+12) {
        print $re->recognizeByFile($argv[1], $startSeconds);
    }
?>