call_recordings.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. Call Recordings Maintenance
  4. - Convert WAV to MP3
  5. - Reduce the file size
  6. - Move recordings
  7. - Move the recording from the source to a destination directory.
  8. At the top of the file need to define or set the destination_directory...
  9. In my case I put the file in /usr/src and then run manually like this.
  10. /usr/bin/php /usr/src/fusionpbx-install.sh/debian/resources/maintenance/call_recordings.php
  11. Debian
  12. crontab -e
  13. 0 * * * * /usr/bin/php /usr/src/fusionpbx-install.sh/debian/resources/maintenance/call_recordings.php > /dev/null 2>&1
  14. */
  15. //add the document root to the included path
  16. if (defined('STDIN')) {
  17. $config_glob = glob("{/usr/local/etc,/etc}/fusionpbx/config.conf", GLOB_BRACE);
  18. $conf = parse_ini_file($config_glob[0]);
  19. set_include_path($conf['document.root']);
  20. }
  21. else {
  22. exit;
  23. }
  24. //set pre-defined variables
  25. $debug = true;
  26. $action = 'convert'; //convert, move or both
  27. $audio_format = 'wav';
  28. $preferred_command = 'lame'; //mpg123, lame, sox
  29. //includes files
  30. require_once "resources/require.php";
  31. //create the database connection
  32. $database = new database;
  33. //use settings object instead of session
  34. $settings = new settings(['database' => $database]);
  35. //set the source and destination paths
  36. $source_path = $settings->get('switch','recordings', '');
  37. //set the destination_path
  38. if ($action == 'move' || $action == 'both') {
  39. $destination_path = $settings->get('call_recordings','destination_path', null);
  40. }
  41. //make sure the directory exists
  42. if ($action == 'move' || $action == 'both') {
  43. system('mkdir -p '.$destination_path);
  44. }
  45. //get the xml cdr call recordings.
  46. $sql = "select xml_cdr_uuid, domain_uuid, domain_name, ";
  47. $sql .= "record_path, record_name, direction, start_stamp, ";
  48. $sql .= "caller_id_name, caller_id_number from v_xml_cdr ";
  49. //$sql .= "where start_stamp > NOW() - INTERVAL '7 days' ";
  50. $sql .= "where true ";
  51. if ($action == 'convert' || $action == 'both') {
  52. $sql .= "and record_name like '%.wav' ";
  53. }
  54. if ($action == 'move' || $action == 'both') {
  55. $sql .= "and length(record_path) > 0 ";
  56. $sql .= "and substr(record_path, 1, length(:source_path)) = :source_path ";
  57. $parameters['source_path'] = $source_path;
  58. }
  59. $sql .= "order by start_stamp desc ";
  60. if ($debug) { echo $sql."\n"; }
  61. $call_recordings = $database->select($sql, $parameters, 'all');
  62. unset($parameters);
  63. //process the changes
  64. foreach ($call_recordings as $row) {
  65. //set the record_name
  66. $record_name = $row['record_name'];
  67. //set the source_path
  68. $source_path = realpath($row['record_path']);
  69. //get the file name without the file extension
  70. $path_parts = pathinfo($source_path.'/'.$record_name);
  71. //convert the audio file from wav to mp3
  72. if ($action == 'convert' || $action == 'both') {
  73. if ($debug) {
  74. if (!file_exists($source_path."/".$record_name)) {
  75. //echo "file not found: ".$source_path."/".$record_name."\n";
  76. }
  77. else {
  78. echo "found file: ".$source_path."/".$record_name."\n";
  79. }
  80. }
  81. if (file_exists($source_path."/".$record_name)) {
  82. //build the run the sox command
  83. if ($preferred_command == 'sox' && !file_exists($source_path."/".$path_parts['filename'].".mp3")) {
  84. $command = "sox ".$source_path."/".$record_name." -C 128 ".$source_path."/".$path_parts['filename'].".mp3 \n";
  85. if ($debug) { echo $command."\n"; }
  86. system($command);
  87. }
  88. //build the run the mpg123 command
  89. if ($preferred_command == 'mpg123' && !file_exists($source_path."/".$path_parts['filename'].".mp3")) {
  90. $command = "mpg123 -w ".$source_path."/".$record_name." ".$source_path."/".$path_parts['filename'].".mp3\n";
  91. if ($debug) { echo $command."\n"; }
  92. system($command);
  93. }
  94. //build the run the mpg123 command
  95. if ($preferred_command == 'lame' && !file_exists($source_path."/".$path_parts['filename'].".mp3")) {
  96. $command = "lame -b 128 ".$source_path."/".$record_name." ".$source_path."/".$path_parts['filename'].".mp3\n";
  97. if ($debug) { echo $command."\n"; }
  98. system($command);
  99. }
  100. //update the record name to use the new file extension
  101. if (file_exists($source_path."/".$path_parts['filename'].".mp3")) {
  102. //make sure the mp3 file exists and then delete the wav file
  103. unlink($source_path."/".$path_parts['filename'].".wav");
  104. //set the record_name with the new file extension
  105. $record_name = $path_parts['filename'].".mp3";
  106. }
  107. }
  108. }
  109. //move the files
  110. if ($action == 'move' || $action == 'both') {
  111. //get break down the date to year, month and day
  112. $start_time = strtotime($row['start_stamp']);
  113. $start_year = date("Y", $start_time);
  114. $start_month = date("M", $start_time);
  115. $start_day = date("d", $start_time);
  116. //move the recording from the old to the new directory
  117. $old_path = realpath($row['record_path']);
  118. $new_path = realpath($destination_path).'/'.$row['domain_name'].'/archive/'.$start_year.'/'.$start_month.'/'.$start_day;
  119. if (!file_exists($new_path)) { system('mkdir -p '.$new_path); }
  120. $command = "mv ".$old_path."/".$record_name." ".$new_path."/".$record_name;
  121. if ($debug) { echo $command."\n"; }
  122. system($command);
  123. }
  124. //update the database to the new directory
  125. $sql = "update v_xml_cdr set \n";
  126. if ($action == 'move' || $action == 'both') {
  127. $sql .= "record_path = '".$new_path."' \n";
  128. }
  129. if ($action == 'convert' || $action == 'both') {
  130. $sql .= "record_name = '".$path_parts['filename'].".mp3'\n";
  131. }
  132. $sql .= "where xml_cdr_uuid = '".$row['xml_cdr_uuid']."';\n";
  133. if ($debug) { echo $sql."\n"; }
  134. $database->execute($sql);
  135. }
  136. ?>