Giter Site home page Giter Site logo

Comments (22)

techstroke avatar techstroke commented on May 21, 2024

Well, strange enough but changing the Video codec to AV_CODEC_ID_MPEG4 from AV_CODEC_ID_H264 , fixed the problem for now , I'll still keep this issue open for you to look into ! & Thanks a lot for this beautiful library , makes it easier to work with ffmpeg and opencv on android 👍

from javacv.

saudet avatar saudet commented on May 21, 2024

Most (all?) codecs are going to fail when trying to setTimestamp() something that is <= getTimestamp(), so doing something like this should fix the error:

if (recorder.getTimestamp() < frameGrabber.getTimestamp()) {
    recorder.setTimestamp(frameGrabber.getTimestamp());
}

Let me know if that doesn't do the trick though, thanks!

from javacv.

techstroke avatar techstroke commented on May 21, 2024

Thanks for the code !, Well this doesn't work even after replacing the setTimestamp() code with yours . So, I assume this is a bug in H264 codec implementation , as I am in hurry I'll rather change my codec to MPEG4 for now , till this thing gets fixed !

from javacv.

saudet avatar saudet commented on May 21, 2024

Well anyway, it's related to the call to setTimestamp(). If you figure out that this is indeed a problem with FFmpegFrameRecorder and not something wrong with your code or your data, please let me know so I can fix it, thanks!

from javacv.

shuky19 avatar shuky19 commented on May 21, 2024

Hi,

I am having the same problem with my code, I am recording a movie from the native camera's API on an Android devices, later on I am using JavaCV to upload it to an RTMP server.

I have looked at @saudet comment, and implemented the same, but still the last few seconds in my videos are being trimed.

Here is my code (I have closed issue #47 since after further investigation I understood it is due to this exact problem):

    FFmpegFrameRecorder mFrameRecorder = new FFmpegFrameRecorder(urlPath, videoProfile.width, videoProfile.height, 1);
    mFrameRecorder.setFormat("flv");
    mFrameRecorder.setSampleRate(videoProfile.audioSampleRate);
    mFrameRecorder.setFrameRate(videoProfile.frameRate);
    mFrameRecorder.setVideoBitrate(videoProfile.bitRate);
    mFrameRecorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
    mFrameRecorder.setVideoOption("preset", "ultrafast");
    mFrameRecorder.start();

    FFmpegFrameGrabber mGrabber = new FFmpegFrameGrabber(mFilePath);
    mGrabber.setSampleRate(videoProfile.audioSampleRate);
    mGrabber.setFrameRate(videoProfile.frameRate);
    mGrabber.setVideoBitrate(videoProfile.bitRate);
    mGrabber.setVideoCodec(avcodec.AV_CODEC_ID_H264);
    mGrabber.start();


    Frame frame;
    while ((frame = getFrame()) != null) {
          if (frame != null) {
                // If frame contains image
                if (frame.image != null) {
                    // If need to rotate
                    if (mRotation != 0) {
                        frame.image = rotateImage(frame.image);
                    }
                }

                if (mGrabber.getTimestamp() > mFrameRecorder.getTimestamp()) {
                    mFrameRecorder.setTimestamp(mGrabber.getTimestamp());
                } else {
                    Log.w(TAG, "Incorrect timestamp: " + mGrabber.getTimestamp() + " when Recorder on: " + mFrameRecorder.getTimestamp());
                }

                try {
                    mFrameRecorder.record(frame);
                } catch (FrameRecorder.Exception e) {
                    Log.e(TAG, e.getMessage());
                    e.printStackTrace();
                }
            }
    }

Can you take a look? also can you reopen this issue?

thanks,
Shuky

from javacv.

saudet avatar saudet commented on May 21, 2024

@shuky19 Does the same thing happen with Java SE? Or does this happen only with Android?

from javacv.

shuky19 avatar shuky19 commented on May 21, 2024

I tried it only on android...

from javacv.

saudet avatar saudet commented on May 21, 2024

So, could you please try it on Java SE as well?

from javacv.

saudet avatar saudet commented on May 21, 2024

Does this still happen with the latest version of JavaCV? If so, let's reopen this as a bug. Thanks!

from javacv.

saudet avatar saudet commented on May 21, 2024

I think this commit should fix this: fb2206d
Let me know! Thanks

from javacv.

liangkuai avatar liangkuai commented on May 21, 2024

Hi, guys
When I upload file to RTMP server, I get a fault all the time.

[libx264 @ 00000000266ccc00] non-strictly-monotonic PTS

And when I open rtmp stream in VLC, the video play fast abnormally.
I need some help...

from javacv.

kmandayam avatar kmandayam commented on May 21, 2024

Can anyone confirm if this if this works? I am using 1.3.1 and I am still facing this issue

from javacv.

saudet avatar saudet commented on May 21, 2024

@kmandayam Please provide a code snippet to reproduce this issue, thank you!

from javacv.

kmandayam avatar kmandayam commented on May 21, 2024

I'm trying to merge audio while video recording. I'm doing it onPreviewFrame:

    @Override
    public void onPreviewFrame(byte[] data, Camera camera)
    {
        try
        {
            if (songPath != null)
            {
                audioFrame = audioGrabber.grabFrame(true, false, false, false);
            }

        }
        catch (FrameGrabber.Exception e)
        {
            e.printStackTrace();
        }
        /* get video data */
        if (yuvIplImage != null && isRecording)
        {
            try
            {
                ((ByteBuffer) yuvIplImage.image[0].position(0)).put(data);
                timestamp = 1000 * (System.currentTimeMillis() - startTime);
                if (songPath == null)
                {
                    if (timestamp > recorder.getTimestamp())
                    {
                        recorder.setTimestamp(timestamp);
                    }
                }
                else
                {
                    // Updating timestamp if audio is chosen from the library
                    if (recorder.getTimestamp() <= audioGrabber.getTimestamp() )
                    {
                        recorder.setTimestamp(audioGrabber.getTimestamp());
                    }
                }

                if (useFilter)
                {
                    Frame frame2;
                    if (useNexusFilter())
                    {
                        nexusFilter.push(yuvIplImage);
                        while ((frame2 = nexusFilter.pull()) != null)
                        {

                            if (audioFrame != null)
                            {
                                recorder.record(audioFrame);
                            }
                            recorder.record(frame2);
                        }
                    }
                    else
                    {
                        filter.push(yuvIplImage);
                        while ((frame2 = filter.pull()) != null)
                        {

                            if (audioFrame != null)
                            {
                                recorder.record(audioFrame);
                            }
                            recorder.record(frame2);
                        }
                    }
                }
                else
                {
                    if (audioFrame != null)
                    {
                        recorder.record(audioFrame);
                    }
                    recorder.record(yuvIplImage);
                }
            }
            catch (NullPointerException e)
            {
                e.printStackTrace();
            }
            catch (FFmpegFrameRecorder.Exception | FrameFilter.Exception | BufferOverflowException e)
            {
                e.printStackTrace();
            }
        }
    }

I'm initializing and updating the FrameGrabber and recorder in surfaceCreated:

@OverRide
public void surfaceCreated(SurfaceHolder holder)
{
try
{
stopPreview();
camera.setPreviewDisplay(holder);
this.camera.setPreviewCallback(CameraView.this);
if (songPath != null)
{
// Initialize audioGrabber
audioGrabber = new FFmpegFrameGrabber(songPath);
audioGrabber.start();
audioGrabber.setFormat("mp3");
audioGrabber.setFrameRate(FRAME_RATE);
audioGrabber.setSampleRate(AUDIO_RATE);

                // Update recorder settings
                recorder.setSampleRate(audioGrabber.getSampleRate());
                recorder.setFrameRate(FRAME_RATE);
                recorder.setAudioChannels(audioGrabber.getAudioChannels());
                recorder.setSampleRate(AUDIO_RATE);
            }
        }
        catch (FrameGrabber.Exception e)
        {
            e.printStackTrace();
        }
        catch (IOException exception)
        {
            camera.release();
            camera = null;
        }
    }

I'm also losing few seconds of audio before the video ends. Please take a look and let me know if I am going wrong somewhere

from javacv.

saudet avatar saudet commented on May 21, 2024

from javacv.

kmandayam avatar kmandayam commented on May 21, 2024

@saudet I've only tried it on android. Not sure how to reproduce it with Java SE

from javacv.

saudet avatar saudet commented on May 21, 2024

@kmandayam Could you elaborate on why you're not sure how to reproduce this issue with Java SE?

from javacv.

kmandayam avatar kmandayam commented on May 21, 2024

@saudet Sorry for the late reply. I don't have a lot of experience working with javacv, so I don't know how to replicate this problem.

from javacv.

kmandayam avatar kmandayam commented on May 21, 2024

It works fine when I don't try to merge a separate audio file with the video (ie using the mic). All the frames are retained.

from javacv.

saudet avatar saudet commented on May 21, 2024

from javacv.

kmandayam avatar kmandayam commented on May 21, 2024

Well, since I am trying to merge audio while I record video, even if I stop recording mid way through the audio file, it still gets cropped. Could it have to do anything with the sample rate or frame rate?

from javacv.

saudet avatar saudet commented on May 21, 2024

from javacv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.