Guest User

Untitled

a guest
Dec 16th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #!/usr/bin/env node
  2. let inputDir,
  3. outputDir,
  4. help = false;
  5. const path = require("path");
  6. const fs = require("fs");
  7. const exec = require("child_process").execSync;
  8. //\.?(\/.*\/)(\S+$)
  9.  
  10. docs();
  11. check_arguments();
  12.  
  13. function readFiles() {
  14. fs.readdir(inputDir, (err, files) => {
  15. if (!err) {
  16. files.map(file => {
  17. let filePath = path.join(inputDir, file);
  18. let stat = fs.statSync(filePath);
  19. if (
  20. stat &&
  21. stat.isFile() &&
  22. [".flac", ".m4a"].indexOf(path.extname(filePath)) >= 0
  23. ) {
  24. let extension = path.extname(filePath);
  25. let name = path.basename(filePath);
  26. let orname = name;
  27. let regex = new RegExp("\\" + extension, "g");
  28. name = name.replace(regex, ".mp3");
  29. orname = orname.replace(regex, ".mp3");
  30. name = bash_parse(name);
  31. filePath = bash_parse(filePath);
  32. outputDir = bash_parse(outputDir);
  33. if (
  34. !fs.existsSync(path.join(outputDir, orname)) &&
  35. !fs.existsSync(path.join(outputDir, `${orname}.finished`))
  36. ) {
  37. console.log("Aun no se ha convertido este archivo");
  38. exec(
  39. `ffmpeg -i ${filePath} ${outputDir}/${name} -ab 320k && touch ${outputDir}/.${name}.finished`
  40. ).toString();
  41. } else {
  42. console.log(
  43. `El archivo ${orname} ya ha sido convertido re visa la carpeta ${outputDir} `
  44. );
  45. if (
  46. fs.existsSync(outputDir + `/${orname}`) &&
  47. !fs.existsSync(outputDir + `/.${orname}.finished`)
  48. ) {
  49. fs.unlinkSync(outputDir + `/${orname}`);
  50. exec(
  51. `ffmpeg -i ${filePath} ${outputDir}/${name} && touch ${outputDir}/.${name}.finished`
  52. ).toString();
  53. }
  54. }
  55. }
  56. });
  57. }
  58. });
  59. }
  60.  
  61. function docs() {
  62. if (process.argv[2] == "--help") {
  63. console.log(
  64. "Script para convertir cualquier formato de audio a mp3"
  65. );
  66. console.log("Ejemplo: <script> <input dir> <output dir>");
  67. console.log("<script> es equivalente a el punto de incio del script");
  68. console.log(
  69. "<input dir> es equivalente a el punto de inicio donde se buscaran archivos de audio"
  70. );
  71. console.log(
  72. "<output dir> es equivalente a el punto de salida donde se colocaran los archivos resultantes"
  73. );
  74. help = true;
  75. }
  76. }
  77.  
  78. function check_arguments() {
  79. if (
  80. typeof process.argv[2] !== "undefined" &&
  81. typeof process.argv[3] !== "undefined"
  82. ) {
  83. if (
  84. process.argv[2].match(/^\.?(\/.*\/?)$/g) &&
  85. process.argv[3].match(/^\.?(\/.*\/?)$/g)
  86. ) {
  87. inputDir = process.argv[2];
  88. outputDir = process.argv[3];
  89. if (
  90. fs.statSync(inputDir).isDirectory() &&
  91. fs.statSync(outputDir).isDirectory()
  92. ) {
  93. readFiles();
  94. console.log("Ambos son directorios");
  95. }
  96. console.log("inputDir :", inputDir);
  97. console.log("outputDir :", outputDir);
  98. } else {
  99. console.log("Debes colocar un directorio de input y output");
  100. }
  101. } else {
  102. if (!help) {
  103. console.log("Debes colocar un directorio de input y output");
  104. }
  105. }
  106. }
  107.  
  108. function bash_parse(txt) {
  109. /*
  110. Normaliza un string para que se pueda ejecutar en terminal
  111. Eliminando espacios y caracteres especiales
  112. */
  113. txt = txt
  114. .replace(/\s/g, "\\ ")
  115. .replace(/\(/g, "\\(")
  116. .replace(/\)/g, "\\)")
  117. .replace(/'/g, "\\'")
  118. .replace(/&/g, "\\&")
  119. .replace(/\$/g, "\\$");
  120.  
  121. return txt;
  122. }
Add Comment
Please, Sign In to add comment