Mp4文件使用Fmpeg分片為Ts文件

package com.example.mediodownload.utils;


import java.io.InputStream;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

/**

*/

public class Ma4FmpegByTs {


public static void main(String[] args) {


String sourceFile = "D:\\chicz_m3u8\\03.mp4";

String destFile = "D:\\chicz_m3u8\\03.ts";

String m3u8File = "D:\\chicz_m3u8\\index.m3u8";

String tsFile = "D:\\chicz_m3u8\\index_%04d.ts";// 4個(gè)占位符

Integer volume = 10;// 長(zhǎng)度

String osName = System.getProperty("os.name");

URL resource;

// 根據(jù)系統(tǒng)名稱獲取ffmpeg,classpath下的路徑

if (osName.toLowerCase().contains("windows")) {

resource = Ma4FmpegByTs.class.getResource("/ffmpeg/bin/ffmpeg.exe");

} else {

resource = Ma4FmpegByTs.class.getResource("/ffmpeg/bin/ffmpeg");

}

// 獲取執(zhí)行命令,音量和比特率可以不傳

List<String> toTs = getFfmpegCommand(resource.getPath(), sourceFile, destFile);

// 獲取執(zhí)行命令,音量和比特率可以不傳

List<String> toMoreTs = getFfmpegCommand(resource.getPath(), destFile, m3u8File, volume, tsFile);


List<String> mp4ToMoreTs = getFfmpegCommand(resource.getPath(), sourceFile, destFile, m3u8File, volume, tsFile);


// 執(zhí)行轉(zhuǎn)換

try {

boolean process = process(mp4ToMoreTs);

System.out.println(process);

// process = process(toMoreTs);

// System.out.println(process);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* mp4文件切片為多個(gè)ts文件

*

* @param ffmpegPath

* @param sourceFile

* @param destFile

* @param m3u8File

* @param volume

* @param tsFile

* @return

*/

private static List<String> getFfmpegCommand(String ffmpegPath, String sourceFile, String destFile, String m3u8File, Integer volume, String tsFile) {

List<String> command = new ArrayList<String>();

command.add(ffmpegPath);

command.add("-i");

command.add(sourceFile);

command.add("-f");

command.add("segment");

command.add("-segment_time");

command.add(volume + "");

command.add("-segment_format");

command.add("mpegts");

command.add("-segment_list");

command.add(m3u8File);

command.add("-c");

command.add("copy");

command.add("-bsf:v");

command.add("h264_mp4toannexb");

command.add("-map");

command.add("0");

command.add(tsFile);

return command;

}


/**

* mp4文件轉(zhuǎn)ts文件

*

* @param ffmpegPath

* @param sourceFile

* @param destFile

* @return

*/

private static List<String> getFfmpegCommand(String ffmpegPath, String sourceFile, String destFile) {

List<String> command = new ArrayList<String>();

command.add(ffmpegPath);

command.add("-i");

command.add(sourceFile);

command.add("-vcodec");

command.add("copy");

command.add("-acodec");

command.add("copy");

command.add("-vbsf");

command.add("h264_mp4toannexb");

command.add(destFile);

return command;

}


/**

* ts文件切片為多個(gè)ts文件

*

* @param ffmpegPath

* @param destFile

* @param m3u8File

* @param volume

* @param tsFile

* @return

*/

private static List<String> getFfmpegCommand(String ffmpegPath, String destFile, String m3u8File, Integer volume, String tsFile) {

List<String> command = new ArrayList<String>();

command.add(ffmpegPath);

command.add("-i");

command.add(destFile);

command.add("-c");

command.add("copy");

command.add("-map");

command.add("0");

command.add("-f");

command.add("segment");

command.add("-segment_list");

command.add(m3u8File);

command.add("-segment_time");

command.add(volume + "");

command.add(tsFile);

return command;

}

/**

*

* @param ffmpegCommand

* @return

*/

private static boolean process(List<String> ffmpegCommand) throws Exception {

try {

if (null == ffmpegCommand || ffmpegCommand.size() == 0) {

return false;

}

Process videoProcess = new ProcessBuilder(ffmpegCommand).redirectErrorStream(true).start();


new PrintStream(videoProcess.getErrorStream()).start();


new PrintStream(videoProcess.getInputStream()).start();


int exitCode = videoProcess.waitFor();


if (exitCode == 1) {

return false;

}

return true;

} catch (Exception e) {

throw new Exception("file uploadfailed", e);

}

}

/**

* 文件讀取

*/

private static class PrintStream extends Thread {

InputStream inputStream = null;


PrintStream(InputStream is) {

inputStream = is;

}


@Override

public void run() {

try {

while (this != null) {

int ch = inputStream.read();

if (ch != -1) {

System.out.print((char) ch);

} else {

break;

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容