TheDeanVanGreunen

Video Overlay on Background Image

Mar 4th, 2022 (edited)
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////
  2. // ENCODING LIBRARY
  3. ////////////////////
  4.  
  5. class Encoding {
  6.  
  7.     constructor(){
  8.         this.error = null;
  9.         this.in_path = null;
  10.         this.out_path = null;
  11.         this.options = ["-loop 1", "-movflags faststart","-c:v libx264","-crf 21","-preset medium", "-r 30","-c:a aac","-ac 2","-strict -2", "-f mp4", '-af volume=6.0','-map [out]', '-map 1:a'];
  12.         this.input_options = ["-sseof -4"];
  13.         this.debug_mode = false;
  14.         this.backgroundFile = false;
  15.         this.watermarkFile = false;
  16.     }
  17.  
  18.     logging(value){
  19.         this.debug_mode = value;
  20.     }
  21.  
  22.     getError(){
  23.         return this.error;
  24.     }
  25.  
  26.     setPaths(in_path, out_path){
  27.         this.in_path = in_path;
  28.         this.out_path = out_path;
  29.     };
  30.  
  31.     setOptions(value){
  32.         this.options = value;
  33.     }
  34.    
  35.     setBackground(backgroundFile){
  36.         this.backgroundFile = backgroundFile;
  37.     }
  38.  
  39.     setWatermark(watermarkFile){
  40.         this.watermarkFile = watermarkFile;
  41.     }
  42.  
  43.     async convert(){
  44.         return new Promise((resolve, reject)=>{
  45.             if(this.debug_mode){
  46.                 console.log("Starting File Conversion");
  47.                 console.log(this.in_path);
  48.                 console.log(this.out_path);
  49.             }
  50.             if(this.watermarkFile){
  51.                 ffmpeg()
  52.                 .input(this.backgroundFile)
  53.                 .input(this.in_path)
  54.                 .complexFilter([
  55.                     "[1]scale=640:480[inner];[0][inner]overlay=0:120:shortest=1[out]"                    
  56.                 ])
  57.                 .outputOptions(this.options)
  58.                 .saveToFile(this.out_path)
  59.                 .on('end', () => {
  60.                     if(this.debug_mode){console.log("Conversion Complete");}
  61.                     resolve(true);
  62.                 })
  63.                 .on('error',(err)=>{
  64.                     if(this.debug_mode){console.log("Conversion Error");}
  65.                     if(this.debug_mode){console.log(err);}
  66.                     resolve(err);
  67.                 });
  68.             } else {
  69.                 ffmpeg()
  70.                 .input(this.backgroundFile)
  71.                 .input(this.in_path)
  72.                 .complexFilter([
  73.                     "[1]scale=640:480[inner];[0][inner]overlay=0:120:shortest=1[out]"
  74.                 ])
  75.                 .outputOptions(this.options)
  76.                 .saveToFile(this.out_path)
  77.                 .on('end', () => {
  78.                     if(this.debug_mode){console.log("Conversion Complete");}
  79.                     resolve(true);
  80.                 })
  81.                 .on('error',(err)=>{
  82.                     if(this.debug_mode){console.log("Conversion Error");}
  83.                     if(this.debug_mode){console.log(err);}
  84.                     resolve(err);
  85.                 });
  86.             }
  87.         });
  88.     }
  89. }
  90.  
  91. ////////////////////
  92. // MAIN FUNCTION
  93. ////////////////////
  94.  
  95. (async()=>{
  96.     let encoder = new Encoding();
  97.     encoder.logging(true);
  98.     encoder.setPaths("./overlay.mp4", "./output.mp4");
  99.     encoder.setWatermark(false);
  100.     encoder.setBackground("./background.png");
  101.     await encoder.convert();
  102. })();
  103.  
  104. ////////////////////
  105. // FILES
  106. ////////////////////
  107. /*
  108.   overlay.mp4  ->  https://storage.googleapis.com/clipdrop-prod/videos/test/overlay.mp4
  109.   background.png -> https://storage.googleapis.com/clipdrop-prod/videos/test/background.png
  110. */
Add Comment
Please, Sign In to add comment