แยกไฟล์ .srt ออกเป็นส่วนๆ เท่าๆ กัน

ฉันยังใหม่กับสิ่งนี้และฉันต้องแยก Srt (ไฟล์คำบรรยาย) ออกเป็นหลาย ๆ ส่วน

ตัวอย่างเช่น: หากฉันมีไฟล์คำบรรยายของวิดีโอ (60 นาที) จากนั้นไฟล์คำบรรยายควรแบ่งออกเป็นไฟล์คำบรรยาย 6 ไฟล์ โดยแต่ละไฟล์มีความยาว 10 นาที

เช่น 6 X 10 = 60 นาที

ต้องแบ่งออกเป็น 6 ชิ้นโดยไม่คำนึงถึงนาที

เมื่อใช้แต่ละช่วงเวลา/คำบรรยายเหล่านี้ ฉันต้องแบ่งวิดีโอออกเป็นส่วนๆ เดียวกัน

ฉันกำลังลองใช้รหัสนี้ คุณช่วยฉันหน่อยได้ไหม ฉันจะคำนวณเวลาและแบ่งออกเป็นชิ้น ๆ ได้อย่างไร

ฉันสามารถบรรลุจำนวนนาทีที่ฉันต้องการได้ แต่ติดอยู่กับวิธีการอ่านไม่เกินนาทีนั้นจากไฟล์ต้นฉบับและสร้างไฟล์ใหม่ จากนั้นจะเริ่มส่วนถัดไปจากไฟล์ต้นฉบับในอีก 10 นาทีถัดไปได้อย่างไร

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;
    }

}

โปรดแนะนำฉันว่าฉันจะบรรลุเป้าหมายนี้ได้อย่างไร


person user6250770    schedule 03.07.2018    source แหล่งที่มา
comment
คุณต้องการมันเป็นชิ้นละ 10 นาทีเสมอหรือว่าชิ้นควรจะเป็นอย่างไร?   -  person XtremeBaumer    schedule 03.07.2018
comment
ฉันต้องแบ่งเป็นชิ้นเท่าๆ กัน... ถ้าเป็น 10 นาที แล้วถ้าฉันต้องการ 5 ชิ้น ก็ควรแบ่งเป็นชิ้นละ 2 นาที ถ้าฉันมีไฟล์ขนาด 120 นาที ถ้าฉันต้องการ 6 ชิ้น แต่ละไฟล์ 20 นาที... เอาต์พุตประเภทนี้ฉันต้องการ @XtremeBaumer   -  person user6250770    schedule 03.07.2018
comment
คุณต้องมีวิธีที่สอดคล้องกันในการทราบว่าคุณต้องการชิ้นจำนวนเท่าใด คุณต้องการกี่ชิ้นสำหรับ 65/79/88 นาที   -  person XtremeBaumer    schedule 03.07.2018
comment
มันอาจแตกต่างกันไปในแต่ละภาพยนตร์ ตกลงจะแก้ไขเป็น 6 ชิ้นโดยไม่คำนึงถึงนาที และทั้ง 6 ไฟล์ควรเขียนเป็นไฟล์ .srt จำนวน 6 ไฟล์   -  person user6250770    schedule 03.07.2018
comment
@XtremeBaumer ต้องการ 6 ชิ้นโดยไม่คำนึงถึงนาที และควรเขียนเป็นไฟล์ .srt ที่แตกต่างกัน 6 ไฟล์ ฉันจะบรรลุเป้าหมายนี้ได้อย่างไร? โปรดแนะนำ   -  person user6250770    schedule 03.07.2018
comment
หากคุณไม่ทราบความยาวของวิดีโอมาก่อน คุณจะต้องรับการประทับเวลาล่าสุดจากไฟล์ เมื่อคุณทราบความยาวแล้ว คุณจะคำนวณขนาดของชิ้นส่วน ด้วยขนาดนี้ คุณสามารถอ่านไฟล์และตรวจสอบว่าคุณจำเป็นต้องเริ่มไฟล์ใหม่เมื่อใด   -  person XtremeBaumer    schedule 03.07.2018
comment
ให้เราสนทนาต่อในการแชท   -  person user6250770    schedule 03.07.2018
comment
ทำไมต้องคิดค้นล้อใหม่? ใช้ไลบรารีที่มีอยู่   -  person rustyx    schedule 03.07.2018
comment
@rustyx ในโค้ดที่ให้มา ทุกอย่างเรียบร้อยดี แค่ฉันต้องคำนวณเวลาและแบ่งออกเป็นชิ้นๆ   -  person user6250770    schedule 03.07.2018
comment
@XtremeBaumer อัปเดตรหัสของฉันที่นี่ ขออภัยสำหรับการโพสต์ซ้ำ   -  person user6250770    schedule 04.07.2018


คำตอบ (2)


ฉันพบวิธีแบ่งไฟล์ออกเป็นชิ้น ๆ แล้ว

    public static void main(String args[]) throws IOException {
String FilePath = "/Users/meh/Desktop/escapeplan.srt";
        FileInputStream fin = new FileInputStream(FilePath);

        System.out.println("size: " +fin.getChannel().size());
        long abc = 0l;
        abc = (fin.getChannel().size())/3;
        System.out.println("6: " +abc);
System.out.println("abc: " +abc);
       //FilePath = args[1];
        File filename = new File(FilePath);
        long splitFileSize = 0,bytefileSize=0;
        if (filename.exists()) {
            try {
                //bytefileSize = Long.parseLong(args[2]);
                splitFileSize = abc;
                Splitme spObj = new Splitme();
                spObj.split(FilePath, (long) splitFileSize);
                spObj = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("File Not Found....");
        }
}
    public void split(String FilePath, long splitlen) {

            long leninfile = 0, leng = 0;
            int count = 1, data;
            try {
                File filename = new File(FilePath);
                InputStream infile = new BufferedInputStream(new FileInputStream(filename));
                data = infile.read();
                System.out.println("data");
                System.out.println(data);

                while (data != -1) {
                    filename = new File("/Users/meh/Documents/srt" + count + ".srt");
    //RandomAccessFile outfile = new RandomAccessFile(filename, "rw");

                    OutputStream outfile = new BufferedOutputStream(new FileOutputStream(filename));
                    while (data != -1 && leng < splitlen) {
                        outfile.write(data);
                        leng++;
                        data = infile.read();
                    }
                    leninfile += leng;
                    leng = 0;
                    outfile.close();
                    changeTimeStamp(filename, count);

                    count++;


                }
            } catch (Exception e) {
                e.printStackTrace();
            }
}
person user6250770    schedule 05.07.2018

คุณจะต้องแยกวิเคราะห์ไฟล์สองครั้ง:

  • หนึ่งครั้งเพื่ออ่านเวลาสิ้นสุดครั้งล่าสุด
  • ครั้งที่สองเพื่อประมวลผลทุกบรรทัดและสร้างไฟล์เอาต์พุต

    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;
        }
    }
person Conffusion    schedule 03.07.2018
comment
ขอบคุณ. แต่ฉันได้รับข้อผิดพลาดใน for(String line: ) ประเภทที่เข้ากันไม่ได้ วัตถุที่คาดหวังแต่พบสตริง - person user6250770; 04.07.2018