Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rtmp streams are stored as mp4 files #1953

Open
fantasylhd opened this issue Dec 8, 2022 · 5 comments
Open

Rtmp streams are stored as mp4 files #1953

fantasylhd opened this issue Dec 8, 2022 · 5 comments

Comments

@fantasylhd
Copy link

fantasylhd commented Dec 8, 2022

Hi,I want to store the rmtp stream as an mp4 file,For example, use the ffmpeg command :ffmpeg -i rtmp://xx.com/3573613170/18573613170 -f mp4 -vcodec copy -acodec copy C:\Users\lhd\Desktop\2.mp4
But what should we do in javacv can you give me some advise? thank you! If there is some example code, that's perfect! thank you ~~

This is the code I tried to use javacv to complete this task, but the recorded video cannot be played:

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("rtmp://xx.com/3573613170/18573613170");
        grabber.start();
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("C:\\Users\\lhd\\Desktop\\2.mp4", grabber.getImageWidth(), grabber.getImageHeight(),grabber.getAudioChannels());
        recorder.setFormat("mp4");
        recorder.start(grabber.getFormatContext());
        LocalDateTime startTime = LocalDateTime.now();
        int times = 0;
        AVPacket avPacket = null;
        while (startTime.plusSeconds(10).compareTo(LocalDateTime.now()) > 0) {
            avPacket = grabber.grabPacket();
            if (avPacket != null) {
                recorder.recordPacket(avPacket);
                times++;
                System.out.println("record! "+times);
            }
        }
        recorder.close();
        grabber.close();

Dependent version:

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.5.8</version>
        </dependency>
@saudet
Copy link
Member

saudet commented Dec 8, 2022

We can easily use the ffmpeg program from Java as well if that does what you need:
http://bytedeco.org/javacpp-presets/ffmpeg/apidocs/org/bytedeco/ffmpeg/ffmpeg.html

@fantasylhd
Copy link
Author

What is the problem with my code? Javacv does not support such writing

@saudet
Copy link
Member

saudet commented Dec 8, 2022

Well, you didn't set the codec, so maybe it's not using a codec supported by your player?

@fantasylhd
Copy link
Author

Cannot play after encoder is set

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("rtmp://xxx.com/3573613170/18573613170");
        grabber.start();
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("C:\\Users\\lhd\\Desktop\\2.mp4", grabber.getImageWidth(), grabber.getImageHeight(),grabber.getAudioChannels());
        recorder.setVideoCodec(grabber.getVideoCodec());
        recorder.setAudioCodec(grabber.getAudioCodec());
        recorder.setFormat("mp4");
        recorder.start(grabber.getFormatContext());
        LocalDateTime startTime = LocalDateTime.now();
        int times = 0;
        AVPacket avPacket = null;
        while (startTime.plusSeconds(10).compareTo(LocalDateTime.now()) > 0) {
            avPacket = grabber.grabPacket();
            if (avPacket != null) {
                recorder.recordPacket(avPacket);
                times++;
                System.out.println("record! "+times);
            }
        }
        recorder.close();
        grabber.close();

@fantasylhd
Copy link
Author

fantasylhd commented Dec 8, 2022

Stored as flv files can be played

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("rtmp://xxx.com/3573613170/18573613170");
        grabber.start();
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("C:\\Users\\lhd\\Desktop\\2.flv", grabber.getImageWidth(), grabber.getImageHeight(),grabber.getAudioChannels());
//        recorder.setVideoCodec(grabber.getVideoCodec());
//        recorder.setAudioCodec(grabber.getAudioCodec());
        recorder.setFormat("flv");
        recorder.start(grabber.getFormatContext());
        LocalDateTime startTime = LocalDateTime.now();
        int times = 0;
        AVPacket avPacket = null;
        while (startTime.plusSeconds(10).compareTo(LocalDateTime.now()) > 0) {
            avPacket = grabber.grabPacket();
            if (avPacket != null) {
                recorder.recordPacket(avPacket);
                times++;
                System.out.println("record! "+times);
            }
        }
        recorder.close();
        grabber.close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants