Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to save concatenated video #580

Closed
FrancescoMussi opened this issue Oct 1, 2018 · 14 comments
Closed

Unable to save concatenated video #580

FrancescoMussi opened this issue Oct 1, 2018 · 14 comments

Comments

@FrancescoMussi
Copy link

FrancescoMussi commented Oct 1, 2018

Q A
Bug? no
New Feature? no
Version Used dev-master#c11b79ab5b0174aa1a56c54c67491169e78a4c17
FFmpeg Version 4.0.2
OS MacOS High Sierra 10.13.6

Actual Behavior

I am trying to concat two videos, but I am getting the following error:

ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-f' 'concat' '-safe' '0' '-i' '/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concateaHp0L' '-c' 'copy' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4'

This is a dump of the commands:

screen shot 2018-10-01 at 16 59 53

Expected Behavior

I expected the command to be executed.

Steps to Reproduce

For the code I am following the docs:

$video = $ffmpeg->open( $path1 );
$video
    ->concat(array($path1, $path2))
    ->saveFromSameCodecs($path_output, TRUE);

EDIT:
I have tried a basic resizing and conversion and it worked fine.
So it should everything alright with the installation of the library and the installation of FFMpeg in the system.
So it may be just related with some details concerning the concat.

EDIT2:
I have opened a SO question: https://stackoverflow.com/questions/52593866/php-ffmpeg-how-to-properly-concatenate-two-videos
And it seems that another user (this time on windows) encounter the same issue.
Could you please be so kind to test if the concat function it is still working properly?
Thanks!

@rolandstarke
Copy link
Contributor

rolandstarke commented Oct 1, 2018

I got the same error on windows. Changing the following line in the library

$line .= "file ".$videoPath;

to

$line .= "file '" . $videoPath . "'";

fixed the error for me. But I am not sure about the correct escaping.

Here the contents of the temp file not working

file C:\Users\Roland\Documents\MyStuff\programmieren\test/big_buck_bunny.mp4
file C:\Users\Roland\Documents\MyStuff\programmieren\test/big_buck_bunny.mp4

and here the working version

file 'C:\Users\Roland\Documents\MyStuff\programmieren\test/big_buck_bunny.mp4'
file 'C:\Users\Roland\Documents\MyStuff\programmieren\test/big_buck_bunny.mp4'

@FrancescoMussi
Copy link
Author

I think I have found the cause.

When I am trying to concat using the command line:

ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4

I get this error:

screen shot 2018-10-02 at 11 29 01

And that may be because the two video files have audio opus codec.

So if I run this commands it works:

ffmpeg -f concat -safe 0 -i mylist.txt -c:v copy -c:a aac output.mp4

The video is properly created as a concatenation of the two videos.

Do you know how can change the code, in order to convert the output file to aac?

@jens1o
Copy link
Member

jens1o commented Oct 3, 2018

Do you know how can change the code, in order to convert the output file to aac?

Not supported directly, yet. Although it sounds very decent for a new version(1.x).

@FrancescoMussi
Copy link
Author

Thanks for replying @jens1o.

I am now try a simple concat using two files mp4 with aac audio codec.

It should work but it doesn't :(
Everytime I get this error:

ffmpeg failed to execute command '/usr/local/bin/ffmpeg' '-f' 'concat' '-safe' '0' '-i' '/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concatVZ6Ncg' '-c' 'copy' '/Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4'

Do you know why? What can be the reason ffmpeg cannot execute the command?
Did somebody encounter a similar problem?
Do you have some hints for more debugging.. I am totally out of ideas...

@jens1o
Copy link
Member

jens1o commented Oct 3, 2018

Please send us the hole output of /usr/local/bin/ffmpeg -f concat -safe 0 -i /private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concatVZ6Ncg -c copy /Users/francescomussi/Desktop/Apps/cameraProject/back-end/camera-laravel/storage/app/public/videos/output.mp4

@FrancescoMussi
Copy link
Author

This is the output:

/private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ffmpeg-concatVZ6Ncg: No such file or directory

screen shot 2018-10-03 at 12 02 36

This is what happen inside the folder /private/var/folders/dw/919v2nds7s78pz_qhp7z9rcm0000gn/T/ right after I run the command:

ffmpeg

Something is being created and deleted soon afterwards

@jens1o
Copy link
Member

jens1o commented Oct 3, 2018

Okay, more debugging here. Could you give me a full list of files(with their respective filepath) you want to concat?

@FrancescoMussi
Copy link
Author

These are the paths of the two mp4 videos and the output:

screen shot 2018-10-03 at 12 34 10

@FrancescoMussi
Copy link
Author

If I can exclude with certainty some reasons for the failure I would say:

  • It's not because of the files themselves (I have tried with many different mp4 and webm videos - also video samples downloaded here and there)
  • It's not because of the path (I don't get any ffprobe error message)
  • It's not because of the ffmpeg in the system (command lines in console work. I also have reinstalled it with all the possible libraries included)

What else can be?
The library has been installed through the laravel bundle: https://github.com/pascalbaljetmedia/laravel-ffmpeg

In the composer.json of that bundle, the PHP-FFMpeg library is included as following:

"php-ffmpeg/php-ffmpeg": "dev-master#c11b79ab5b0174aa1a56c54c67491169e78a4c17",

Is that correct? I am not sure which version is that..

@FrancescoMussi
Copy link
Author

And actually this is the whole code:

$disk = FFMpeg::fromDisk('local');

$video1 = $disk->open('public/videos/test3.mp4');
$video2 = $disk->open('public/videos/test4.mp4');
$output = $disk->open('public/videos/output.mp4');
$path1 = $video1->getPathfile();
$path2 = $video2->getPathfile();
$output_path = $output->getPathfile();

$result = $video1
    ->concat([$path1, $path2])
    ->saveFromSameCodecs($output_path, TRUE);

return response()->json(compact('result')); 

@FrancescoMussi
Copy link
Author

FrancescoMussi commented Oct 3, 2018

The issue has been finally been resolved!

The problem was that I totally misunderstood about the output file.
I thought it had to exists and with video and audio coded of the other videos.
I don't remember where I read it or if I dream about.

That was the reason why it was failing...

If the output doesn't exists then everything works perfectly!!

Thank you very much for your time and sorry for trivial error!

By the way is it possible to improve that error message in the source code.
For example:

  • before executing the commands - check that there is no existing file in the output_path given.

  • If it exists then give error: 'output file already exists in the path given. Commands cannot be executed'

Something like that.
Otherwise ffmpeg failed to execute command ... is generic and difficult to identify the actual cause.

If is possible would be great!

@jens1o
Copy link
Member

jens1o commented Oct 3, 2018

Great! Yeah, giving users a hint what could have failed is on track #310 :)

@FrancescoMussi
Copy link
Author

Ah great! That will be perfect!

@VicGUTT
Copy link

VicGUTT commented Apr 22, 2019

If anyone is still encountering this problem (on Window at least), consider the following code :

// Creating the files to work with
// $this->storage --> \Illuminate\Support\Facades\Storage::disk('testing')
if (!$this->storage->exists('ffmpeg/file-to-concat-1.mp4')) {
    $this->storage->copy('ffmpeg/some-file.mp4', 'ffmpeg/file-to-concat-1.mp4');
}
if (!$this->storage->exists('ffmpeg/file-to-concat-2.mp4')) {
    $this->storage->copy('ffmpeg/some-file.mp4', 'ffmpeg/file-to-concat-2.mp4');
}
if (!$this->storage->exists('ffmpeg/file-to-concat-3.mp4')) {
    $this->storage->copy('ffmpeg/some-file.mp4', 'ffmpeg/file-to-concat-3.mp4');
}

$basePath = storage_path('app/testing/ffmpeg');

// $this->ffmpeg --> \Pbmedia\LaravelFFMpeg\FFMpegFacade
$this->ffmpeg::fromDisk('testing')->open('ffmpeg/file-to-concat-1.mp4')
        ->concat([
            str_replace('\\', '/', $basePath . '/file-to-concat-2.mp4'),
            str_replace('\\', '/', $basePath . '/file-to-concat-3.mp4')
        ])
        ->saveFromSameCodecs(str_replace('\\', '/', $basePath . '/' . time() . '-concatenated-file.mp4'), true);

The important things to note here are :

  • str_replace('\\', '/', 'full-path-to-file-to-concat.mp4')
  • saveFromSameCodecs(str_replace('\\', '/', 'full-path-to-concatenated-file-uniquely-named.mp4'), true);

Explication

My code kept throwing the "Unable to save concatenated video" error and it was impossible to figure out why from the this PHP package side of things. So I in my terminal I had to reproduce what the Concat.php#L82 was doing, which is (for those too lazy to read the code) :
Create a file-to-concat-list.txt with the following content :

file C:/[...]/file-to-concat-2.mp4
file C:/[...]/file-to-concat-3.mp4

replacing all the \ with /, otherwise you'll get an Impossible to open 'file-to-concat-2.mp4'.

And finally running the command :

"C:/ffmpeg/bin/ffmpeg.exe" -f concat -safe 0 -i C:/[...]/file-to-concat-list.txt -c copy "C:/[...]/concatinated-file.mp4"

After some trials and errors I adapted my code to one that finally work as posted above.

Oh and yeah, make sure the output file does not exist otherwise you'll get an other error.

Hope it helps 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants