第一部分:Windows10配置FFmpeg
简介:FFmpeg是一个功能强大的多媒体处理工具(用于录制、转换和播放音频和视频)。可以进行转换、剪辑、拼接、过滤等操作。
1、下载FFmpeg工具(分Windows和Linux其他)
Download FFmpeg https://ffmpeg.org/download.html
https://ffmpeg.org/download.html 
2、下载full-share版本
full版本是完整构建,shared版本添加了头文件和库,用来学习和调试程序。我选择ffmpeg-release-full-shared.7z版本。
 
3、下载并解压ffmpeg-7.0.2
 
4、配置环境变量
我的电脑—>属性(右键)—>高级系统配置—>高级—>环境变量—>path—>编辑—>新增—>D:\Program Files (x86)\ffmpeg-7.0.2-full_build-shared\bin
 
5、查看ffmpeg版本
启动cmd,在终端执行:ffmpeg -version
 
第二部分:大华摄像机的RTMP视频流分割
(每隔 15秒切分视频并存储)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
/**
 * 视频流地址实现每隔 15秒切分视频并存储
 * 视频流格式:rtmp
 */
public class DaHuaRtmpVideoStreamSplitter {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // 切分视频流并存储的逻辑
                // 存视频开始~结束时间【文件名】
                try {
                    String inputUrl = "http://192.168.5.188:80/flv?port=1935&app=live&stream=test";
                    String outputFileName = "video_" + System.currentTimeMillis() + ".mp4";
//                    String command = "D:\\Program Files (x86)\\ffmpeg-7.0.2-full_build-shared\\bin\\ffmpeg.exe -i " + inputUrl + " -c copy -t 15 " + outputFileName;
                    String command = "D:\\Program Files (x86)\\ffmpeg-7.0.2-full_build-shared\\bin\\ffmpeg.exe -i " + inputUrl + " -c:v copy -c:a aac -t 15 " + outputFileName;
                    Process process = Runtime.getRuntime().exec(command);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    String line;
                    while ((line = reader.readLine())!= null) {
                        System.out.println(line);
                    }
                    int exitCode;
                    try {
                        exitCode = process.waitFor();
                    } catch (InterruptedException e) {
                        System.out.println("等待进程结束被中断!");
                        e.printStackTrace();
                        return;
                    }
                    if (exitCode!= 0) {
                        System.out.println("FFmpeg 执行出错!退出码: " + exitCode);
                    } else {
                        System.out.println("视频切分和存储成功: " + outputFileName);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 0, 15 * 1000);  // 15 秒 = 15 * 1000 毫秒
    }
}


















