Advertisement
AyrA

__frametools.js

Nov 1st, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. //maximum filesize in bytes
  4. var MAX_SIZE=3000000;
  5. //Percentage of bitrate to subtract (use at least 2)
  6. var SPACE=2;
  7. //Factor of bitrate (125 converts bytes to kbits (x*8/1000)
  8. var FACTOR=125;
  9. //Unit of bitrate (for display purposes only)
  10. var UNIT="k";
  11. //Upper [0] and lower [1] bitrate cap.
  12. var CAP=[1000,100];
  13.  
  14. //You can set this to true or false. The default is to check for "-v" argument
  15. var VERBOSE=process.argv.slice(2).map(function(v){return v.toLowerCase();}).indexOf("-v")>=0;
  16.  
  17. //////////////////////
  18. //                  //
  19. // no changes below //
  20. //                  //
  21. //////////////////////
  22.  
  23. var FILENAME=process.argv.slice(2).filter(function(v){return v.toLowerCase()!="-v";})[0];
  24.  
  25. if(!FILENAME)
  26. {
  27.     console.log("node",nameOnly(__filename),"<filename> [-v]");
  28.     process.exit(2);
  29. }
  30.  
  31. function nameOnly(x)
  32. {
  33.     return x=x.split('/').slice(-1)[0].split('\\').slice(-1)[0];
  34. }
  35.  
  36. function r(x,d)
  37. {
  38.     var factor=Math.pow(10, d);
  39.     var tempNumber=x*factor;
  40.     var roundedTempNumber=Math.round(tempNumber);
  41.     return roundedTempNumber/factor;
  42. }
  43.  
  44. function niceSize(o,f)
  45. {
  46.     var units=["Bytes","KB","MB","GB","TB"];
  47.    
  48.     if(!f)
  49.     {
  50.         f=1000;
  51.     }
  52.    
  53.     var current=o;
  54.     var index=0;
  55.     while(current>=f && index++<units.length-1)
  56.     {
  57.         current/=f;
  58.     }
  59.     return r(current,1)+" "+units[index];
  60. }
  61.  
  62. var path=require("path");
  63. var spawn=require("child_process").spawn;
  64. var proc=spawn(path.join(__dirname,"../ffprobe"),[
  65. "-show_entries",
  66. "format=duration",
  67. "-of",
  68. "default=noprint_wrappers=1:nokey=1",
  69. "-v",
  70. "quiet",
  71. process.argv[2]
  72. ]);
  73.  
  74. SPACE=(100-SPACE)/100;
  75. if(SPACE>1.0 || SPACE<=0.0)
  76. {
  77.     console.log("Invalid percentage (must be in the range 0-99)");
  78.     process.exit(1);
  79. }
  80.  
  81. proc.stdout.on("data",function(data)
  82. {
  83.     var Duration=+data.toString();
  84.     if(Duration)
  85.     {
  86.         var Bitrate=MAX_SIZE/Duration/FACTOR*SPACE|0;
  87.         if(VERBOSE)
  88.         {
  89.             console.log("Duration    :",Duration,"seconds");
  90.             console.log("Raw bitrate :",Bitrate+"k");
  91.         }
  92.         if(Bitrate>CAP[0])
  93.         {
  94.             Bitrate=CAP[0];
  95.             if(VERBOSE)
  96.             {
  97.                 console.log("Bitrate too big. Capped to",CAP[0]);
  98.                 console.log(
  99.                     "File size will be",niceSize(Bitrate*Duration*FACTOR),
  100.                     "instead of",niceSize(MAX_SIZE),
  101.                     "("+r(Bitrate*Duration*FACTOR/MAX_SIZE*100,1)+"% of original)");
  102.             }
  103.         }
  104.         else if(Bitrate<CAP[1])
  105.         {
  106.             Bitrate=CAP[1];
  107.             if(VERBOSE)
  108.             {
  109.                 console.log("Bitrate too small. Capped to",CAP[1]);
  110.                 console.log(
  111.                     "File size will be",niceSize(Bitrate*Duration*FACTOR),
  112.                     "instead of",niceSize(MAX_SIZE),
  113.                     "("+r(Bitrate*Duration*FACTOR/MAX_SIZE*100,1)+"% of original)");
  114.             }
  115.         }
  116.         console.log("Recommended :",Bitrate+UNIT);
  117.     }
  118.     else
  119.     {
  120.         console.log("Error estimating bitrate. Invalid input file (maybe image)?");
  121.     }
  122. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement