ffmpeg通过java代码获取流地址中的视频保存mp4格式至本地,视频为啥不能播放?

完整的测试代码如下:(保存至本地但不能播放)

import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Test1 implements Runnable {

    InputStream in ; // 正常输出流
    boolean stop ; // 结束进程
    int maxFrameCount = 7500;
    Process pro ;

    public Test1(Process pro,InputStream in ) {
        this.pro = pro;
        this.in=in;
    }

    public boolean isStop() {
        return stop;
    }

    public void setStop(boolean stop) {
        this.stop = stop;
    }

    public int getMaxFrameCount() {
        return maxFrameCount;
    }

    public void setMaxFrameCount(int maxFrameCount) {
        this.maxFrameCount = maxFrameCount;
    }


   


    public static void main(String[] args)throws Exception{
        System.out.println("###使用进程的方式进行编码###");
        // 1. 视频转换视频
        String rtspUrl = "rtsp://wangzhi:1qaz2wsx@192.168.104.251/h264/ch1/main/av_stream";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
        Date now = new Date();
        Date afterDate = new Date(now .getTime() + 300000);
        String dest = "D:/"+sdf.format(now)+"—"+sdf.format(afterDate)+".mp4";
        System.out.println("+++++++++++++++++++"+dest);
        convert(rtspUrl, dest);

        System.out.println("###end###");

    }
    public static void convert(String src,String dest)throws Exception{
        String ffcmdpath = "D:\\download\\ffmpeg-4.2.2-win64-static\\bin\\ffmpeg.exe";
        StringBuilder cmd = new StringBuilder();
        cmd.append(ffcmdpath)
                .append(" -rtsp_transport tcp ") // 使用tcp的命令,默认是udp
                .append(" -i ").append(src)
                //	.append(" -vcodec copy ")
                .append(" -vcodec h264 ")
                //	.append(" -acodec copy ") // 音频,不设置好像也有。
                //	.append(" -s 1280*720 ")   // 设置分辨率,关系到视频的大小或者为 -vf scale=iw/2:ih/2
                //	.append(" -vf scale=iw/2:ih/2 ")
                .append(" -y ") // 覆盖
                .append(dest);
        System.out.println("cmd="+cmd.toString());
        System.out.println("###start cmd="+new Date().toLocaleString());
        Process process = Runtime.getRuntime().exec(cmd.toString());
        // 输出内容
        Test1 twffIn = new Test1(process, process.getInputStream());
        Test1 twffInErr = new Test1(process,process.getErrorStream());
        Thread t = new Thread(twffIn);
        Thread t1 = new Thread(twffInErr);
        t.start();t1.start();
        // 停止指令,10秒后停止 ,一定要发送这个,要不然视频保存不下来
        //
        int i = process.waitFor(); // 一定要配合2个 inputstream ,要不然会一直阻塞
        System.out.println(i+"###end cmd="+new Date().toLocaleString());
        twffIn.setStop(true);twffInErr.setStop(true); // 停止 线程

    }
    /**
     * @title stopConvert
     * @date 2018年10月11日
     * @param process
     * @return
     * @description 发送停止命令,
     * 但是要注意发送完成后,不一定会马上停止 。因为进程结束也是需要时间的
     */
    public static boolean stopConvert(Process process ){
        System.out.println("###send q cmd ");
        try{
            OutputStream os = process.getOutputStream();
            os.write("q".getBytes());
            os.flush(); // 一定要刷新
        }catch(Exception err){
            err.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public void run() {
        Scanner scanner = new Scanner(in);
        while(!stop){
            if(scanner.hasNext()){
                String s = scanner.nextLine();
                System.out.println(s);
                // 判断停止录像的条件
                if(s.startsWith("frame=") && s.indexOf("fps=") > 6){
                    String frameCountStr = s.substring(6
                            ,s.indexOf("fps="));
//					System.out.println(s.indexOf("fps=") + ",frameCountStr="+frameCountStr);
                    // 获得当前解析到的帧数,退出
                    int frameCount = Integer.parseInt(frameCountStr.trim());
                    System.out.println("======================"+frameCount);
                    if(frameCount >= maxFrameCount){
                        System.out.println("##maxFrameCount="+maxFrameCount +",frameCount="+frameCount);
                        stopConvert(pro);
                    }
                }
            }else{
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        scanner.close();
        System.out.println("###读取线程结束啦###");

    }
}

这个转换格式问题应该怎么解决,求大神解疑.

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值