Base64 Voicemail to Audio File Conversion Script

Status
Not open for further replies.

netpro25

New Member
Apr 7, 2019
2
0
1
40
We've been having an issue for quite some time with audio playback being delayed, sometimes up to 15 seconds. I attributed this delay to the fact that FusionPBX has to write the audio file to disk from the database before Freeswitch can play back the audio file as Freeswitch does not have the ability to play a base64 audio file from the database. I was able to find a script to convert to base64 but not from base64 so I wrote up this script.

Key Takeaways
  • The script will write a log file to the temp directory as a CSV so you can see the result of each message conversion.
  • Change $vm_message_ext to whatever format you are using (it's probably MP3), you can find this under Advanced -> Variables
  • The script won't delete the base64 data from the database for safety. (as the end user deletes the messages the base64 data will also get deleted.)
  • You can rerun the script as needed as it will skip over any existing audio files
  • If the script encounters base64 data in the database is empty but not null you'll see a Failure in the log for that conversion.
  • Run the script from Advanced -> Command as PHP.
Let me know if anyone has any suggestions or comments.

PHP:
  // Base64 from DB to Filesystem

  /*
   * The goal of this script is to assist with migrating from base64 postgresql
   * voicemail message storage to file system message storage. After speaking with
   * mcrane on IRC he stated that moving forward FusionPBX uses file based storage
   * as the default and using SyncThing these files can be replicated across servers
   * without the need to rely on Postgresql for syncing between nodes.
   */

  // Get this from Advanced -> Variables, Change if using WAV
  $vm_message_ext = 'mp3';

  $log_file = '/tmp/vm_message_migration'.date("Ymd").'.csv';

  $sql = "select d.domain_name, v.voicemail_id, m.voicemail_message_uuid ";
  $sql .= "from v_domains as d, v_voicemails as v, v_voicemail_messages as m ";
  $sql .= "where m.domain_uuid = d.domain_uuid ";
  $sql .= "and m.voicemail_uuid = v.voicemail_uuid ";
  $sql .= "and m.message_base64 IS NOT null ";

  $prep_statement = $db->prepare($sql);
  $prep_statement->execute();
  $messages = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  unset ($prep_statement, $sql);
  //print_r($messages);

  foreach ($messages as $row) {
    // Let's get the voicemail base64 audio message so we can convert it
    $sql = "select voicemail_message_uuid, message_base64 ";
    $sql .= "from v_voicemail_messages ";
    $sql .= "where voicemail_message_uuid = '" .$row['voicemail_message_uuid']."' ";
    $prep_statement = $db->prepare($sql);
    $prep_statement->execute();
    $message = $prep_statement->fetchAll(PDO::FETCH_ASSOC)[0];
    unset ($prep_statement, $sql);

    $dir = $_SESSION['switch']['voicemail']['dir'].'/default/'.$row['domain_name'].'/'.$row['voicemail_id'];
       $path = $dir.'/msg_'.$row['voicemail_message_uuid'];

    $new_dir = '';
    if (!file_exists($dir)) {
      mkdir($dir, 0770, true);
      $new_dir = 'Created Mailbox Dir';
    }

    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $file_data = base64_decode($message['message_base64']);
    $fileType = $finfo->buffer($file);

    if (!file_exists($path.'.mp3')) {
      $result = file_put_contents($path.'.mp3', $file_data);

      if ($result == FALSE) {
        $log_txt = "'Failure','".$new_dir."'," . $path.'.mp3'."'".PHP_EOL;
      } else {
        $log_txt = "'Success','".$new_dir."'," . $path.'.mp3'."'".PHP_EOL;
      }
      //echo $path.'.mp3';
    } else {
      $log_txt = "'Already Exists','".$new_dir."'," . $path.'.mp3'."'".PHP_EOL;
    }
    file_put_contents($log_file, $log_txt, FILE_APPEND);
  }
 
Status
Not open for further replies.