tanersener / mobile-ffmpeg

FFmpeg for Android, iOS and tvOS. Not maintained anymore. Superseded by FFmpegKit.
https://tanersener.github.io/mobile-ffmpeg
GNU General Public License v3.0
3.87k stars 791 forks source link

Unable to do two pass video compression #252

Closed kliffy closed 5 years ago

kliffy commented 5 years ago

Description Trying to attempt a two pass video compression. Works when run directly through my terminal but can't seem to get it to work on iOS.

I'm trying essentially the same commands on both and it fails on the iOS end giving me the message:

2019-10-15 12:22:16.538970-0700 test[13291:1472769] ratecontrol_init: can't open stats file
2019-10-15 12:22:16.539289-0700 test[13291:1472769] Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

Interesting that it tells me maybe incorrect parameters when doing the same through the terminal works. I'm wondering maybe the ratecontrol_init line has anything to do with it? Perhaps something with the log file output or setting /dev/null for iOS is not correct? Would appreciate any insight on this.

Logs The following logs are just concerning the first pass command.

Heres the log of the successful first pass command through my terminal. On completion of this it also creates the "ffmpeg2pass-0.log.mbtree" log file.

command: ffmpeg -y -i ~/Desktop/18seconds.mp4 -c:v libx264 -b:v 280k -pass 1 -an -f mp4 /dev/null

ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
  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
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/kliffy/Desktop/18seconds.mp4':
  Metadata:
    major_brand     : qt
    minor_version   : 0
    compatible_brands: qt
    creation_time   : 2019-06-24T18:26:39.000000Z
  Duration: 00:00:18.00, start: 0.000000, bitrate: 10350 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 720x1280, 10092 kb/s, 60 fps, 60 tbr, 600 tbn, 1200 tbc (default)
    Metadata:
      creation_time   : 2019-06-24T18:26:39.000000Z
      handler_name    : Core Media Video
      encoder         : H.264
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 248 kb/s (default)
    Metadata:
      creation_time   : 2019-06-24T18:26:39.000000Z
      handler_name    : Core Media Audio
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x7f92d6817c00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x7f92d6817c00] profile Main, level 3.2
[libx264 @ 0x7f92d6817c00] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=1 deblock=1:0:0 analyse=0x1:0 me=dia subme=2 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=6 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=280 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/dev/null':
  Metadata:
    major_brand     : qt
    minor_version   : 0
    compatible_brands: qt
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 720x1280, q=-1--1, 280 kb/s, 60 fps, 15360 tbn, 60 tbc (default)
    Metadata:
      creation_time   : 2019-06-24T18:26:39.000000Z
      handler_name    : Core Media Video
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/280000 buffer size: 0 vbv_delay: -1
frame= 1080 fps=103 q=-1.0 Lsize=     630kB time=00:00:17.95 bitrate= 287.5kbits/s speed=1.72x
video:617kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.171472%
[libx264 @ 0x7f92d6817c00] frame I:6     Avg QP:38.21  size:  7112
[libx264 @ 0x7f92d6817c00] frame P:398   Avg QP:39.55  size:  1244
[libx264 @ 0x7f92d6817c00] frame B:676   Avg QP:38.00  size:   138
[libx264 @ 0x7f92d6817c00] consecutive B-frames:  0.9% 45.9%  2.8% 50.4%
[libx264 @ 0x7f92d6817c00] mb I  I16..4: 91.5%  0.0%  8.5%
[libx264 @ 0x7f92d6817c00] mb P  I16..4:  8.0%  0.0%  0.0%  P16..4:  8.1%  0.0%  0.0%  0.0%  0.0%    skip:83.9%
[libx264 @ 0x7f92d6817c00] mb B  I16..4:  0.3%  0.0%  0.0%  B16..8:  0.8%  0.0%  0.0%  direct: 0.2%  skip:98.8%  L0:40.0% L1:59.8% BI: 0.1%
[libx264 @ 0x7f92d6817c00] final ratefactor: 38.47
[libx264 @ 0x7f92d6817c00] coded y,uvDC,uvAC intra: 2.1% 15.4% 0.0% inter: 0.2% 0.3% 0.0%
[libx264 @ 0x7f92d6817c00] i16 v,h,dc,p: 42% 30% 17% 11%
[libx264 @ 0x7f92d6817c00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 15% 28%  9%  8%  6%  6%  6%  4%
[libx264 @ 0x7f92d6817c00] i8c dc,h,v,p: 91%  5%  4%  0%
[libx264 @ 0x7f92d6817c00] Weighted P-Frames: Y:7.3% UV:0.3%
[libx264 @ 0x7f92d6817c00] kb/s:280.28

Log of failing compression on iOS. This log is from using my simulator. Running on the iphone itself produces the same error but I noticed "using cpu capabilities" is different between the two and perhaps others are different, let me know if you want those logs:

command: -y -i /Users/kliffy/Library/Developer/CoreSimulator/Devices/7556C903-E57B-48B1-B238-8F54DA02B5E3/data/Containers/Bundle/Application/A091165E-9D33-4BD4-98C8-6A9138A2674F/test.app/18seconds.mp4 -c:v libx264 -b:v 280k -pass 1 -an -f mp4 /dev/null

2019-10-15 12:45:29.547985-0700 test[14048:1699352] ffmpeg version v4.2-dev-1824
2019-10-15 12:45:29.548142-0700 test[14048:1699352]  Copyright (c) 2000-2019 the FFmpeg developers
2019-10-15 12:45:29.548255-0700 test[14048:1699352] 
2019-10-15 12:45:29.548364-0700 test[14048:1699352]   built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
2019-10-15 12:45:29.548476-0700 test[14048:1699352]   configuration: --sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk --prefix=/Users/taner/Projects/mobile-ffmpeg/prebuilt/ios-x86_64-apple-darwin/ffmpeg --enable-version3 --arch=x86_64 --cpu=x86_64 --target-os=darwin --ar=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar --cc=clang --cxx=clang++ --as='clang -arch x86_64 -target x86_64-ios-darwin -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel -DMOBILE_FFMPEG_X86_64 -Wno-unused-function -Wno-deprecated-declarations -fstrict-aliasing -DIOS -DMOBILE_FFMPEG_BUILD_DATE=20190703 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk -O2 -mios-simulator-version-min=12.1 -I/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.2.sdk/usr/include' --ranlib=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib --strip=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip --disable-neon --enable-cross-compile --enable-pic --enable-asm --enable-inline-asm --enable-optimizations --enable-swscale --enable-static --disable-shared --enable-small --disable-v4l2-m2m --disable-outdev=v4l2 --disable-outdev=fbdev --disable-indev=v4l2 --disable-indev=fbdev --disable-openssl --disable-xmm-clobber-test --disable-debug --disable-neon-clobber-test --disable-programs --disable-postproc --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-sndio --disable-schannel --disable-securetransport --disable-xlib --disable-cuda --disable-cuvid --disable-nvenc --disable-vaapi --disable-vdpau --disable-appkit --disable-alsa --disable-cuda --disable-cuvid --disable-nvenc --disable-vaapi --disable-vdpau --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gmp --enable-gnutls --enable-libmp3lame --enable-libass --enable-iconv --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libxml2 --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libshine --enable-libspeex --enable-libwavpack --enable-libkvazaar --enable-libx264 --enable-gpl --enable-libxvid --enable-gpl --enable-libx265 --enable-gpl --enable-libvidstab --enable-gpl --enable-libilbc --enable-libopus --enable-libsnappy --enable-libsoxr --enable-libaom --enable-libtwolame --disable-sdl2 --enable-zlib --enable-audiotoolbox --enable-coreimage --enable-bzlib --enable-videotoolbox --enable-avfoundation
2019-10-15 12:45:29.551114-0700 test[14048:1699352]   libavutil      56. 30.100 / 56. 30.100
2019-10-15 12:45:29.551262-0700 test[14048:1699352]   libavcodec     58. 53.101 / 58. 53.101
2019-10-15 12:45:29.551400-0700 test[14048:1699352]   libavformat    58. 28.101 / 58. 28.101
2019-10-15 12:45:29.551512-0700 test[14048:1699352]   libavdevice    58.  7.100 / 58.  7.100
2019-10-15 12:45:29.551605-0700 test[14048:1699352]   libavfilter     7. 55.100 /  7. 55.100
2019-10-15 12:45:29.551693-0700 test[14048:1699352]   libswscale      5.  4.101 /  5.  4.101
2019-10-15 12:45:29.551782-0700 test[14048:1699352]   libswresample   3.  4.100 /  3.  4.100
2019-10-15 12:45:29.551893-0700 test[14048:1699352] Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/kliffy/Library/Developer/CoreSimulator/Devices/7556C903-E57B-48B1-B238-8F54DA02B5E3/data/Containers/Bundle/Application/A091165E-9D33-4BD4-98C8-6A9138A2674F/test.app/18seconds.mp4':
2019-10-15 12:45:29.552109-0700 test[14048:1699352]   Metadata:
2019-10-15 12:45:29.552346-0700 test[14048:1699352]     major_brand     :
2019-10-15 12:45:29.552643-0700 test[14048:1699352] qt
2019-10-15 12:45:29.552905-0700 test[14048:1699352] 
2019-10-15 12:45:29.553181-0700 test[14048:1699352]     minor_version   :
2019-10-15 12:45:29.553435-0700 test[14048:1699352] 0
2019-10-15 12:45:29.553719-0700 test[14048:1699352] 
2019-10-15 12:45:29.554015-0700 test[14048:1699352]     compatible_brands:
2019-10-15 12:45:29.554327-0700 test[14048:1699352] qt
2019-10-15 12:45:29.554559-0700 test[14048:1699352] 
2019-10-15 12:45:29.554804-0700 test[14048:1699352]     creation_time   :
2019-10-15 12:45:29.555100-0700 test[14048:1699352] 2019-06-24T18:26:39.000000Z
2019-10-15 12:45:29.555357-0700 test[14048:1699352] 
2019-10-15 12:45:29.555588-0700 test[14048:1699352]   Duration:
2019-10-15 12:45:29.555818-0700 test[14048:1699352] 00:00:18.00
2019-10-15 12:45:29.556402-0700 test[14048:1699352] , start:
2019-10-15 12:45:29.556587-0700 test[14048:1699352] 0.000000
2019-10-15 12:45:29.556926-0700 test[14048:1699352] , bitrate:
2019-10-15 12:45:29.557275-0700 test[14048:1699352] 10350 kb/s
2019-10-15 12:45:29.557501-0700 test[14048:1699352] 
2019-10-15 12:45:29.557760-0700 test[14048:1699352]     Stream #0:0
2019-10-15 12:45:29.558134-0700 test[14048:1699352] (und)
2019-10-15 12:45:29.558452-0700 test[14048:1699352] : Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709), 720x1280, 10092 kb/s
2019-10-15 12:45:29.558734-0700 test[14048:1699352] ,
2019-10-15 12:45:29.559012-0700 test[14048:1699352] 60 fps,
2019-10-15 12:45:29.559292-0700 test[14048:1699352] 60 tbr,
2019-10-15 12:45:29.559585-0700 test[14048:1699352] 600 tbn,
2019-10-15 12:45:29.559836-0700 test[14048:1699352] 1200 tbc
2019-10-15 12:45:29.560079-0700 test[14048:1699352]  (default)
2019-10-15 12:45:29.560349-0700 test[14048:1699352] 
2019-10-15 12:45:29.560625-0700 test[14048:1699352]     Metadata:
2019-10-15 12:45:29.560898-0700 test[14048:1699352]       creation_time   :
2019-10-15 12:45:29.561252-0700 test[14048:1699352] 2019-06-24T18:26:39.000000Z
2019-10-15 12:45:29.561567-0700 test[14048:1699352] 
2019-10-15 12:45:29.561817-0700 test[14048:1699352]       handler_name    :
2019-10-15 12:45:29.562085-0700 test[14048:1699352] Core Media Video
2019-10-15 12:45:29.562351-0700 test[14048:1699352] 
2019-10-15 12:45:29.562610-0700 test[14048:1699352]       encoder         :
2019-10-15 12:45:29.562891-0700 test[14048:1699352] H.264
2019-10-15 12:45:29.563199-0700 test[14048:1699352] 
2019-10-15 12:45:29.563463-0700 test[14048:1699352]     Stream #0:1
2019-10-15 12:45:29.563756-0700 test[14048:1699352] (und)
2019-10-15 12:45:29.563919-0700 test[14048:1699352] : Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 248 kb/s
2019-10-15 12:45:29.564200-0700 test[14048:1699352]  (default)
2019-10-15 12:45:29.564426-0700 test[14048:1699352] 
2019-10-15 12:45:29.564650-0700 test[14048:1699352]     Metadata:
2019-10-15 12:45:29.564903-0700 test[14048:1699352]       creation_time   :
2019-10-15 12:45:29.565176-0700 test[14048:1699352] 2019-06-24T18:26:39.000000Z
2019-10-15 12:45:29.565511-0700 test[14048:1699352] 
2019-10-15 12:45:29.565767-0700 test[14048:1699352]       handler_name    :
2019-10-15 12:45:29.566026-0700 test[14048:1699352] Core Media Audio
2019-10-15 12:45:29.566300-0700 test[14048:1699352] 
2019-10-15 12:45:29.569475-0700 test[14048:1699352] Stream mapping:
2019-10-15 12:45:29.569627-0700 test[14048:1699352]   Stream #0:0 -> #0:0
2019-10-15 12:45:29.569735-0700 test[14048:1699352]  (h264 (native) -> h264 (libx264))
2019-10-15 12:45:29.569837-0700 test[14048:1699352] 
2019-10-15 12:45:29.569928-0700 test[14048:1699352] Press [q] to stop, [?] for help
2019-10-15 12:45:29.570019-0700 test[14048:1699352] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
2019-10-15 12:45:29.570181-0700 test[14048:1699352] ratecontrol_init: can't open stats file
2019-10-15 12:45:29.570507-0700 test[14048:1699352] Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
2019-10-15 12:45:29.570755-0700 test[14048:1699352] Conversion failed!

Environment

tanersener commented 5 years ago

Can you use -passlogfile option to specify the path of video statistics file?

kliffy commented 5 years ago

... It works! I tried using passlogfile previously but must have done something wrong. Anyway I specified a path in the documents directory for the passlogfile for both passes and that did the trick. Thanks a bunch, again.

Here's what the commands came out to look like:

ffmpeg -y -i /private/var/containers/Bundle/Application/5E0A94FB-FC39-46CD-B9A5-D95B0294050E/test.app/18seconds.mp4 -c:v libx264 -b:v 280k -pass 1 -an -f mp4 -passlogfile /var/mobile/Containers/Data/Application/EB4DFC1F-4A58-404A-80E3-69C4EFE2F245/Documents/somelogfile /dev/null

ffmpeg -i /private/var/containers/Bundle/Application/5E0A94FB-FC39-46CD-B9A5-D95B0294050E/test.app/18seconds.mp4 -c:v libx264 -b:v 280k -pass 2 -c:a aac -b:a 128k -passlogfile /var/mobile/Containers/Data/Application/EB4DFC1F-4A58-404A-80E3-69C4EFE2F245/Documents/somelogfile /var/mobile/Containers/Data/Application/EB4DFC1F-4A58-404A-80E3-69C4EFE2F245/Documents/video343.mp4