• vlcj读本地文件和接收网络流并转发rtp http rtsp ts流


    java发送rtp,http,rtsp,ts流和接收网络流再转发--java音频流视频流传输

    添加maven依赖

      <dependency>
                <groupId>uk.co.caprica</groupId>
                <artifactId>vlcj</artifactId>
                <version>4.0.0</version>
            </dependency>
            <dependency>
                <groupId>net.java.dev.jna</groupId>
                <artifactId>jna</artifactId>
                <version>5.3.1</version>
            </dependency>

    将vlc底层动态链接库 plugins libvlc.dll libvlccore.dll (在vlc的安装目录下,没有安装过vlc可以去下面链接拿)导入JDK的bin目录

    链接:https://pan.baidu.com/s/1oVuyW04w8-QeKeWx7MDL3A
    提取码:t1xm

    读取本地MP3文件发送rtp http rtsp 流

    import org.apache.logging.log4j.util.Strings;
    import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
    import uk.co.caprica.vlcj.media.MediaRef;
    import uk.co.caprica.vlcj.medialist.MediaList;
    import uk.co.caprica.vlcj.medialist.MediaListRef;
    import uk.co.caprica.vlcj.player.list.MediaListPlayer;
    import uk.co.caprica.vlcj.player.list.MediaListPlayerEventAdapter;
    import uk.co.caprica.vlcj.player.list.PlaybackMode;
    import java.io.File;
    import java.io.FileFilter;
    import java.util.*;
    /**
     * A simple example combining a media list player along with network streaming.
     * <p>
     * A directory is scanned recursively for mp3 files to create a play-list. This play-list is then
     * randomised before being played through a media list player.
     * <p>
     * Since a multicast streaming protocol is used, you can have any number of concurrent clients. You
     * can use any client you want, whether it is vlc itself, another client built using vlcj or any
     * other client that can receive and play audio over RTP.
     * <p>
     * You specify three arguments on the command-line when starting this application:
     * <ul>
     * <li>root directory to recursively scan for audio files</li>
     * <li>multicast address to stream to</li>
     * <li>port to stream to</li>
     * </ul>
     * For example:
     *
     * <pre>
     *   /home/music 230.0.0.1 5555
     * </pre>
     * <p>
     * Using this example, the streaming audio can be played in vlc with the following address:
     *
     * <pre>
     *   rtp://230.0.0.1:5555
     * </pre>
     */
    public class StreamingAudioTest {
    
        public static Map<String, ControlsEntity> ControlsApiMap = new HashMap<String, ControlsEntity>();
        private final MediaPlayerFactory factory;
    
        private final MediaListPlayer mediaListPlayer;
    
        private final MediaList playList;
    
        private String ebmId;
    
        public static void main(String[] args) throws Exception {
            new StreamingAudioPlay().rtpStart("C:\Users\Administrator\Music\MV\1.mp3", 3, "224.101.102.103", 5001);
    
        }
    
        public StreamingAudioTest() {
            factory = new MediaPlayerFactory();
            mediaListPlayer = factory.mediaPlayers().newMediaListPlayer();
            mediaListPlayer.events().addMediaListPlayerEventListener(new MediaListPlayerEventAdapter() {
                @Override
                public void nextItem(MediaListPlayer mediaListPlayer, MediaRef item) {
                    System.out.println("Playing next item: " + item + ")");
                }
            });
            playList = factory.media().newMediaList();
        }
    
        public void rtpStart(String filePath, int number, String address, int port) throws Exception {
            start(filePath, number, address, port, 1);
        }
    
        public void httpStart(String filePath, int number, int port) throws Exception {
            start(filePath, number, "", port, 2);
        }
    
        public void rtspStart(String filePath, int number, int port) throws Exception {
            start(filePath, number, "", port, 3);
        }
    
        private void start(String filePath, int number, String address, int port, int type) throws Exception {
            System.out.println("Scanning for audio files...");
            // Scan for media files
            List<File> files = new ArrayList<>();
            File file = new File(filePath);
            for (int a = 0; a < number; a++) {
                files.add(file);
            }
            // Randomise the order
            Collections.shuffle(files);
            // Prepare the media options for streaming
            String mediaOptions = Strings.EMPTY;
            if (type == 1) {//rtp
                mediaOptions = formatRtpStream(address, port);
                // Add each media file to the play-list...
                for (File file2 : files) {
                    System.out.println(mediaOptions);
                    playList.media().add(file2.getAbsolutePath(), mediaOptions, ":sout-keep", ":sout-all");
                }
            } else if (type == 2) {//http
                mediaOptions = formatHttpStream(port);
                // Add each media file to the play-list...
                for (File file2 : files) {
                    System.out.println(mediaOptions);
                    playList.media().add(file2.getAbsolutePath(), mediaOptions, ":sout-keep", ":no-sout-all");
                }
            } else {//rtsp
                mediaOptions = formatRtspStream(port);
                // Add each media file to the play-list...
                for (File file2 : files) {
                    System.out.println(mediaOptions);
                    playList.media().add(file2.getAbsolutePath(), mediaOptions, ":sout-keep", ":no-sout-all");
                }
            }
    
    
            // Loop the play-list over and over
            mediaListPlayer.controls().setMode(PlaybackMode.DEFAULT);
            // Attach the play-list to the media list player
            MediaListRef playlistRef = playList.newMediaListRef();
            try {
                mediaListPlayer.list().setMediaList(playlistRef);
            } finally {
                playlistRef.release();
            }
            // Finally, start the media player
            mediaListPlayer.controls().play();
            System.out.println(Thread.currentThread().getName() + "start");
            try {
                Thread.currentThread().join();
            } catch (Exception e) {
                //  e.printStackTrace();
            }
        }
        /**
         * Search a directory, recursively, for mp3 files.
         *
         * @param root root directory
         * @return collection of mp3 files
         */
        private List<File> scanForMedia(String root) {
            List<File> result = new ArrayList<File>(400);
            scanForMedia(new File(root), result);
            return result;
        }
    
        private void scanForMedia(File root, List<File> result) {
            if (root.exists() && root.isDirectory()) {
                // List all matching mp3 files...
                File[] files = root.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File pathname) {
                        return pathname.isFile() && pathname.getName().toLowerCase().endsWith(".mp3");
                    }
                });
                // Add them to the collection
                result.addAll(Arrays.asList(files));
                // List all nested directories...
                File[] dirs = root.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File pathname) {
                        return pathname.isDirectory();
                    }
                });
                // Recursively scan each nested directory...
                for (File dir : dirs) {
                    scanForMedia(dir, result);
                }
            }
        }
    
        /**
         * "vlc.exe " + vlcFile + " :sout=#rtp{dst=" + ip + ",port=" + arr[1]+ "} :loop -I dummy :sout-all :sout-keep"
         *
         * @param serverAddress
         * @param serverPort
         * @return
         */
        private String formatRtpStream(String serverAddress, int serverPort) {
            StringBuilder sb = new StringBuilder(60);
            sb.append(":sout=#rtp{dst=");
            sb.append(serverAddress);
            sb.append(",port=");
            sb.append(serverPort);
            sb.append("}");
            return sb.toString();
        }
        /**
         * "vlc.exe " + vlcFile + " :sout=#http{mux=ts{pid-audio=6216},dst=:" + vlcPort + "/Audio} :loop -I dummy :no-sout-all :sout-keep"
         *
         * @param serverPort
         * @return
         */
        private String formatHttpStream(int serverPort) {
            StringBuilder sb = new StringBuilder(60);
            sb.append(":sout=#http{mux=ts{pid-audio=6216},dst=:");
            sb.append(serverPort);
            sb.append("/audio}");
            return sb.toString();
        }
    
        /**
         * "vlc.exe " + vlcFile + " :sout=#rtp{sdp=rtsp://:" + vlcPort + "/Audio} :loop -I dummy :no-sout-all :sout-keep";
         *
         * @param serverPort
         * @return
         */
        private String formatRtspStream(int serverPort) {
            StringBuilder sb = new StringBuilder(60);
            sb.append(":sout=#rtp{sdp=rtsp://:");
            sb.append(serverPort);
            sb.append("/audio}");
            return sb.toString();
        }
    }
    /**
     * "vlc -I dummy rtp://@:" + ServerHandler.udpPort + " --sout=#duplicate{dst=standard{mux=ts{pid-audio=" + dtmb_pid + "},access=udp,dst=" + tsIp + ":" + tsPort + "}} --sout-all --sout-keep";
     */
    private String formatTsStream(String tsIp, int tsPort, int dtmb_pid) {
        StringBuilder sb = new StringBuilder(200);
        sb.append(":sout=#duplicate{dst=standard{mux=ts{pid-audio=");
        sb.append(dtmb_pid);
        sb.append("},access=udp,dst=");
        sb.append(tsIp);
        sb.append(":");
        sb.append(tsPort);
        sb.append("}}");
        return sb.toString();
    }

    接收rtp流再转发

    public static void main(String[] args) throws Exception {
    // new StreamingAudioPlay().rtpStart("C:\Users\Administrator\Music\MV\1.mp3", 3, "224.101.102.103", 5001);
    new StreamingAudioPlay().start("127.0.0.1", 8000, "224.101.102.103", 5001);

    }
    public void start(String iIp, int iPort, String oIp, int oPort) throws Exception {
    System.out.println("Scanning for audio files...");
    // Scan for media files
    // Randomise the order
    // Prepare the media options for streaming
    String mediaOptions = formatRtpStream(oIp, oPort);
    // Add each media file to the play-list...
    System.out.println(mediaOptions);
    iIp = Utils.isEmpty(iIp) ? "@" : iIp;
    playList.media().add("rtp://" + iIp + ":" + iPort, mediaOptions, ":sout-keep", ":sout-all");
    // Loop the play-list over and over
    mediaListPlayer.controls().setMode(PlaybackMode.DEFAULT);
    // Attach the play-list to the media list player
    MediaListRef playlistRef = playList.newMediaListRef();
    try {
    mediaListPlayer.list().setMediaList(playlistRef);
    } finally {
    playlistRef.release();
    }
    // Finally, start the media player
    mediaListPlayer.controls().play();
    System.out.println("Streaming started at rtp://" + oIp + ":" + oPort);
    // Wait forever...
    Thread.currentThread().join();
    }
  • 相关阅读:
    【2020-02-13】内容变了,但计划本身没变
    【2020-02-12】新的工作计划方式
    【2020-02-10】煮饭那点家常
    【2020-02-10】生活需要不断地相互协调
    nacicat premium 快捷键
    python 爬虫之 正则的一些小例子
    Python爬虫之Cookie和Session
    Python爬虫之关于登录那些事
    爬虫常用库之pyquery 库
    day 39 jq 学习入门2
  • 原文地址:https://www.cnblogs.com/blog-taoa/p/12857017.html
Copyright © 2020-2023  润新知