Advertisement
Watrik

Untitled

Feb 21st, 2023 (edited)
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package ru.bump.util.media;
  2.  
  3. import org.codehaus.jackson.annotate.JsonProperty;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import ru.bump.core.util.ArgumentAssertUtil;
  7. import ru.bump.core.util.JsonUtils;
  8. import ru.bump.util.FileUtils;
  9. import ru.bump.util.files.FileUtil;
  10. import ru.bump.util.process.ProcessRunException;
  11. import ru.bump.util.process.ProcessRunner;
  12. import ru.bump.util.string.StringUtil;
  13.  
  14. import java.io.IOException;
  15. import java.util.List;
  16. import java.util.concurrent.TimeoutException;
  17.  
  18.  
  19. public final class WaveFormUtil {
  20.  
  21.     private static final Logger LOG = LoggerFactory.getLogger(WaveFormUtil.class);
  22.  
  23.     private static final String MP3_TO_WAV_UTILITY_NAME = "ffmpeg";
  24.  
  25.     private static final String WAV2JSON_UTILITY_NAME = "wav2json";
  26.  
  27.     private static final long SAMPLES_COUNT = 680;
  28.  
  29.     // -------------------------------------------------------
  30.     // -                        LOGIC                        -
  31.     // -------------------------------------------------------
  32.  
  33.     private WaveFormUtil() {
  34.     }
  35.  
  36.     public static String getWaveformJson(String filePath) {
  37.         ArgumentAssertUtil.emptyStringCheck("filePath", filePath);
  38.         java.io.File tempFile = FileUtil.getNewTempFile("wav");
  39.         String tempFilePath = tempFile.getAbsolutePath();
  40.         Boolean succeed = false;
  41.  
  42.         try {
  43.             new ProcessRunner(MP3_TO_WAV_UTILITY_NAME)
  44.                     .addParameter("-i", filePath)
  45.                     .addParameter("-acodec", "pcm_s16le")
  46.                     .addParameter("-ac", "1")
  47.                     .addParameter("-ar", "8000")
  48.                     .addParameter("-f", "wav")
  49.                     .addParameter(tempFilePath)
  50.                     .run();
  51.             succeed = true;
  52.         } catch (IOException e) {
  53.             LOG.error("Error decoding mp3. I/O problem. Parameter = `{}`.", e, filePath);
  54.         } catch (InterruptedException e) {
  55.             LOG.error("Error decoding mp3. Process is interrupted. Parameter = `{}`.", e, filePath);
  56.         } catch (ProcessRunException e) {
  57.             LOG.error("Error decoding mp3. Parameter = `{}`.", e, filePath);
  58.         } catch (TimeoutException e) {
  59.             LOG.error("Error decoding mp3. Timeout is expired. Parameter = `{}`.", e, filePath);
  60.         }
  61.  
  62.         if (succeed) {
  63.             java.io.File resultFile = FileUtil.getNewTempFile();
  64.             try {
  65.                 new ProcessRunner(WAV2JSON_UTILITY_NAME)
  66.                         .addParameter(tempFilePath)
  67.                         .addParameter("--channels", "left")
  68.                         .addParameter("-p", "2")
  69.                         .addParameter("-n")
  70.                         .addParameter("-s", String.valueOf(SAMPLES_COUNT))
  71.                         .addParameter("-o", resultFile.getAbsolutePath())
  72.                         .run();
  73.  
  74.                 String samplesJson = FileUtils.readFile(resultFile);
  75.  
  76.                 if (!StringUtil.isEmpty(samplesJson)) {
  77.                     Wav2jsonPresenter samples = JsonUtils.deserializeFromJson(samplesJson, Wav2jsonPresenter.class);
  78.                     return JsonUtils.serializeToJson(samples.left);
  79.                 }
  80.             } catch (IOException e) {
  81.                 LOG.error("Error processing wav to json. I/O problem. Parameter = `{}`.", e, tempFilePath);
  82.             } catch (InterruptedException e) {
  83.                 LOG.error("Error processing wav to json. Process is interrupted. Parameter = `{}`.", e, tempFilePath);
  84.             } catch (ProcessRunException e) {
  85.                 LOG.error("Error processing wav to json. Parameter = `{}`.", e, tempFilePath);
  86.             } catch (TimeoutException e) {
  87.                 LOG.error("Error processing wav to json. Timeout is expired. Parameter = `{}`.", e, tempFilePath);
  88.             } finally {
  89.                 if (tempFile.exists()) {
  90.                     tempFile.delete();
  91.                 }
  92.                 if (resultFile.exists()) {
  93.                     resultFile.delete();
  94.                 }
  95.             }
  96.         } else {
  97.             if (tempFile.exists()) {
  98.                 tempFile.delete();
  99.             }
  100.         }
  101.  
  102.         return null;
  103.     }
  104.  
  105.     private static class Wav2jsonPresenter {
  106.  
  107.         @JsonProperty
  108.         private List<Double> left;
  109.  
  110.         // -------------------------------------------------------
  111.         // -                      GET & SET                      -
  112.         // -------------------------------------------------------
  113.  
  114.         public List<Double> getLeft() {
  115.             return left;
  116.         }
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement