holzschu / a-Shell-commands

shell commands, pre-compiled to webAssembly, ready to use in a-Shell
554 stars 21 forks source link

ffmpeg unable to accept multiple input files #5

Closed jankais3r closed 3 years ago

jankais3r commented 3 years ago

Hi,

I tried to use the pre-compiled ffmpeg to merge images into a video, but none of the possible approaches work.

Approach 1:

$ ffmpeg -pattern_type glob -i '*.bmp' -c:v mpeg4 -vf fps=5 out.avi
[image2 @ 0xd8a440] Pattern type 'glob' was selected but globbing is not supported by this libavformat build
frames/*.bmp: Function not implemented

Approach 2:

$ ffmpeg -i frame_%04d.bmp -c:v mpeg4 -vf fps=5 out.avi
[image2 @ 0xd8a400] Could find no file with path 'frame_%04d.bmp' and index in the range 0-4
frame_%04d.bmp: No such file or directory

Approach 3:

$ ffmpeg -f image2 -r 5 -i frame_0000.bmp frame_0001.bmp frame_0002.bmp frame_0003.bmp frame_0004.bmp -c:v mpeg4 -vf fps=5 out.avi
[image2 @ 0xd8a5f0] Could find no file with path 'frame_0000.bmp' and index in the range 0-4
frame_0000.bmp: No such file or directory

$ ls
frame_0000.bmp  frame_0001.bmp  frame_0002.bmp  frame_0003.bmp  frame_0004.bmp

Approach 4:

$ ffmpeg -f concat -safe 0 -i files.txt -c:v mpeg4 -vf fps=5 out.avi
[concat @ 0xd8a3d0] Line 1: unknown keyword 'frame_0000.bmp'
files.txt: Invalid data found when processing input

Approach 5:

ls *.bmp | ffmpeg -f image2pipe -c:v bmp -r 5 -i - -c:v mpeg4 -vf fps=5 out.avi
[bmp @ 0xd8b3b0] Warning: not compiled with thread support, using thread emulation
[bmp @ 0xd8b3b0] bad magic number
[image2pipe @ 0xd8a450] decoding for stream 0 failed
[image2pipe @ 0xd8a450] Could not find codec parameters for stream 0 (Video: bmp, none): unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, image2pipe, from 'pipe:':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: bmp, none, 5 tbr, 5 tbn, 5 tbc
[bmp @ 0xd8b8c0] Warning: not compiled with thread support, using thread emulation
Stream mapping:
  Stream #0:0 -> #0:0 (bmp (native) -> mpeg4 (native))
[bmp @ 0xd8b8c0] bad magic number
Error while decoding stream #0:0: Invalid data found when processing input
Cannot determine format of input stream 0:0 after EOF
Error marking filters as finished
Conversion failed!
$ file frame_0000.bmp 
frame_0000.bmp: PC bitmap, Windows 3.x format, 500 x 500 x 24, image size 750000, resolution 3780 x 3780 px/m, cbSize 750054, bits offset 54

Besides this, there is also a problem with many lacking encoders and decoders, even though the binary is presented as “full version”. There is basically no modern encoder like h264 or h265 (these are present only as decoders), forcing the use of ancient mpeg4. Also basic image decoders are missing, like PNG or JPEG.

One potential solution to this problem would be to generate a single GIF file with all the frames and then use it as an input for ffmpeg, but Pillow produces horrible GIFs. I see you also have a fork of imagemagick - is there a plan to release a wasm binary of it?

holzschu commented 3 years ago

Hi, I'm going to have a look. ffmpeg is currently compiled with everything included with the distribution but no external library. Including external libraries is the next step.

ImageMagick (compiled to arm64) is included with the latest versions of a-Shell.

holzschu commented 3 years ago

Some issues could be related to wasm and how it access the local file system.

jankais3r commented 3 years ago

Hi, I'm going to have a look. ffmpeg is currently compiled with everything included with the distribution but no external library. Including external libraries is the next step.

ImageMagick (compiled to arm64) is included with the latest versions of a-Shell.

Thank you for the information on imagemagick. I was able to create a good looking animated GIF with convert, then make a video out of it using ffmpeg. Unfortunately, as far as I can tell, there is not a single codec supported by iOS. So while I was able to generate a valid mpeg4 AVI, there is no way to play it or share it on iOS. Support for additional encoders would be immensely helpful. Thank you for your work on a-shell.

holzschu commented 3 years ago

Hi, I've added libx264 to the ffmpeg compiled version, if you can give it a try: https://github.com/holzschu/a-Shell-commands/releases/download/0.1/ffmpeg_x264.wasm

This new version appears to be able to load a png file, and to produce m4a and mp4 files that are readable by iOS ("play" does show them on screen). I'll have a look into the "multiple files" issue next.

holzschu commented 3 years ago

Hi again, with approach 2, I now have the same error message as approach 5:

unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options

I ran the program step by step, and ffmpeg is opening the files and calling stat and fstat on them. The meaning of the error message is that was unable to find the size of the png files in a limited time. It has no problems finding the size of a single png file, so it seems to be related with how ffmpeg tries to control the time it allows for subtasks. Could you try with specifying the size of the images?

jankais3r commented 3 years ago

Hi, I've added libx264 to the ffmpeg compiled version, if you can give it a try: https://github.com/holzschu/a-Shell-commands/releases/download/0.1/ffmpeg_x264.wasm

Many thanks. I can confirm that I was able to generate an mp4 file playable on the iPad using this new binary 🥳

Hi again, with approach 2, I now have the same error message as approach 5:

unspecified size
Consider increasing the value for the 'analyzeduration' and 'probesize' options

I ran the program step by step, and ffmpeg is opening the files and calling stat and fstat on them. The meaning of the error message is that was unable to find the size of the png files in a limited time. It has no problems finding the size of a single png file, so it seems to be related with how ffmpeg tries to control the time it allows for subtasks. Could you try with specifying the size of the images?

Thanks again for your investigative work on this issue. I was able to generate a movie from series of BMP images using the approach 2 while specifying image size. However, PNGs are still failing. The exact same syntax works with BMPs.

$ ffmpeg -s 500x500 -i frames/frame_%04d.png -c:v libx264 -vf fps=15 out.mp4
ffmpeg version N-97747-gffae62d96c Copyright (c) 2000-2020 the FFmpeg developers
  built with clang version 12.0.0 (https://github.com/llvm/llvm-project e0905553b424afe94938e2cc010fcecd0822eaba)
  configuration: --cc=/Users/holzschu/src/Xcode_iPad/wasi-sdk/opt/bin/clang --ar=/Users/holzschu/src/Xcode_iPad/wasi-sdk/opt/bin/ar --ranlib=/Users/holz
schu/src/Xcode_iPad/wasi-sdk/opt/bin/ranlib --disable-asm --enable-cross-compile --disable-avfoundation --disable-appkit --disable-network --disable-pth
reads --disable-debug --enable-libx264 --enable-gpl
  libavutil      56. 45.100 / 56. 45.100
  libavcodec     58. 84.100 / 58. 84.100
  libavformat    58. 43.100 / 58. 43.100
  libavdevice    58.  9.103 / 58.  9.103
  libavfilter     7. 80.100 /  7. 80.100
  libswscale      5.  6.101 /  5.  6.101
  libswresample   3.  6.100 /  3.  6.100
  libpostproc    55.  6.100 / 55.  6.100
Input #0, image2, from 'frames/frame_%04d.png':
  Duration: 00:00:12.00, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: png, none, 500x500, 25 fps, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (? (?) -> h264 (libx264))
Decoder (codec png) not found for input stream #0:0
holzschu commented 3 years ago

Hi, So the problem was not the time allocated to subtasks, but the fact that most png files are compressed and need zlib for access. There is now a new version of ffmpeg available for download, with zlib added to the list of external libraries. This one can decode png files and create mp4 movies, at least on my iPad.

I've had one failure, when trying to create a m4a movie with 2048x4096 images, but it was a malloc failure...

For simplification, the new ffmpeg binary replaced the old ffmpeg binary. I'm wondering: beyond libx264 and zlib, are there other libraries I should consider?