0

I have been trying to load video that I recorded from AVPro Movie Capture (Free Version). The video file was in GB so I converted it using ffmpeg command -y -i RawVideo.avi -qscale 7 FinalVideo.avi and saved it to StreamingAssets. I got the desired result. Now I want to play that converted video file in video player for preview. But the problem is when video is played from URL the audio is very choppy. I played it in windows player and VLC and it was fine. The problem occurs only in Unity's VideoPlayer.

PreviewVideo Class:

public class PreviewVideo : MonoBehaviour
{

    public GameObject VideoSelection;
    public GameObject RecordingCanvas;
    public GameObject FacebookCanvas;
    public GameObject Home;
    public Sprite pauseImage;
    public Sprite playImage;
    public VideoPlayer videoPlayer;
    public GameObject EmailCanvas;

    // Use this for initialization
    void Start ()
    {

    }

    public void Referesh()
    {
        videoPlayer.gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("Thumbnails/" + StaticVariables.VideoToPlay);

        videoPlayer.url = Application.streamingAssetsPath + "/FinalVideo.avi";
    }

    public void PlayVideo()
    {
        if (!videoPlayer.isPlaying) {
            videoPlayer.Play ();
        }
    }

    public void Back ()
    {
        this.gameObject.SetActive (false);
        VideoSelection.SetActive (true);
    }

    public void HomeBtn ()
    {
        SceneManager.LoadScene (0);
    }

    public void SendEmailDialogue()
    {
        EmailCanvas.SetActive (true);
        this.gameObject.SetActive (false);
    }

    public void FacebookShare()
    {
        FacebookCanvas.SetActive (true);
    }
}

Refresh() is called from RecordingCanvas class:

public class RecordingCanvas : MonoBehaviour {

    public GameObject VideoSelection;
    public GameObject PreviewVideo;
    public GameObject Home;
    public GameObject canvas;
    public RawImage rawImage;
    public GameObject videoThumbnail;
    float _seconds;
    bool canStart = false;
    public SpriteRenderer NumSprite;
    public VideoPlayer videoPlayer;
    WebCamTexture webcamTexture;
    Process process;
    void Start ()
    {
        Refresh ();
    }

    public void Refresh()
    {
        _seconds = 0;
        NumSprite.gameObject.SetActive(true);
        webcamTexture = new WebCamTexture (1280, 720);
        webcamTexture.Stop ();
        rawImage.texture = webcamTexture;
        rawImage.material.mainTexture = webcamTexture;
        webcamTexture.Play ();
        videoPlayer.loopPointReached += VideoEndReached;

        videoPlayer.gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("Thumbnails/" + StaticVariables.VideoToPlay);
        videoThumbnail.GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite> ("Thumbnails/" + StaticVariables.VideoToPlay);
        videoPlayer.clip = Resources.Load<VideoClip> ("Videos/" + StaticVariables.VideoToPlay);
    }

    void Update()
    {
        _seconds += Time.deltaTime;
        print ((int)_seconds);
        if (_seconds < 1) {
            NumSprite.sprite = Resources.Load<Sprite> ("Numbers/3");
        }
        else if(_seconds <2)
            NumSprite.sprite = Resources.Load<Sprite>("Numbers/2");
        else if(_seconds <3)
            NumSprite.sprite = Resources.Load<Sprite>("Numbers/1");


        if (_seconds >= 3 && _seconds <=4 ) {
            canStart = true;
        }

        if (canStart) {
            NumSprite.gameObject.SetActive(false);
            canStart = false;
            FindObjectOfType<CaptureGUI> ().StartCapture();
            videoPlayer.Play ();
            videoThumbnail.SetActive (false);
        }

    }

    IEnumerator StartConversion()
    {
        yield return new WaitForSeconds (1.5f);
        process = new Process();

        if (File.Exists (Application.streamingAssetsPath + "/FinalVideo.avi"))
            File.Delete(Application.streamingAssetsPath + "/FinalVideo.avi");

        process.StartInfo.WorkingDirectory = Application.streamingAssetsPath;
        process.StartInfo.FileName = Application.streamingAssetsPath + "/ffmpeg.exe";
        process.StartInfo.Arguments = " -y -i " + StaticVariables.RawVideo + ".avi " + "-qscale 7 " + StaticVariables.FinalVideo + ".avi";
        process.StartInfo.CreateNoWindow = false;
        process.EnableRaisingEvents = true;
        process.Exited += new EventHandler(Process_Exited);
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit ();
        canvas.SetActive (false);
        PreviewVideo.SetActive (true);
        FindObjectOfType<PreviewVideo> ().Referesh ();
        File.Copy (Application.streamingAssetsPath + "/FinalVideo.avi", @"C:\xampp\htdocs\facebook\images\FinalVideo.avi", true);
        this.gameObject.SetActive (false);
    }

    void Process_Exited(object sender, EventArgs e)
    {
        process.Dispose ();
    }

    void VideoEndReached(UnityEngine.Video.VideoPlayer vp)
    {
        videoPlayer.Stop ();
        FindObjectOfType<CaptureGUI> ().StopCapture();
        webcamTexture.Stop ();
        canvas.SetActive (true);
        StartCoroutine(StartConversion ());
    }

}

I am using Unity version 2017.1.1p4 personal edition. Windows 10 with high end PC. I am making this app for standalone PC.

I am stuck here. Can't proceed further. Please help me with this issue.

Saad Anees
  • 1,255
  • 1
  • 15
  • 32
  • Post the code you are using to play the video – Programmer Nov 08 '17 at 17:26
  • for assigning url `videoPlayer.url = Application.streamingAssetsPath + "/FinalVideo.avi";` on start and `videoPlayer.Play()` on button click – Saad Anees Nov 08 '17 at 18:15
  • No. Post the complete code in your question. Also mention what platform you tested this on that is slow – Programmer Nov 08 '17 at 18:18
  • @Programmer i have edited my question and added code. – Saad Anees Nov 08 '17 at 18:50
  • This is a known problem with large videos in Unity. Wait for the video to be prepared with `isPrepared` before playing it. See [this](https://stackoverflow.com/questions/41144054/using-new-unity-videoplayer-and-videoclip-api-to-play-video/41154599#41154599) post. – Programmer Nov 08 '17 at 19:04
  • @Programmer isPrepared seems to be working only with videoClip not URL. – Saad Anees Nov 11 '17 at 08:47

0 Answers0