Video first frame not at zero, transcoded its fps to see if it would fix but the error persist.

I have this code below from a youtube tutorial and his end works perfectly fine, but on my pc it has errors

public GameObject cinemaPlane;
    public GameObject btnPlay;
    public GameObject btnPause;
    public GameObject knob;
    public GameObject progressBar;
    public GameObject progressBarBG;

    private float maxKnobValue;
    private float newKnobX;
    private float maxKnobX;
    private float minKnobX;
    private float knobPosY;
    private float simpleKnobValue;
    private float knobValue;
    private float progressBarWidth;
    private bool knobIsDragging;
    private bool videoIsJumping = false;
    private bool videoIsPlaying = false;
    private VideoPlayer videoPlayer;

    private void Start()
    {
        knobPosY = knob.transform.localPosition.y;
        videoPlayer = GetComponent<VideoPlayer>() ;
        btnPause.SetActive(true);
        btnPlay.SetActive(false);
        videoPlayer.frame = (long)100;
        progressBarWidth = progressBarBG.GetComponent<SpriteRenderer>().bounds.size.x;
    }

    private void Update()
    {
        if (!knobIsDragging && !videoIsJumping)
        {
            if (videoPlayer.frameCount > 0)
            {
                float progress = (float)videoPlayer.frame / (float)videoPlayer.frameCount;
                progressBar.transform.localScale = new Vector3(progressBarWidth * progress, progressBar.transform.localScale.y, 0);
                knob.transform.localPosition = new Vector2(progressBar.transform.localPosition.x + (progressBarWidth * progress), knob.transform.localPosition.y);
            }
        }

        if (Input.GetMouseButtonDown(0))
        {
            Vector3 pos = Input.mousePosition;
            Collider2D hitCollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(pos));

            if (hitCollider != null && hitCollider.CompareTag(btnPause.tag))
            {
                BtnPlayVideo();
            }
            if (hitCollider != null && hitCollider.CompareTag(btnPlay.tag))
            {
                print("playBtn");
                BtnPlayVideo();
            }
        }
    }

    public void KnobOnPressDown()
    {
        VideoStop();
        minKnobX = progressBar.transform.localPosition.x;
        maxKnobX = minKnobX + progressBarWidth;
    }

    public void KnobOnRelease()
    {
        knobIsDragging = false;
        CalcKnobSimpleValue();
        VideoPlay();
        VideoJump();
        StartCoroutine(DelayedSetVideoIsJumpingToFalse());
    }

    IEnumerator DelayedSetVideoIsJumpingToFalse()
    {
        yield return new WaitForSeconds(0);
        SetVideoIsJumpingToFalse();
    }

    public void KnobOnDrag()
    {
        knobIsDragging = true;
        videoIsJumping = true;
        Vector3 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
        knob.transform.position = new Vector2(curPosition.x, curPosition.y);
        newKnobX = knob.transform.localPosition.x;
        if (newKnobX > maxKnobX) { newKnobX = maxKnobX; }
        if (newKnobX < minKnobX) { newKnobX = minKnobX; }
        knob.transform.localPosition = new Vector2(newKnobX, knobPosY);
        CalcKnobSimpleValue();
        progressBar.transform.localScale = new Vector3(simpleKnobValue * progressBarWidth, progressBar.transform.localScale.y, 0);
    }

    private void SetVideoIsJumpingToFalse()
    {
        videoIsJumping = false;
    }

    private void CalcKnobSimpleValue()
    {
        maxKnobValue = maxKnobX - minKnobX;
        knobValue = knob.transform.localPosition.x - minKnobX;
        simpleKnobValue = knobValue / maxKnobValue;
    }

    private void VideoJump()
    {
        var frame = videoPlayer.frameCount * simpleKnobValue;
        videoPlayer.frame = (long)frame;
    }

    private void BtnPlayVideo()
    {
        if (videoIsPlaying)
        {
            VideoStop();
        }
        else
        {
            VideoPlay();
        }
    }

    private void VideoStop()
    {
        videoIsPlaying = false;
        videoPlayer.Pause();
        btnPause.SetActive(false);
        btnPlay.SetActive(true);
    }

    private void VideoPlay()
    {
        videoIsPlaying = true;
        videoPlayer.Play();
        btnPause.SetActive(true);
        btnPlay.SetActive(false);

Im not sure whats the cause, but my clip is only 1:10 minutes long and when played it skips first 2 seconds of the frame rendering my clip a bit laggy when played. showing the error “Video first frame not at zero”
I installed handbrake and transcoded my fps if that would fix it but when i uploaded the transcoded video the error was still there.

Have you tried other players and video files? It will help know if the issue is from the file or the player. Otherwise, can you try enabling VideoPlayer.waitForFirstFrame?

I don’t have any other video players, but I did try uploading different video clips but they all seem to have issues. And apologies I’m completely newb to c#. where can I insert the VideoPlayer.waitForFirstFrame in my codes??

You don’t have even one coming with the OS? Can you install VLC then? https://www.videolan.org/

You could do it through code like this.

private void Start()
{
    knobPosY = knob.transform.localPosition.y;
    videoPlayer = GetComponent<VideoPlayer>() ;
    btnPause.SetActive(true);
    btnPlay.SetActive(false);
    videoPlayer.frame = (long)100;
    videoPlayer.waitForFirstFrame = true; // Here
    progressBarWidth = progressBarBG.GetComponent<SpriteRenderer>().bounds.size.x;
}

and you should be able to toggle it in the UI
8273097--1083990--upload_2022-7-13_0-54-43.png

I tried this but it didn’t work, I checked the WaitForFirstFrame box it didn’t fix it, but this shows up after updating the code though
“WindowsVideoMedia error unhandled Color Standard: 0 falling back to default this may result in rendering issues”
does this color issues affects the video clip when played?

thers also this code error:
Unexpected timestamp values detected. This can occur in H.264 videos not encoded with the baseline profile.

No, this only impact the image colour.

This could have an impact. Can you try this clip? You should be able to see every frame. https://drive.google.com/file/d/1x9FujOIdwJP2ceFbwJsZjrh43uhqGbHD/view?usp=sharing

I have the same problem. First video frame not zero: 256 (8.533333s)

  • In VLC I can play the video with no problems from the second 0, but unity player only play from 8.533 seconds wich is really a problem for me.
  • I could not find an answer from Unity explainig what exactly means “First video frame not zero: 256”, Is not having P-Frames at the beggining?
  • I can not share the link of the video
  • Transcoding is not an option for me because Im playing a external videoFile (not in the project when the build is done)

If it’s and encoding issue… what can I do to on the encoding parameters to fix it?

It means the first frame timestamp is 8.53333s so we honour the data and wait for 8.5333s

@JU4NGUZM4N it depends on your software. In Adobe Premiere, I remember you can specify to make the clip start at 0. You can use FFmpeg to extract the stream and remux it but it would be better if you find the settings in your software to do this.