gpt4 book ai didi

java - 将 .srt 文件分割成相等的 block

转载 作者:太空宇宙 更新时间:2023-11-04 10:21:11 27 4
gpt4 key购买 nike

我是新手,我需要将 Srt(字幕文件)分成多个 block 。

例如:如果我有一个视频的字幕文件(60 分钟)。然后将字幕文件分割成6个字幕文件,每个字幕文件10分钟。

即 6 X 10 = 60 分钟

无论分钟如何,都需要分为 6 个 block 。

使用每个字幕时间/持续时间,我必须将视频分成相同的 block 。

我正在尝试这段代码,你能帮我看看如何计算时间并分成 block 吗,

我能够实现我需要多少分钟的chuck。但是陷入如何从源文件中读取该chunk分钟并创建一个新文件。然后如何从源文件的接下来10分钟开始下一个 block 。

import org.apache.commons.io.IOUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

/**
* The class SyncSRTSubtitles reads a subtitles .SRT file and offsets all the
* timestamps with the same specific value in msec.
*
* The format of the .SRT file is like this:
*
* 123
* 00:11:23,456 --> 00:11:25,234
* subtitle #123 text here
*
*
* @author Sorinel CRISTESCU
*/
public class SyncSRTSubtitles {

/**
* Entry point in the program.
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

/* INPUT: offset value: negative = less (-) ... positive = more (+). */
long delta = (22 * 1000L + 000); /* msec */


/* INPUT: source & destination files */
String srcFileNm = "/Users/meh/Desktop/avatar.srt";
String destFileNm = "/Users/meh/Desktop/avatar1.srt";


/* offset algorithm: START */
File outFile = new File(destFileNm);
outFile.createNewFile();
FileWriter ofstream = new FileWriter(outFile);
BufferedWriter out = new BufferedWriter(ofstream);

/* Open the file that is the first command line parameter */
FileInputStream fstream = new FileInputStream(srcFileNm);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// List<String> doc = IOUtils.readLines(in, StandardCharsets.UTF_8);


String strEnd = null;
long diff = 0;
String line;
String startTS1;
try (Stream<String> lines = Files.lines(Paths.get(srcFileNm))) {
line = lines.skip(1).findFirst().get();
String[] atoms = line.split(" --> ");
startTS1 = atoms[0];

}
System.out.println("bolo:" +line);
System.out.println("startTS1:" +startTS1);

String startTS = null;
String endTS = null;
/* Read File Line By Line */
while ((strLine = br.readLine()) != null) {
String[] atoms = strLine.split(" --> ");
if (atoms.length == 1) {
//out.write(strLine + "\n");
}
else {
startTS = atoms[0];


endTS = atoms[1];
// out.write(offsetTime(startTS, delta) + " --> "
// + offsetTime(endTS, delta) + "\n");
strEnd = endTS;

}


}
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
Date parsedendDate = dateFormat.parse(strEnd);
Date parsedStartDate = dateFormat.parse(startTS1);
diff = parsedendDate.getTime() - parsedStartDate.getTime();

} catch(Exception e) { //this generic but you can control another types of exception
// look the origin of excption
}
System.out.println("strEnd");
System.out.println(strEnd);
/* Close the input streams */
in.close();
out.close();
System.out.println(diff);
long diff1 =diff/6;
System.out.println(diff1);
long diff2= (diff1*6);
System.out.println(diff2);
System.out.println((diff / 3600000) + " hour/s " + (diff % 3600000) / 60000 + " minutes");
System.out.println((diff1 / 3600000) + " hour/s " + (diff1 % 3600000) / 60000 + " minutes");
System.out.println((diff2 / 3600000) + " hour/s " + (diff2 % 3600000) / 60000 + " minutes");

/* offset algorithm: END */
System.out.println("DONE! Check the rsult oin the file: " + destFileNm);
}

/**
* Computes the timestamp offset.
*
* @param ts
* String value of the timestamp in format: "hh:MM:ss,mmm"
* @param delta
* long value of the offset in msec (positive or negative).
* @return String with the new timestamp representation.
*/
private static String offsetTime(String ts, long delta) {
long tsMsec = 0;
String atoms[] = ts.split("\\,");
if (atoms.length == 2) {
tsMsec += Integer.parseInt(atoms[1]);
}
atoms = atoms[0].split(":");

tsMsec += Integer.parseInt(atoms[2]) * 1000L; /* seconds */
tsMsec += Integer.parseInt(atoms[1]) * 60000L; /* minutes */
tsMsec += Integer.parseInt(atoms[0]) * 3600000L; /* hours */
tsMsec += delta; /* here we do the offset. */

long h = tsMsec / 3600000L;
System.out.println(h);

String result = get2digit(h, 2) + ":";

System.out.println(result);


long r = tsMsec % 3600000L;
System.out.println(r);

long m = r / 60000L;
System.out.println(m);

result += get2digit(m, 2) + ":";
System.out.println(result);
r = r % 60000L;
System.out.println(r);

long s = r / 1000L;
result += get2digit(s, 2) + ",";

result += get2digit(r % 1000L, 3);
System.out.println(result);

return result;
}

/**
* Gets the string representation of the number, adding the prefix '0' to
* have the required length.
*
* @param n
* long number to convert to string.
* @param digits
* int number of digits required.
* @return String with the required length string (3 for digits = 3 -->
* "003")
*/
private static String get2digit(long n, int digits) {
String result = "" + n;
while (result.length() < digits) {
result = "0" + result;
}
return result;
}

}

请建议我如何实现这一目标?

最佳答案

您需要解析该文件两次:

  • 一次读取最后结束时间
  • 第二次处理所有行并生成输出文件。

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class SplitSRTFiles {

/**
* Splits a SRT file in multiple files each containing an equal time duration.
* @param args
* [0] number of wanted chunks
* [1] source file name
* @throws IOException
*/
public static void main(String[] args) throws IOException {
int nrOfChunks = Integer.parseInt(args[0]);
File srtFile = new File(args[1]);
System.out.println("Splitting "+srtFile.getAbsolutePath()+" into "+nrOfChunks+" files.");

List<String> srcLines = FileUtils.readLines(srtFile);
long fileEndTime = lastEndTime(srcLines);
long msecsPerChunkFile = fileEndTime / nrOfChunks;
int destFileCounter = 1;
String[] fileNameParts = srtFile.getName().split("\\.");
File outFile = new File(fileNameParts[0] + destFileCounter + "." + fileNameParts[1]);
System.out.println("Writing to "+outFile.getAbsolutePath());
outFile.createNewFile();
FileWriter ofstream = new FileWriter(outFile);
BufferedWriter out = new BufferedWriter(ofstream);

for (String line : srcLines) {
String[] atoms = line.split(" --> ");
if (atoms.length > 1) {
long startTS = toMSec(atoms[0]);
// check if start time of this subtitle is after the current
// chunk
if (startTS > msecsPerChunkFile * destFileCounter) {
// close existing file ...
out.close();
ofstream.close();
// ... and start a new file
destFileCounter++;
outFile = new File(srtFile.getParent(), fileNameParts[0] + destFileCounter + "." + fileNameParts[1]);
System.out.println("Writing to "+outFile.getAbsolutePath());
outFile.createNewFile();
ofstream = new FileWriter(outFile);
out = new BufferedWriter(ofstream);
}
}
out.write(line + "/n");
}
out.close();
ofstream.close();
System.out.println("Done.");
}

/**
* Calculates the time in msec of the end time of the last subtitle of the
* file
*
* @param lines
* read from file
* @return end time in milliseconds of the last subtitle
*/
public static long lastEndTime(List lines) throws IOException {
String endTS = null;
for (String line : lines) {
String[] atoms = line.split(" --> ");
if (atoms.length > 1) {
endTS = atoms[1];
}
}
return endTS == null ? 0L : toMSec(endTS);
}

public static long toMSec(String time) {
long tsMsec = 0;
String atoms[] = time.split("\\,");
if (atoms.length == 2) {
tsMsec += Integer.parseInt(atoms[1]);
}
atoms = atoms[0].split(":");

tsMsec += Integer.parseInt(atoms[2]) * 1000L; /* seconds */
tsMsec += Integer.parseInt(atoms[1]) * 60000L; /* minutes */
tsMsec += Integer.parseInt(atoms[0]) * 3600000L; /* hours */
return tsMsec;
}
}

关于java - 将 .srt 文件分割成相等的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51154896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com