Search Unity

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

Discussion in 'Audio & Video' started by larieflor, Jul 11, 2022.

  1. larieflor

    larieflor

    Joined:
    Jul 2, 2022
    Posts:
    8
    I have this code below from a youtube tutorial and his end works perfectly fine, but on my pc it has errors
    Code (CSharp):
    1. public GameObject cinemaPlane;
    2.     public GameObject btnPlay;
    3.     public GameObject btnPause;
    4.     public GameObject knob;
    5.     public GameObject progressBar;
    6.     public GameObject progressBarBG;
    7.  
    8.     private float maxKnobValue;
    9.     private float newKnobX;
    10.     private float maxKnobX;
    11.     private float minKnobX;
    12.     private float knobPosY;
    13.     private float simpleKnobValue;
    14.     private float knobValue;
    15.     private float progressBarWidth;
    16.     private bool knobIsDragging;
    17.     private bool videoIsJumping = false;
    18.     private bool videoIsPlaying = false;
    19.     private VideoPlayer videoPlayer;
    20.  
    21.     private void Start()
    22.     {
    23.         knobPosY = knob.transform.localPosition.y;
    24.         videoPlayer = GetComponent<VideoPlayer>() ;
    25.         btnPause.SetActive(true);
    26.         btnPlay.SetActive(false);
    27.         videoPlayer.frame = (long)100;
    28.         progressBarWidth = progressBarBG.GetComponent<SpriteRenderer>().bounds.size.x;
    29.     }
    30.  
    31.     private void Update()
    32.     {
    33.         if (!knobIsDragging && !videoIsJumping)
    34.         {
    35.             if (videoPlayer.frameCount > 0)
    36.             {
    37.                 float progress = (float)videoPlayer.frame / (float)videoPlayer.frameCount;
    38.                 progressBar.transform.localScale = new Vector3(progressBarWidth * progress, progressBar.transform.localScale.y, 0);
    39.                 knob.transform.localPosition = new Vector2(progressBar.transform.localPosition.x + (progressBarWidth * progress), knob.transform.localPosition.y);
    40.             }
    41.         }
    42.  
    43.         if (Input.GetMouseButtonDown(0))
    44.         {
    45.             Vector3 pos = Input.mousePosition;
    46.             Collider2D hitCollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(pos));
    47.  
    48.             if (hitCollider != null && hitCollider.CompareTag(btnPause.tag))
    49.             {
    50.                 BtnPlayVideo();
    51.             }
    52.             if (hitCollider != null && hitCollider.CompareTag(btnPlay.tag))
    53.             {
    54.                 print("playBtn");
    55.                 BtnPlayVideo();
    56.             }
    57.         }
    58.     }
    59.  
    60.     public void KnobOnPressDown()
    61.     {
    62.         VideoStop();
    63.         minKnobX = progressBar.transform.localPosition.x;
    64.         maxKnobX = minKnobX + progressBarWidth;
    65.     }
    66.  
    67.     public void KnobOnRelease()
    68.     {
    69.         knobIsDragging = false;
    70.         CalcKnobSimpleValue();
    71.         VideoPlay();
    72.         VideoJump();
    73.         StartCoroutine(DelayedSetVideoIsJumpingToFalse());
    74.     }
    75.  
    76.     IEnumerator DelayedSetVideoIsJumpingToFalse()
    77.     {
    78.         yield return new WaitForSeconds(0);
    79.         SetVideoIsJumpingToFalse();
    80.     }
    81.  
    82.     public void KnobOnDrag()
    83.     {
    84.         knobIsDragging = true;
    85.         videoIsJumping = true;
    86.         Vector3 curScreenPoint = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    87.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
    88.         knob.transform.position = new Vector2(curPosition.x, curPosition.y);
    89.         newKnobX = knob.transform.localPosition.x;
    90.         if (newKnobX > maxKnobX) { newKnobX = maxKnobX; }
    91.         if (newKnobX < minKnobX) { newKnobX = minKnobX; }
    92.         knob.transform.localPosition = new Vector2(newKnobX, knobPosY);
    93.         CalcKnobSimpleValue();
    94.         progressBar.transform.localScale = new Vector3(simpleKnobValue * progressBarWidth, progressBar.transform.localScale.y, 0);
    95.     }
    96.  
    97.     private void SetVideoIsJumpingToFalse()
    98.     {
    99.         videoIsJumping = false;
    100.     }
    101.  
    102.     private void CalcKnobSimpleValue()
    103.     {
    104.         maxKnobValue = maxKnobX - minKnobX;
    105.         knobValue = knob.transform.localPosition.x - minKnobX;
    106.         simpleKnobValue = knobValue / maxKnobValue;
    107.     }
    108.  
    109.     private void VideoJump()
    110.     {
    111.         var frame = videoPlayer.frameCount * simpleKnobValue;
    112.         videoPlayer.frame = (long)frame;
    113.     }
    114.  
    115.     private void BtnPlayVideo()
    116.     {
    117.         if (videoIsPlaying)
    118.         {
    119.             VideoStop();
    120.         }
    121.         else
    122.         {
    123.             VideoPlay();
    124.         }
    125.     }
    126.  
    127.     private void VideoStop()
    128.     {
    129.         videoIsPlaying = false;
    130.         videoPlayer.Pause();
    131.         btnPause.SetActive(false);
    132.         btnPlay.SetActive(true);
    133.     }
    134.  
    135.     private void VideoPlay()
    136.     {
    137.         videoIsPlaying = true;
    138.         videoPlayer.Play();
    139.         btnPause.SetActive(true);
    140.         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.
     
  2. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    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?
     
  3. larieflor

    larieflor

    Joined:
    Jul 2, 2022
    Posts:
    8
    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??
     
  4. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    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.
    Code (CSharp):
    1. private void Start()
    2. {
    3.     knobPosY = knob.transform.localPosition.y;
    4.     videoPlayer = GetComponent<VideoPlayer>() ;
    5.     btnPause.SetActive(true);
    6.     btnPlay.SetActive(false);
    7.     videoPlayer.frame = (long)100;
    8.     videoPlayer.waitForFirstFrame = true; // Here
    9.     progressBarWidth = progressBarBG.GetComponent<SpriteRenderer>().bounds.size.x;
    10. }
    and you should be able to toggle it in the UI
    upload_2022-7-13_0-54-43.png
     
  5. larieflor

    larieflor

    Joined:
    Jul 2, 2022
    Posts:
    8
    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?
     
  6. larieflor

    larieflor

    Joined:
    Jul 2, 2022
    Posts:
    8
    thers also this code error:
    Unexpected timestamp values detected. This can occur in H.264 videos not encoded with the baseline profile.
     
  7. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
  8. JuanGuzmanH

    JuanGuzmanH

    Joined:
    Feb 8, 2018
    Posts:
    74
    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?
     
  9. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    It means the first frame timestamp is 8.53333s so we honour the data and wait for 8.5333s

    @JuanGuzmanH 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.