javacv 服务器发TS流视频,客户端拉流实时播发视频

文章描述了一个使用Java实现的服务器向客户端发送TS格式视频流的系统。服务器端读取多个TS文件并发送到客户端,客户端通过JavaCV库的FFmpegFrameGrabber解析并显示视频。系统已经可以连续播放多段TS视频,但存在几秒的停顿问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 服务器可以发送图片,TS格式视频,客户端实时接收并显示.  注: 视频只能是TS格式的,mp4必须用ffmpeg转换 为TS格式视频,已可以连续播放ts的小段分视频。

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>wz1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>wz1</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.5.8</version>
        </dependency>

        <!-- Additional dependencies required to use CUDA and cuDNN -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>opencv-platform-gpu</artifactId>
            <version>4.6.0-1.5.8</version>
        </dependency>

        <!-- Optional GPL builds with (almost) everything enabled -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>ffmpeg-platform-gpl</artifactId>
            <version>5.1.2-1.5.8</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.服务器:下面只能发一个视频文件

package org.example;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Fs {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream("/home/wzpad/1/ts/a.1.ts");
        byte[] b=new byte[90000000];
        fileInputStream.read(b);
        ServerSocket serverSocket=new ServerSocket(3000);
        Socket socket=serverSocket.accept();

        OutputStream outputStream=socket.getOutputStream();
        outputStream.write(b);

       outputStream.close();
       System.out.println("over");

    }
}

3.客户端

package org.example;
import javax.swing.JFrame;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.nio.file.Files;

public class App{
    public static void main(String[] args) throws IOException, InterruptedException {

   //    OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);//新建opencv抓取器,一般的电脑和移动端设备中摄像头默认序号是0,不排除其他情况
         Socket socket=new Socket("192.168.43.61",3000);
        InputStream is=socket.getInputStream();

        FFmpegFrameGrabber grabber=new FFmpegFrameGrabber(is);
        grabber.start();//开始获取摄像头数据

        CanvasFrame canvas = new CanvasFrame("摄像头预览");//新建一个预览窗口
        canvas.setCanvasSize(600,600);
        canvas.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //窗口是否关闭
        while(canvas.isDisplayable()){
            /*获取摄像头图像并在窗口中显示,这里Frame frame=grabber.grab()得到是解码后的视频图像*/
            canvas.showImage(grabber.grab());
        }
        grabber.close();//停止抓取
    }
}

4,修改后的服务器可以连续播发视频了,发现两段视频之间有几秒的停顿,程序简陋,主要是说明原理。

package org.example;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Fs {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream("/home/wzpad/1/ts/a.0.ts");
        byte[] b=new byte[90000000];
        fileInputStream.read(b);
        FileInputStream fileInputStream1=new FileInputStream("/home/wzpad/1/ts/a.1.ts");
        byte[] b1=new byte[90000000];
        fileInputStream1.read(b1);
        FileInputStream fileInputStream2=new FileInputStream("/home/wzpad/1/ts/a.2.ts");
        byte[] b2=new byte[90000000];
        fileInputStream2.read(b2);
        FileInputStream fileInputStream3=new FileInputStream("/home/wzpad/1/ts/a.3.ts");
        byte[] b3=new byte[90000000];
        fileInputStream3.read(b3);

        ServerSocket serverSocket=new ServerSocket(3000);
        Socket socket=serverSocket.accept();

        OutputStream outputStream=socket.getOutputStream();
        outputStream.write(b);
        outputStream.write(b1);
        outputStream.write(b2);
        outputStream.write(b3);

        outputStream.close();
       System.out.println("over");

    }
}
package org.example;

import org.bytedeco.javacv.CanvasFrame;

import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Fs {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        FileInputStream fileInputStream=new FileInputStream("/home/wzpad/1/ts/a.0.ts");
        byte[] b=new byte[90000000];
        fileInputStream.read(b);
        FileInputStream fileInputStream1=new FileInputStream("/home/wzpad/1/ts/a.1.ts");
        byte[] b1=new byte[90000000];
        fileInputStream1.read(b1);
        FileInputStream fileInputStream2=new FileInputStream("/home/wzpad/1/ts/a.2.ts");
        byte[] b2=new byte[90000000];
        fileInputStream2.read(b2);
        FileInputStream fileInputStream3=new FileInputStream("/home/wzpad/1/ts/a.3.ts");
        byte[] b3=new byte[90000000];
        fileInputStream3.read(b3);

        ServerSocket serverSocket=new ServerSocket(3000);
        Socket socket=serverSocket.accept();

        OutputStream outputStream=socket.getOutputStream();
    /*    outputStream.write(b);
        outputStream.write(b1);
        outputStream.write(b2);
        outputStream.write(b3);*/
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(b);
        objectOutputStream.flush();

        outputStream.close();
       System.out.println("over");


    }
}
package org.example;
import javax.swing.JFrame;

import org.bytedeco.ffmpeg.avcodec.AVPacket;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.OpenCVFrameGrabber;

import java.io.*;
import java.lang.reflect.Array;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Files;

public class App{
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

    //      OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);//新建opencv抓取器,一般的电脑和移动端设备中摄像头默认序号是0,不排除其他情况
     //       FFmpegFrameGrabber grabber=FFmpegFrameGrabber.createDefault("/dev/video0");  //摄像头,加载快

            Socket socket=new Socket("192.168.43.61",3000);
          InputStream is=socket.getInputStream();
          ObjectInputStream objectInputStream=new ObjectInputStream(is);
          byte[] b= (byte[]) objectInputStream.readObject();
          InputStream is1= new ByteArrayInputStream(b);

         FFmpegFrameGrabber grabber=new FFmpegFrameGrabber(is1);
         grabber.start();//开始获取摄像头数据

          CanvasFrame canvas = new CanvasFrame("摄像头预览");//新建一个预览窗口
          canvas.setCanvasSize(800,600);
          canvas.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //窗口是否关闭
      while(canvas.isDisplayable()){
            /*获取摄像头图像并在窗口中显示,这里Frame frame=grabber.grab()得到是解码后的视频图像*/
           Frame frame=grabber.grab();
           canvas.showImage(frame);

       }
       grabber.close();//停止抓取
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值