0

I have used tus.js client and tus-php to upload videos to my server. But after uploading the files when I try to stream it on the player it is waiting the whole video to load to start playing.

This is my client

 const input = document.querySelector(".videoUploadInput");
        input.addEventListener('change', function(e) {
            // Get the selected file from the input element
            var file = e.target.files[0]
            const extension = file.name.split('.').pop();
            const uniqueId = Date.now().toString(36);
            const tempFileName = `${uniqueId}_${Date.now()}.${extension}`;
            // Create a new tus upload
            var upload = new tus.Upload(file, {
                headers: {
                    'X-CSRF-TOKEN': "{{ csrf_token() }}", // Include the CSRF token in the headers
                },
                // Endpoint is the upload creation URL from your tus server
                endpoint: '/tus',
                // Retry delays will enable tus-js-client to automatically retry on errors
                retryDelays: [0, 3000, 5000, 10000, 20000],
                // Attach additional meta data about the file for the server
                metadata: {
                    filename: tempFileName,
                    filetype: file.type,
                },
                // Callback for errors which cannot be fixed using retries
                onError: function(error) {
                    console.log('Failed because: ' + error)
                },
                // Callback for reporting upload progress
                onProgress: function(bytesUploaded, bytesTotal) {
                    var percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2)
                    console.log(bytesUploaded, bytesTotal, percentage + '%')

                    $('.progress-bar').css('width', percentage + '%').text(percentage + '%');

                },
                // Callback for once the upload is completed
                onSuccess: function() {
                    console.log("UPLOAD", upload);
                    $('.VideoInputFile').val('/storage/videos/' + tempFileName);
                    // console.log('Download %s from %s', upload.file.name, upload.url)
                    $('.submitButton').attr('disabled', false);
                },
            })

            // Check if there are any previous uploads to continue.
            upload.findPreviousUploads().then(function(previousUploads) {
                // Found previous uploads so we select the first one.
                if (previousUploads.length) {
                    upload.resumeFromPreviousUpload(previousUploads[0])
                }

                // Start the upload
                upload.start()
            })
        })

This is my backend service provider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use TusPhp\Tus\Server as TusServer;

class TusServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {



        $this->app->singleton('tus-server', function ($app) {
            $server = new TusServer();

            $server->setApiPath('/tus');
            $server->setUploadDir(public_path('storage/videos'));

            return $server;
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

I am sending video chunks to my route and my route initiates the provider and it combines the video chunks. But after it combines the video to a single file I am waiting the whole video to load to stream

  • So the issue is actually about the streaming process rather than the upload process? In that case the code which does the uploading probably isn't relevant. Instead you need to show us how you are loading the video into the page for viewing. Also have you studied any guides about doing video streaming on the Web with a php backend? I'm sure someone must have posted useful info in the past – ADyson Jun 18 '23 at 09:33
  • well Actually I believe It is about the uploading process, I am just giving the source to the videoPlayer. I am assuming when I upload the video I am missing something maybe my upload process is changing the metadata or video's codec ? – islam özçelik Jun 18 '23 at 10:01
  • @islamözçelik It's possible your MP4 file(s) have their metadata bytes placed at the back of the file (it's actually a typical state for MP4 format). In such cases that file plays immediately from your hard drive because all bytes (from front to back) are available to the media player. You need to fix your file bytes (using Javascript) before you upload, or else fix file from the server side using PHP. **To fix** means editing your bytes by [doing these steps](https://stackoverflow.com/a/18552833/2057709)... Open a small MP4 in a **Hex editor** to see the "atoms" mentioned in that Answer – VC.One Jun 18 '23 at 10:40
  • @islamözçelik PS: If you're uploading your own files then try a command line tool like FFmpeg to do a `faststart` operation on the MP4 before using it as an upload input. If your allows other users to upload then you need your own code to fix any given MP4. – VC.One Jun 18 '23 at 10:44
  • can you give me an example how to fix my video files with ffmpeg ? because I am allowing users to upload files as well. I think It would be better to fix mp4 files with fast start with javascript but I have no idea how – islam özçelik Jun 18 '23 at 11:03

0 Answers0