0

I want to be able to change the frame rate of a video from 0.05fps to 3fps.
I receive the video as an Illuminate\Http\UploadedFile element, with mp4 format, how do I speed up its frame rate before sending it via email?
The video is 25 minutes long, it should become 25 seconds long after changing the frame rate.
Is there a way to change the fps without saving the element as a File?
I was planning to use the ffmpeg repository here but it requires me to save both the initial file, and the second file after changing the fps.

This is my code:

<?php

namespace Server\Mail;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Http\UploadedFile;

class TimeLapse extends Mailable
{
    use Queueable, SerializesModels;

    public UploadedFile $video;

    public bool $is_timelapsed;

    public string $format;

    /**
     * Create a new message instance.
     */
    public function __construct(
        UploadedFile $video, 
        bool $is_timelapsed, 
        string $format
    ){
        $this->video = $video;
        $this->is_timelapsed = $is_timelapsed;
        $this->format = $format;
    }

    private function timelapse(){
        // here I want to change $video element's fps from 0.05 to 3
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        if ( !$is_timelapsed ) { timelapse(); }

        $this->subject('message')
                    ->text('emails.timelapse');

        $this->attach($this->video, [
            'as' => 'sample'.$format,
            'mime' => 'video/'.$format,
        ]);

        return $this;
    }
}
The Blind Hawk
  • 1,199
  • 8
  • 24
  • Don't know PHP, but you need to either use [the `setpts` filter](https://ffmpeg.org/ffmpeg-filters.html#setpts_002c-asetpts) or force change the input framerate (`-r 3 -i ...`). The latter is a brute-force approach, and it may not always work. – kesh Aug 22 '22 at 15:36
  • @kesh Thanks, but is it possible for me to do that without having to save the file on my server? – The Blind Hawk Aug 23 '22 at 04:57
  • Yes. FFmpeg can modify the parameter on the fly as you stream. (That being said, you may need to re-encode on the fly to achieve this, which may not be what you'd like to do, but give it a try first.) – kesh Aug 23 '22 at 05:37
  • @kesh I am recording the video using MediaRecorder API on javascript. [stack thread](https://stackoverflow.com/questions/73412855/create-a-timelapse-video-using-mediarecorder-api-and-ffmpeg?noredirect=1#comment129698456_73412855). At what point, and how should I call ffmpeg to modify the parameter? – The Blind Hawk Aug 23 '22 at 05:45
  • You seem to be telling 2 different stories ("without having to save the file on my server" vs. "recording the video")... In any case, all I can say is "it 100% depends on your app design/logic." All, I can tell you from ffmpeg perspective is that it can change the rate when it transcodes (again, probably not if you're just stream copying) – kesh Aug 23 '22 at 14:13
  • I found a solution for javascript MediaRecorder API [here](https://stackoverflow.com/questions/73412855/create-a-timelapse-video-using-mediarecorder-api-and-ffmpeg?noredirect=1#comment129698456_73412855) (so before sending the video to the server side), but it does not exactly answer this question, so I will be leaving it open. – The Blind Hawk Aug 24 '22 at 03:09

0 Answers0