Subtitle Trimmer (Atau Apalah Istilahnya)

filmAwal cerita

Saya ada film tapi gak ada subtitlenya. Jadi, saya cari aja via google subtitlenya. Setelah dapat beberapa subtitle saya senang, tapi ada yang mengganjal di hati ini (ciee). Subtitlenya gak pas dengan jalannya si film. Subtitlenya telat muncul. Orang di film udah ngomong, eh subtitlenya baru muncul 10 detik kemudian. Kan gak enak.

Yaudah

Awalnya mau pakai Ulead video editor buat motong file film nya, namun rasanya kurang efisien. Yasudah mending coba pake java aja buat ngedit file subtitlenya. Saya gak tau apakah udah pernah ada yang bikin atau belum. Mau cari di google juga bingung kata kuncinya apa. Jadilah saya mengistilahkannya subtitle trimmer. a very very simple java program dan masih belum rapih.

Untuk kebutuhan sendiri sih, sudah bisa jalan. lumayan. Silahkan kalau mau mencoba. Menjalankannya gampang, langsung compile sendiri saja programnya. Jangan lupa isi parameter di mehtod main, ada tiga parameter : filename(path), leadtime, mode.

hehe :p

the Code

/**
 * Program  : Subtitle Trimmer
 * Author 	: risyana
 * Email  	: risyana[at]gmail[dot]com
 * License	: free to use, free to redistribute, free to develop. Credit please
 * */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class SubtitleTrimmer {
	String currentSubtitleFile;
	String leadTimeStr;
	String mode;
	String dir;

	SubtitleTrimmer(String currentSubtitleFile, String leadTimeStr, String mode){
		this.currentSubtitleFile = currentSubtitleFile;
		this.leadTimeStr = leadTimeStr;
		this.mode = mode;
		this.dir = GetFileDirectory(currentSubtitleFile);
	}

	public String GetFileDirectory(String currentSubtitleFile){
		int lastIndex = currentSubtitleFile.lastIndexOf("/");
		dir = currentSubtitleFile.substring(0, lastIndex);
		return dir;
	}

	public StringBuffer readCurrentSubtitle() throws ParseException{
		Date leadTime;
		String text = null;
		BufferedReader reader = null;

		File file = new File(currentSubtitleFile);
		StringBuffer contents = new StringBuffer();

		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		leadTime = df.parse(leadTimeStr);

		try {
			reader = new BufferedReader (new FileReader(file));

			String start_time_str = null;
			String end_time_str = null;

			Date start_time ;
			Date end_time ;

			long start_time_new ;
			long end_time_new ;

			while ((text = reader.readLine()) != null){
				if (text.contains(" --> ")){
					start_time_str = text.substring(0, 8);
					end_time_str = text.substring(17, 25);

					start_time = df.parse(start_time_str);
					end_time = df.parse(end_time_str);

					int hours, minutes, seconds;

					// new start time
					if(mode == "minus"){
						start_time_new = (start_time.getTime() - leadTime.getTime());
					}else{
						start_time_new = (start_time.getTime() + leadTime.getTime());
					}

					int timeInSeconds = (int) (start_time_new / 1000);
					hours = timeInSeconds / 3600;
					timeInSeconds = timeInSeconds - (hours * 3600);
					minutes = timeInSeconds / 60;
					timeInSeconds = timeInSeconds - (minutes * 60);
					seconds = timeInSeconds;
					String start_time_new_str = Integer.toString(hours)+":"+Integer.toString(minutes)+":"+Integer.toString(seconds);

					// new end time
					if(mode == "minus"){
						end_time_new = end_time.getTime() - leadTime.getTime();
					}else{
						end_time_new = end_time.getTime() + leadTime.getTime();
					}

					timeInSeconds = (int) (end_time_new / 1000);
					hours = timeInSeconds / 3600;
					timeInSeconds = timeInSeconds - (hours * 3600);
					minutes = timeInSeconds / 60;
					timeInSeconds = timeInSeconds - (minutes * 60);
					seconds = timeInSeconds;
					String end_time_new_str = Integer.toString(hours)+":"+Integer.toString(minutes)+":"+Integer.toString(seconds);

					// replace old time with new start time
					text = text.replace(df.format(start_time), start_time_new_str);
					text = text.replace(df.format(end_time), end_time_new_str);
				}

				contents.append(text).append(System.getProperty("line.separator"));

			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try{
				if(reader != null){
					reader.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}

		return contents ;
	}

	public void writeNewSubtitle(StringBuffer contents){

		Writer writer = null;

		try{
			//String text = "this is a text file..";
			File file2 = new File(dir+"new_subtitle.srt");
			writer = new BufferedWriter(new FileWriter(file2));
			writer.write(contents.toString());
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try{
				if(writer != null){
					writer.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}

		System.out.println(contents.toString());
	}

	public static void main(String[]args) throws ParseException{
		// use this constructor to enter needed arguments :
		// arg#1: fill with path and filename current subtitle
		// arg#2: fill with lead time with format HH:mm:ss
		// arg#3: fill with 'minus' to backward subtitle, or fill with any text to forward subtitle

		SubtitleTrimmer st = new SubtitleTrimmer("D://coba SubtitleTrimmer//titanic.srt","00:00:11","minus");

		st.writeNewSubtitle(st.readCurrentSubtitle());
		System.out.println("success");
	}
}

Update Aug 21 2011
Memperbaiki beberapa line untuk kasus jika subtitlenya terlalu cepat.

/**
 * Program  : Subtitle Trimmer
 * Author 	: risyana
 * Email  	: risyana[at]gmail[dot]com
 * License	: free to use, free to redistribute, free to develop. Credit please😀
 * */

package fileAndString;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class SubtitleTrimmer {
	String currentSubtitleFile;
	String leadTimeStr;
	String mode;
	String dir;

	SubtitleTrimmer(String currentSubtitleFile, String leadTimeStr, String mode){
		this.currentSubtitleFile = currentSubtitleFile;
		this.leadTimeStr = leadTimeStr;
		this.mode = mode;
		this.dir = GetFileDirectory(currentSubtitleFile);
	}

	public String GetFileDirectory(String currentSubtitleFile){
		int lastIndex = currentSubtitleFile.lastIndexOf("/");
		dir = currentSubtitleFile.substring(0, lastIndex);
		return dir;
	}

	public StringBuffer readCurrentSubtitle() throws ParseException{
		Date leadTime;
		String text = null;
		BufferedReader reader = null;

		File file = new File(currentSubtitleFile);
		StringBuffer contents = new StringBuffer();

		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		leadTime = df.parse(leadTimeStr);

		int timeInSeconds = 0;

		try {
			reader = new BufferedReader (new FileReader(file));

			String start_time_str = null;
			String end_time_str = null;

			Date start_time ;
			Date end_time ;
			Date default_time;	// 21 aug 2011

			long start_time_new ;
			long end_time_new ;

			while ((text = reader.readLine()) != null){
				if (text.contains(" --> ")){
					start_time_str = text.substring(0, 8);
					end_time_str = text.substring(17, 25);

					start_time = df.parse(start_time_str);
					end_time = df.parse(end_time_str);
					default_time = df.parse("21:00:00");	// 21 aug 2011

					int hours = 0, minutes = 0, seconds;

					// new start time
					if(mode == "minus"){
						start_time_new = (start_time.getTime() - leadTime.getTime());
					}else{
						start_time_new = start_time.getTime() + (default_time.getTime()+leadTime.getTime()) ;	// 21 aug 2011
					}

					timeInSeconds = (int) (start_time_new / 1000);
					hours = timeInSeconds / 3600;
					timeInSeconds = timeInSeconds - (hours * 3600);
					minutes = timeInSeconds / 60;
					timeInSeconds = timeInSeconds - (minutes * 60);
					seconds = timeInSeconds;

					System.out.println("   | "+start_time.getTime()+"&"+leadTime.getTime()+" = "+start_time_new);

					String start_time_new_str = Integer.toString(hours)+":"+Integer.toString(minutes)+":"+Integer.toString(seconds);

					// new end time
					if(mode == "minus"){
						end_time_new = end_time.getTime() - leadTime.getTime();
					}else{
						end_time_new = end_time.getTime() + (default_time.getTime()+leadTime.getTime()) ;	// 21 aug 2011
					}

					timeInSeconds = (int) (end_time_new / 1000);
					hours = timeInSeconds / 3600;
					timeInSeconds = timeInSeconds - (hours * 3600);
					minutes = timeInSeconds / 60;
					timeInSeconds = timeInSeconds - (minutes * 60);
					seconds = timeInSeconds;
					String end_time_new_str = Integer.toString(hours)+":"+Integer.toString(minutes)+":"+Integer.toString(seconds);

					// replace old time with new start time
					text = text.replace(df.format(start_time), start_time_new_str);
					text = text.replace(df.format(end_time), end_time_new_str);
				}

				contents.append(text).append(System.getProperty("line.separator"));

			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try{
				if(reader != null){
					reader.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}

		return contents ;
	}

	public void writeNewSubtitle(StringBuffer contents){

		Writer writer = null;

		try{
			//String text = "this is a text file..";
			File file2 = new File(dir+"new_subtitle.srt");
			writer = new BufferedWriter(new FileWriter(file2));
			writer.write(contents.toString());
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try{
				if(writer != null){
					writer.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}

		System.out.println(contents.toString());
	}

	public static void main(String[]args) throws ParseException{
		// use this constructor to enter needed arguments :
		// arg#1: fill with path and filename current subtitle
		// arg#2: fill with lead time with format HH:mm:ss
		// arg#3: fill with 'minus' to backward subtitle, or fill with any text to forward subtitle

		SubtitleTrimmer st = new SubtitleTrimmer("D://ceceu//The Chronicles of Narnia-The Voyage of the Dawn Treader.srt","00:00:04","xx");

		st.writeNewSubtitle(st.readCurrentSubtitle());
		System.out.println("success");
	}
}


6 thoughts on “Subtitle Trimmer (Atau Apalah Istilahnya)

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s