Advertisement
Guest User

Untitled

a guest
Oct 14th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.io.OutputStreamWriter;
  8. import java.util.ArrayList;
  9.  
  10.  
  11. public class java_video_audio_werbecutter {
  12.  
  13.     /**
  14.      * @param args
  15.      */
  16.     public static void main(String[] args) {
  17.  
  18.         if (args.length <3){
  19.             System.out.println("Zu wenig argumente");
  20.             System.out.println("Benötige Inputfilename, encode file und Outputfilename");
  21.         }
  22.         else{
  23.         String[] candidates = write_ffmpeg_info(args[0]);
  24.         Double[] positions = get_positions(candidates,args[0]);
  25.         write_encode_file(positions,args[1],args[0],args[2]);
  26.        
  27.         }
  28.        
  29.     }
  30.  
  31.     private static void write_encode_file(Double[] positions, String outputfilename, String inputfilename, String outputvideo) {
  32.  
  33.         try {
  34.             BufferedWriter out = new BufferedWriter (new FileWriter(outputfilename));
  35.             out.write("#"+inputfilename+"\n");
  36.             for (int i=0;i<positions.length;i++){
  37.                 out.write("#"+positions[i]+"\n");
  38.             }
  39.            
  40.             int endpos=0;
  41.             if ( positions.length / 2 == 0){
  42.                 endpos=positions.length;
  43.             }
  44.             else{
  45.                 endpos=positions.length-1;
  46.             }
  47.             for (int i=0;i<endpos;i=i+2){
  48.                
  49.                 //double startpos = get_realstartpos(positions[i-0.5]);
  50.                 String out_string="ffmpeg -ss "+Double.toString(positions[i]-10)+" -i \""+inputfilename+"\" -ss 10 -t "+Double.toString(positions[i+1]-positions[i])+"  -acodec flac -vcodec libx264 -coder 1 -flags +loop -partitions +parti8x8+parti4x4+partp8x8+partp4x4+partb8x8 -me_method umh -subq 10 -me_range 24 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 2 -qcomp 0.6 -qmin 0 -qmax 69 -qdiff 4 -bf 8 -refs 4 -trellis 2 -level 41 -crf 24 -threads 0 -async 1000 ";
  51.                 out_string=out_string+"/tmp/"+i+".mkv\n";
  52.                 out.write(out_string);
  53.             }
  54.             out.write("mkvmerge -o /tmp/dummy.mkv /tmp/0.mkv");
  55.             for (int i=2;i<endpos;i=i+2){
  56.                 out.write(" + /tmp/"+i+".mkv");
  57.             }
  58.             out.write("\n");
  59.             out.write("ffmpeg -i /tmp/dummy.mkv -vcodec copy -acodec libfaac -ab 160k \""+outputvideo+"\"\n");
  60.             out.flush();
  61.             out.close();
  62.         } catch (IOException e) {
  63.             // TODO Auto-generated catch block
  64.             e.printStackTrace();
  65.         }
  66.  
  67.        
  68.     }
  69.  
  70.  
  71.  
  72.     private static Double[] get_positions(String[] candidates, String videofilename) {
  73.         ArrayList<Double> output = new ArrayList<Double>();
  74.         for(int i=0;i<candidates.length;i++){
  75.             String[] data = candidates[i].split(":");
  76.             double a = Double.parseDouble(data[0])*3600;
  77.             double b = Double.parseDouble(data[1])*60;
  78.             double c = Double.parseDouble(data[2]);
  79.             double summe=a+b+c;
  80.             double outi = check_candidate(summe,0,videofilename);
  81.             output.add(outi);
  82.         }
  83.         Double[] arr = output.toArray(new Double[output.size()]);
  84.         return arr;
  85.     }
  86.  
  87.     private static double check_candidate(double start, double i, String videofilename) {
  88.         System.out.println("Check start: "+ Double.toString(start)+" offset "+ Double.toString(i));
  89.         double start1= start -10;
  90.         double start2= 10 + i ;
  91.         String start1_string = Double.toString(start1);
  92.         String start2_string = Double.toString(start2);
  93.         String[] befehl = new String[] {"ffmpeg","-ss",start1_string,"-i",videofilename,"-t",start2_string,"-f","wav","/dev/null"};
  94.         try {
  95.         ProcessBuilder builder = new ProcessBuilder(befehl);
  96.         builder.redirectErrorStream(true);
  97.         Process process = builder.start();
  98.  
  99.         OutputStream os = process.getOutputStream();
  100.         OutputStreamWriter osr = new OutputStreamWriter(os);
  101.        
  102.         BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
  103.         boolean found=false;
  104.         osr.write("y\n");
  105.         osr.flush();
  106.         String line;
  107.         while ((line = br.readLine()) != null) {
  108.             if (line.contains("frame changed")){
  109.                 found=true;
  110.              }
  111.         }
  112.         if (found){
  113.             return 0;
  114.         }
  115.         else{
  116.             double a = check_candidate(start,(i+(double)0.1),videofilename);
  117.             if (0== a){
  118.                 System.out.println("Found!!!: "+Double.toString(start+i));
  119.                 return start+i;
  120.             }
  121.             else{
  122.                 return a;
  123.             }
  124.                
  125.         }
  126.         }
  127.        
  128.         catch (IOException e1) {
  129.             // TODO Auto-generated catch block
  130.             e1.printStackTrace();
  131.         }
  132.         return 0;
  133.     }
  134.  
  135.     private static String[] write_ffmpeg_info(String videofilename) {
  136.         System.out.println("Video Input file: " + videofilename);
  137.         try {
  138.         String[] befehl = new String[] {"ffmpeg","-i",videofilename,"-f","wav","/dev/null"};
  139.         ProcessBuilder builder = new ProcessBuilder(befehl);
  140.         builder.redirectErrorStream(true);
  141.         Process process = builder.start();
  142.  
  143.         OutputStream os = process.getOutputStream();
  144.         OutputStreamWriter osr = new OutputStreamWriter(os);
  145.        
  146.         BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
  147.        
  148.         try {
  149.             Thread.sleep(500);
  150.         } catch (InterruptedException e1) {
  151.             // TODO Auto-generated catch block
  152.             e1.printStackTrace();
  153.         }
  154.         osr.write("y\n");
  155.         osr.flush();
  156.        
  157.         String line;
  158.         ArrayList<String> output = new ArrayList<String>();
  159.         String xy = null;
  160.         while ((line = br.readLine()) != null) {
  161.              
  162.              if (line.startsWith("size=")){
  163.                  xy=(line.substring(line.indexOf("time=")+5,line.indexOf("time=")+13));
  164.              }
  165.              if (line.contains("frame changed")){
  166.                  System.out.println("Candidate found: "+xy);
  167.                  output.add(xy);
  168.              }
  169.              
  170.            }
  171.         String[] arr = output.toArray(new String[output.size()]);
  172.         return arr;
  173.     }
  174.     catch (IOException e1) {
  175.         // TODO Auto-generated catch block
  176.         e1.printStackTrace();
  177.     }
  178.         return null;
  179.  
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement