protonemedia / laravel-ffmpeg

This package provides an integration with FFmpeg for Laravel. Laravel's Filesystem handles the storage of the files.
https://protone.media/en/blog/how-to-use-ffmpeg-in-your-laravel-projects
MIT License
1.66k stars 194 forks source link

Mp4 to Mp3 clips audio to one second. #431

Open Johannestegner opened 2 years ago

Johannestegner commented 2 years ago

Hi!

I'm having an issue in my Laravel queue jobs which converts sounds from multiple formats into mp3. Most audio formats works well, while mp4 sounds uploaded from iOS devices only produce a 1 second long audio stream on conversion.
All sounds are both fetched and uploaded to a s3-like storage, which might be a part in the issue.

I have tried to reproduce the issue by only using ffmpeg directly to no avail.

Is this an issue that has been observed before, or am I doing something wrong?

This is basically the code I use to convert files:

<?php
namespace App\Services;

use App\Services\Contracts\AudioServiceInterface;
use FFMpeg\Format\Audio\Mp3;
use Illuminate\Filesystem\FilesystemManager;
use ProtoneMedia\LaravelFFMpeg\Exporters\EncodingException;
use Psr\Log\LoggerInterface;
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;

class FFMpegService implements AudioServiceInterface {
    protected FilesystemManager $filesystem;
    private LoggerInterface     $logger;

    public function __construct(LoggerInterface $logger, FilesystemManager $filesystem) {
        $this->logger = $logger;
        $this->filesystem = $filesystem;
    }

    public function convert(string $fromPath, string $toPath): bool {

        try {
            // Default disk is a s3-like storage.
            FFMpeg::fromFilesystem($this->filesystem->disk())
                  ->open($fromPath)
                  ->export()
                  ->toDisk($this->filesystem->disk())
                  ->inFormat(new Mp3())
                  ->save($toPath);

            $this->logger->debug('Audio converted', [
                'from'       => $fromPath,
                'to'         => $toPath
            ]);

            FFMpeg::cleanupTemporaryFiles();
        } catch (EncodingException $exception) {
            $this->logger->error('Audio failed to convert', [
                'from'    => $fromPath,
                'to'      => $toPath,
                'command' => $exception->getCommand(),
                'error'   => $exception->getErrorOutput(),
            ]);

            throw $exception;
        }

        return true;
    }

}

There are no exceptions and the logs says that everything went fine, but the mp3 files are 1 second long, every time.


Runtime:

PHP 8.1 Ubuntu 20.04.3 Laravel 9.18 pbmedia/laravel-ffmpeg 8.1.2

FFMPEG output:

ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
  configuration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Hyper fast Audio and Video encoder