convert.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. FFmpeg Factory ArOZ JavaScript Gateway Interface Converter
  3. This script will launch ffmpeg and convert a given file into the desired format
  4. */
  5. //Require library
  6. requirelib("filelib");
  7. //Globaal variables
  8. var targetFilepath = filepath;
  9. var conversionCommand = command;
  10. //Helper function to get the filepath.Dir of the realpath
  11. function dir(filepath){
  12. filepath = filepath.split("/");
  13. filepath.pop();
  14. return filepath.join("/");
  15. }
  16. //Return the filename of the path without extension
  17. function base(filepath){
  18. filepath = filepath.split("\\").join("/");
  19. filepath = filepath.split("/");
  20. filename = filepath.pop();
  21. filename = filename.split(".")
  22. filename.pop();
  23. return filename.join(".");
  24. }
  25. //Package required. Get the real path of the file
  26. if (filelib.fileExists(targetFilepath)){
  27. var srcReal = decodeVirtualPath(targetFilepath);
  28. srcReal = srcReal.split("\\").join("/");
  29. //Parse the command for the conversion
  30. var actualCommand = decodeURIComponent(command);
  31. actualCommand = actualCommand.replace('{filepath}',srcReal);
  32. actualCommand = actualCommand.replace('{filename}',dir(srcReal) + "/" + base(srcReal))
  33. //Register this task in on-going task list
  34. newDBTableIfNotExists("FFmpeg Factory")
  35. var ts = Math.round((new Date()).getTime() / 1000);
  36. var taskKey = USERNAME + "/" + ts;
  37. writeDBItem("FFmpeg Factory",taskKey,targetFilepath)
  38. //Pass the command into ffmpeg pkg
  39. var results = execpkg("ffmpeg",actualCommand);
  40. //Deregister this task from on-going task list
  41. deleteDBItem("FFmpeg Factory",taskKey,targetFilepath);
  42. sendJSONResp(JSON.stringify({
  43. status: "ok",
  44. execresults: results
  45. }));
  46. }else{
  47. //File not exists. Return an error json
  48. sendJSONResp(JSON.stringify({
  49. error: "File not exists: " + targetFilepath
  50. }));
  51. }