Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.util.ArrayList;
- public class java_video_audio_werbecutter {
- /**
- * @param args
- */
- public static void main(String[] args) {
- if (args.length <3){
- System.out.println("Zu wenig argumente");
- System.out.println("Benötige Inputfilename, encode file und Outputfilename");
- }
- else{
- String[] candidates = write_ffmpeg_info(args[0]);
- Double[] positions = get_positions(candidates,args[0]);
- write_encode_file(positions,args[1],args[0],args[2]);
- }
- }
- private static void write_encode_file(Double[] positions, String outputfilename, String inputfilename, String outputvideo) {
- try {
- BufferedWriter out = new BufferedWriter (new FileWriter(outputfilename));
- out.write("#"+inputfilename+"\n");
- for (int i=0;i<positions.length;i++){
- out.write("#"+positions[i]+"\n");
- }
- int endpos=0;
- if ( positions.length / 2 == 0){
- endpos=positions.length;
- }
- else{
- endpos=positions.length-1;
- }
- for (int i=0;i<endpos;i=i+2){
- //double startpos = get_realstartpos(positions[i-0.5]);
- 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 ";
- out_string=out_string+"/tmp/"+i+".mkv\n";
- out.write(out_string);
- }
- out.write("mkvmerge -o /tmp/dummy.mkv /tmp/0.mkv");
- for (int i=2;i<endpos;i=i+2){
- out.write(" + /tmp/"+i+".mkv");
- }
- out.write("\n");
- out.write("ffmpeg -i /tmp/dummy.mkv -vcodec copy -acodec libfaac -ab 160k \""+outputvideo+"\"\n");
- out.flush();
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private static Double[] get_positions(String[] candidates, String videofilename) {
- ArrayList<Double> output = new ArrayList<Double>();
- for(int i=0;i<candidates.length;i++){
- String[] data = candidates[i].split(":");
- double a = Double.parseDouble(data[0])*3600;
- double b = Double.parseDouble(data[1])*60;
- double c = Double.parseDouble(data[2]);
- double summe=a+b+c;
- double outi = check_candidate(summe,0,videofilename);
- output.add(outi);
- }
- Double[] arr = output.toArray(new Double[output.size()]);
- return arr;
- }
- private static double check_candidate(double start, double i, String videofilename) {
- System.out.println("Check start: "+ Double.toString(start)+" offset "+ Double.toString(i));
- double start1= start -10;
- double start2= 10 + i ;
- String start1_string = Double.toString(start1);
- String start2_string = Double.toString(start2);
- String[] befehl = new String[] {"ffmpeg","-ss",start1_string,"-i",videofilename,"-t",start2_string,"-f","wav","/dev/null"};
- try {
- ProcessBuilder builder = new ProcessBuilder(befehl);
- builder.redirectErrorStream(true);
- Process process = builder.start();
- OutputStream os = process.getOutputStream();
- OutputStreamWriter osr = new OutputStreamWriter(os);
- BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
- boolean found=false;
- osr.write("y\n");
- osr.flush();
- String line;
- while ((line = br.readLine()) != null) {
- if (line.contains("frame changed")){
- found=true;
- }
- }
- if (found){
- return 0;
- }
- else{
- double a = check_candidate(start,(i+(double)0.1),videofilename);
- if (0== a){
- System.out.println("Found!!!: "+Double.toString(start+i));
- return start+i;
- }
- else{
- return a;
- }
- }
- }
- catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- return 0;
- }
- private static String[] write_ffmpeg_info(String videofilename) {
- System.out.println("Video Input file: " + videofilename);
- try {
- String[] befehl = new String[] {"ffmpeg","-i",videofilename,"-f","wav","/dev/null"};
- ProcessBuilder builder = new ProcessBuilder(befehl);
- builder.redirectErrorStream(true);
- Process process = builder.start();
- OutputStream os = process.getOutputStream();
- OutputStreamWriter osr = new OutputStreamWriter(os);
- BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
- try {
- Thread.sleep(500);
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- osr.write("y\n");
- osr.flush();
- String line;
- ArrayList<String> output = new ArrayList<String>();
- String xy = null;
- while ((line = br.readLine()) != null) {
- if (line.startsWith("size=")){
- xy=(line.substring(line.indexOf("time=")+5,line.indexOf("time=")+13));
- }
- if (line.contains("frame changed")){
- System.out.println("Candidate found: "+xy);
- output.add(xy);
- }
- }
- String[] arr = output.toArray(new String[output.size()]);
- return arr;
- }
- catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement