Recherche avancée

Médias (0)

Mot : - Tags -/formulaire

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (43)

  • Multilang : améliorer l’interface pour les blocs multilingues

    18 février 2011, par

    Multilang est un plugin supplémentaire qui n’est pas activé par défaut lors de l’initialisation de MediaSPIP.
    Après son activation, une préconfiguration est mise en place automatiquement par MediaSPIP init permettant à la nouvelle fonctionnalité d’être automatiquement opérationnelle. Il n’est donc pas obligatoire de passer par une étape de configuration pour cela.

  • Encoding and processing into web-friendly formats

    13 avril 2011, par

    MediaSPIP automatically converts uploaded files to internet-compatible formats.
    Video files are encoded in MP4, Ogv and WebM (supported by HTML5) and MP4 (supported by Flash).
    Audio files are encoded in MP3 and Ogg (supported by HTML5) and MP3 (supported by Flash).
    Where possible, text is analyzed in order to retrieve the data needed for search engine detection, and then exported as a series of image files.
    All uploaded files are stored online in their original format, so you can (...)

  • Contribute to translation

    13 avril 2011

    You can help us to improve the language used in the software interface to make MediaSPIP more accessible and user-friendly. You can also translate the interface into any language that allows it to spread to new linguistic communities.
    To do this, we use the translation interface of SPIP where the all the language modules of MediaSPIP are available. Just subscribe to the mailing list and request further informantion on translation.
    MediaSPIP is currently available in French and English (...)

Sur d’autres sites (2668)

  • avconv on debian jessie throws error when trying to convert raw video to mp4

    20 août 2017, par Vinay Divakar

    I am trying to convert a raw video captured using a Logitech USB Cam(YUYV) to mp4 using avconv. I have tried the below to do so :

    avconv -i out.raw -c copy output.mp4
    avconv -i out.raw -vcodec copy -acodec copy output.mp4

    but I get an error as shown below.

    root@beaglebone:~/BBB_CV/boneCV# avconv -i out.raw -c copy output.mp4
    avconv version 11.9-6:11.9-1~deb8u1, Copyright (c) 2000-2017 the Libav developersbuilt on Apr 23 2017 23:00:54 with gcc 4.9.2 (Debian 4.9.2-10)
    out.raw: Invalid data found when processing input

    I have checked here, but did not find any relevant examples for the above purpose.

    Edit : Added more details—

    I tried doing the below by adding the infile options as suggested :

    root@beaglebone:~/BBB_CV/boneCV# avconv -s 640x480 -framerate 30 -pix_fmt yuyv422 -f rawvideo -i out.raw -c copy output.mp4

    This got me rid of the previous error and some new ones popped up as shown below.

    root@beaglebone:~/BBB_CV/boneCV# avconv -s 640x480 -framerate 30 -pix_fmt yuyv422 -f rawvideo -i out.raw -c copy output.mp4
    avconv version 11.9-6:11.9-1~deb8u1, Copyright (c) 2000-2017 the Libav
    developers built on Apr 23 2017 23:00:54 with gcc 4.9.2 (Debian 4.9.2-10)
    [rawvideo @ 0x17e21c0] Estimating duration from bitrate, this may be inaccurate
    Input #0, rawvideo, from 'out.raw':
    Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: rawvideo, yuyv422, 640x480, 30 tbn
    [mp4 @ 0x17eb840] Codec for stream 0 does not use global headers but
    container format requires global headers
    [mp4 @ 0x17eb840] track 0: could not find tag, codec not currently
    supported in container
    Output #0, mp4, to 'output.mp4':
    Metadata:
    encoder         : Lavf56.1.0
    Stream #0.0: Video: rawvideo, yuyv422, 640x480, q=2-31, 30 tbn, 30 tbc
    Stream mapping:
    Stream #0:0 -> #0:0 (copy)
    Could not write header for output file #0 (incorrect codec parameters ?):
    Operation not permitted

    The output.mp4 is being generated but when tried to open it throws an error.

    Edit : I ran a "sudo apt-get install libav-tools" an then tried. This is what I got.

    root@beaglebone:~/BBB_CV/boneCV# avconv -s 640x480 -framerate 30 -pix_fmt
    yuyv422 -f rawvideo -i out.raw -c copy output.mp4
    ffmpeg version 2.6.9 Copyright (c) 2000-2016 the FFmpeg developers
    built with gcc 4.9.2 (Debian 4.9.2-10)
    configuration: --prefix=/usr --extra-cflags='-g -O2 -fstack-protector-
    strong -Wformat -Werror=format-security ' --extra-ldflags='-Wl,-z,relro'
      -cc='ccache cc' --enable-shared --enable-libmp3lame --enable-gpl --enable-
       nonfree --enable-libvorbis --enable-pthreads --enable-libfaac --enable-
       libxvid --enable-postproc --enable-x11grab --enable-libgsm --enable-
       libtheora --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-
       libx264 --enable-libspeex --enable-nonfree --disable-stripping --enable-
       libvpx --enable-libschroedinger --disable-encoder=libschroedinger --
       enable-version3 --enable-libopenjpeg --enable-librtmp --enable-avfilter -
       enable-libfreetype --enable-libvo-aacenc --disable-decoder=amrnb --enable-
      libvo-amrwbenc --enable-libaacplus --libdir=/usr/lib/arm-linux-gnueabihf -
      -disable-vda --enable-libbluray --enable-libcdio --enable-gnutls --enable-
      frei0r --enable-openssl --enable-libass --enable-libopus --enable-
      fontconfig --enable-libpulse --disable-mips32r2 --disable-mipsdspr1 --
      disable-mipsdspr2 --enable-libvidstab --enable-libzvbi --enable-avresample
      --disable-htmlpages --disable-podpages --enable-libutvideo --enable-
      libfdk-aac --enable-libx265 --enable-libiec61883 --enable-libdc1394 --
      disable-altivec --shlibdir=/usr/lib/arm-linux-gnueabihf
      libavutil      54. 20.100 / 54. 20.100
      libavcodec     56. 26.100 / 56. 26.100
      libavformat    56. 25.101 / 56. 25.101
      libavdevice    56.  4.100 / 56.  4.100
      libavfilter     5. 11.102 /  5. 11.102
      libavresample   2.  1.  0 /  2.  1.  0
      libswscale      3.  1.101 /  3.  1.101
      libswresample   1.  1.100 /  1.  1.100
      libpostproc    53.  3.100 / 53.  3.100
     [rawvideo @ 0x18465e0] Estimating duration from bitrate, this may be
      inaccurate
      Input #0, rawvideo, from 'out.raw':
      Duration: 00:00:00.10, start: 0.000000, bitrate: 146626 kb/s
      Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, 147456
      kb/s, 30 tbr, 30 tbn, 30 tbc
      [mp4 @ 0x184f790] Could not find tag for codec rawvideo in stream #0,
      codec not currently supported in container
      Output #0, mp4, to 'output.mp4':
      Metadata:
      encoder         : Lavf56.25.101
      Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, q=2-
      31, 147456 kb/s, 30 tbr, 30 tbn, 30 tbc
      Stream mapping:
      Stream #0:0 -> #0:0 (copy)
      Could not write header for output file #0 (incorrect codec parameters ?):
      Invalid argument

    I ran "ffmpeg -s 640x480 -framerate 30 -pix_fmt yuv420p -f rawvideo -i out.raw output.mp4" and below is what I got.

    root@beaglebone:~/BBB_CV/boneCV# ffmpeg -s 640x480 -framerate 30 -pix_fmt yuv420p -f rawvideo -i out.raw output.mp4
    ffmpeg version 2.6.9 Copyright (c) 2000-2016 the FFmpeg developers
     built with gcc 4.9.2 (Debian 4.9.2-10)
     configuration: --prefix=/usr --extra-cflags='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security ' --extra-ldflags='-Wl,-z,relro' --cc='ccache cc' --enable-shared --enable-libmp3lame --enable-gpl --enable-nonfree --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-x11grab --enable-libgsm --enable-libtheora --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libspeex --enable-nonfree --disable-stripping --enable-libvpx --enable-libschroedinger --disable-encoder=libschroedinger --enable-version3 --enable-libopenjpeg --enable-librtmp --enable-avfilter --enable-libfreetype --enable-libvo-aacenc --disable-decoder=amrnb --enable-libvo-amrwbenc --enable-libaacplus --libdir=/usr/lib/arm-linux-gnueabihf --disable-vda --enable-libbluray --enable-libcdio --enable-gnutls --enable-frei0r --enable-openssl --enable-libass --enable-libopus --enable-fontconfig --enable-libpulse --disable-mips32r2 --disable-mipsdspr1 --disable-mipsdspr2 --enable-libvidstab --enable-libzvbi --enable-avresample --disable-htmlpages --disable-podpages --enable-libutvideo --enable-libfdk-aac --enable-libx265 --enable-libiec61883 --enable-libdc1394 --disable-altivec --shlibdir=/usr/lib/arm-linux-gnueabihf
     libavutil      54. 20.100 / 54. 20.100
     libavcodec     56. 26.100 / 56. 26.100
     libavformat    56. 25.101 / 56. 25.101
     libavdevice    56.  4.100 / 56.  4.100
     libavfilter     5. 11.102 /  5. 11.102
     libavresample   2.  1.  0 /  2.  1.  0
     libswscale      3.  1.101 /  3.  1.101
     libswresample   1.  1.100 /  1.  1.100
     libpostproc    53.  3.100 / 53.  3.100
    [rawvideo @ 0x1d55e0] Estimating duration from bitrate, this may be inaccurate
    Input #0, rawvideo, from 'out.raw':
     Duration: 00:00:00.13, start: 0.000000, bitrate: 109969 kb/s
       Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 640x480, 110592 kb/s, 30 tbr, 30 tbn, 30 tbc
    [libx264 @ 0x1df6c0] using cpu capabilities: ARMv6 NEON
    [libx264 @ 0x1df6c0] profile High, level 3.0
    [libx264 @ 0x1df6c0] 264 - core 146 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 lookahead_threads=1 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=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
    Output #0, mp4, to 'output.mp4':
     Metadata:
       encoder         : Lavf56.25.101
       Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 640x480, q=-1--1, 30 fps, 15360 tbn, 30 tbc
       Metadata:
         encoder         : Lavc56.26.100 libx264
    Stream mapping:
     Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
    Press [q] to stop, [?] for help
    [rawvideo @ 0x1de3a0] Invalid buffer size, packet size 450428 < expected frame_size 460800
    Error while decoding stream #0:0: Invalid argument
    frame=    3 fps=1.7 q=29.0 Lsize=     263kB time=00:00:00.03 bitrate=64692.6kbits/s    
    video:262kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.318205%
    [libx264 @ 0x1df6c0] frame I:1     Avg QP:33.09  size:100676
    [libx264 @ 0x1df6c0] frame P:2     Avg QP:32.99  size: 83672
    [libx264 @ 0x1df6c0] mb I  I16..4:  0.0% 95.4%  4.6%
    [libx264 @ 0x1df6c0] mb P  I16..4:  0.0% 45.7%  3.0%  P16..4: 35.9% 10.7%  4.8%  0.0%  0.0%    skip: 0.0%
    [libx264 @ 0x1df6c0] 8x8 transform intra:94.6% inter:86.1%
    [libx264 @ 0x1df6c0] coded y,uvDC,uvAC intra: 100.0% 100.0% 100.0% inter: 53.6% 100.0% 100.0%
    [libx264 @ 0x1df6c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 56% 12% 24%  2%  0%  1%  1%  2%  3%
    [libx264 @ 0x1df6c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 82% 11%  4%  0%  0%  0%  1%  0%  1%
    [libx264 @ 0x1df6c0] i8c dc,h,v,p: 48% 30% 14%  7%
    [libx264 @ 0x1df6c0] Weighted P-Frames: Y:0.0% UV:0.0%
    [libx264 @ 0x1df6c0] ref P L0: 83.5%  7.9%  8.6%
    [libx264 @ 0x1df6c0] kb/s:21441.60
  • FFmpeg - Concatenating videos with different resolutions

    7 août 2017, par Jill

    I currently have an android app where the videos get combined using ffmpeg’s concat. That works fine. However, when I add text (using drawtext) to one of the videos, after combining the videos again, the combined video gets distorted. The video with the drawtext shows up fine, but then the videos without text switches orientation.

    I think the problem roots from the fact that a vertical video on my android phone has the resolution of 1920x1080, however if I use that for the scale when executing drawtext, it becomes horizontal.

    I think an example might help explain this better. Let’s say I have 2 vertically recorded videos, and I combine them with concat. The combined video shows up fine. Then if I add text to the 1st video using drawtext, the combined video will then show the first video (with text) at normal orientation (vertical). However, when it transitions to the next video, it first stalls for a couple seconds and then the 2nd video is shown at a horizontal orientation.

    Also, I tried this also with having the aspect ratio as 1:1 so it wouldn’t matter, but the second video’s orientation still changed counter clockwise.

    Thanks for the help, and happy to add more clarification if needed.

    This is for adding the text :

    ffmpeg -i input.mp4 -vf drawbox=y=ih-200:color=black@0.5:width=iw:height=200:t=max
       ,drawtext=fontfile=RobotoRegular.ttf:text='Text':x=(w-text_w)/2:y=(h-text_h)-100:fontsize=70:fontcolor=white
       -c:v libx264 -b:v 17000k -r 30 -preset ultrafast -y output.mp4

    Output :

    ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
                                                          built with gcc 4.8 (GCC)
                                                          configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
                                                          libavutil      55. 17.103 / 55. 17.103
                                                          libavcodec     57. 24.102 / 57. 24.102
                                                          libavformat    57. 25.100 / 57. 25.100
                                                          libavdevice    57.  0.101 / 57.  0.101
                                                          libavfilter     6. 31.100 /  6. 31.100
                                                          libswscale      4.  0.100 /  4.  0.100
                                                          libswresample   2.  0.101 /  2.  0.101
                                                          libpostproc    54.  0.100 / 54.  0.100
                                                        Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/Android/data/com.me.app/files/65/VID_20170802_111518.mp4':
                                                          Metadata:
                                                            major_brand     : mp42
                                                            minor_version   : 0
                                                            compatible_brands: isommp42
                                                            creation_time   : 2017-08-02 18:15:20
                                                            com.android.version: 7.0
                                                          Duration: 00:00:00.93, start: 0.000000, bitrate: 16415 kb/s
                                                            Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 16408 kb/s, SAR 1:1 DAR 16:9, 28.94 fps, 30 tbr, 90k tbn, 180k tbc (default)
                                                            Metadata:
                                                              rotate          : 90
                                                              creation_time   : 2017-08-02 18:15:20
                                                              handler_name    : VideoHandle
                                                            Side data:
                                                              displaymatrix: rotation of -90.00 degrees
                                                        [libx264 @ 0xeab7ec00] using SAR=1/1
                                                        [libx264 @ 0xeab7ec00] using cpu capabilities: none!
                                                        [libx264 @ 0xeab7ec00] profile Constrained Baseline, level 4.1
                                                        [libx264 @ 0xeab7ec00] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 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=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=abr mbtree=0 bitrate=17000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
                                                        Output #0, mp4, to '/storage/emulated/0/Android/data/com.me.app/files/65/temp.mp4':
                                                          Metadata:
                                                            major_brand     : mp42
                                                            minor_version   : 0
                                                            compatible_brands: isommp42
                                                            com.android.version: 7.0
                                                            encoder         : Lavf57.25.100
                                                            Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], q=-1--1, 17000 kb/s, 30 fps, 15360 tbn, 30 tbc (default)
                                                            Metadata:
                                                              handler_name    : VideoHandle
                                                              creation_time   : 2017-08-02 18:15:20
                                                              encoder         : Lavc57.24.102 libx264
                                                            Side data:
                                                              unknown side data type 10 (24 bytes)
                                                        Stream mapping:
                                                          Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
                                                        Press [q] to stop, [?] for help
                                                        frame=    6 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=1 drop=0 speed=   0x    
                                                        frame=   10 fps=8.8 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=1 drop=0 speed=   0x    
                                                        frame=   14 fps=8.4 q=15.0 size=     155kB time=00:00:00.03 bitrate=38067.5kbits/s dup=1 drop=0 speed=0.0201x    
                                                        frame=   18 fps=8.0 q=22.0 size=     744kB time=00:00:00.16 bitrate=36592.5kb

    ffmpeg -f concat -safe 0 -i input.mp4 -c copy -y output.mp4

    Output :

    ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
                                                           built with gcc 4.8 (GCC)
                                                           configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
                                                           libavutil      55. 17.103 / 55. 17.103
                                                           libavcodec     57. 24.102 / 57. 24.102
                                                           libavformat    57. 25.100 / 57. 25.100
                                                           libavdevice    57.  0.101 / 57.  0.101
                                                           libavfilter     6. 31.100 /  6. 31.100
                                                           libswscale      4.  0.100 /  4.  0.100
                                                           libswresample   2.  0.101 /  2.  0.101
                                                           libpostproc    54.  0.100 / 54.  0.100
                                                         [mov,mp4,m4a,3gp,3g2,mj2 @ 0xe762a600] Auto-inserting h264_mp4toannexb bitstream filter
                                                         Input #0, concat, from '/storage/emulated/0/Android/data/com.me.app/files/64/TXT_20170802_110325.txt':
                                                           Duration: N/A, start: 0.000000, bitrate: 23403 kb/s
                                                             Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], 23403 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc
                                                             Metadata:
                                                               handler_name    : VideoHandler
                                                         Output #0, mp4, to '/storage/emulated/0/Android/data/com.me.app/files/64/VID_20170802_110325concat.mp4':
                                                           Metadata:
                                                             encoder         : Lavf57.25.100
                                                             Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], q=2-31, 23403 kb/s, 30 fps, 30 tbr, 15360 tbn, 15360 tbc
                                                             Metadata:
                                                               handler_name    : VideoHandler
                                                         Stream mapping:
                                                           Stream #0:0 -> #0:0 (copy)
                                                         Press [q] to stop, [?] for help
                                                         [mov,mp4,m4a,3gp,3g2,mj2 @ 0xe762a600] Auto-inserting h264_mp4toannexb bitstream filter
                                                         frame=   64 fps=0.0 q=-1.0 Lsize=    5218kB time=00:00:12.30 bitrate=3474.5kbits/s speed= 122x    
                                                         video:5217kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.025721%
  • ffmpeg/libx264 C API : frames dropped from end of short MP4

    19 juillet 2017, par Blake McConnell

    In my C++ application, I am taking a series of JPEG images, manipulating their data using FreeImage, and then encoding the bitmaps as H264 using the ffmpeg/libx264 C API. The output is an MP4 which shows the series of 22 images at 12fps. My code is adapted from the "muxing" example that comes with ffmpeg C source code.

    My problem : no matter how I tune the codec parameters, a certain number of frames at the end of the sequence which are passed to the encoder do not appear in the final output. I’ve set the AVCodecContext parameters like this :

    //set context params
    ctx->codec_id = AV_CODEC_ID_H264;
    ctx->bit_rate = 4000 * 1000;
    ctx->width = _width;
    ctx->height = _height;
    ost->st->time_base = AVRational{ 1, 12 };
    ctx->time_base = ost->st->time_base;
    ctx->gop_size = 1;
    ctx->pix_fmt = AV_PIX_FMT_YUV420P;

    I have found that the higher the gop_size the more frames are dropped from the end of the video. I can also see from the output that, with this gop size (where I’m essentially directing that all output frames be I frames) that only 9 frames are written.

    I’m not sure why this is occurring. I experimented with encoding duplicate frames and making a much longer video. This resulted in no frames being dropped. I know with the ffmpeg command line tool there is a concatenation command that accomplishes what I am trying to do, but I’m not sure how to accomplish the same goal using the C API.

    Here’s the output I’m getting from the console :

    [libx264 @ 026d81c0] using cpu capabilities : MMX2 SSE2Fast SSSE3
    SSE4.2 AVX FMA3 BMI2 AVX2 [libx264 @ 026d81c0] profile High, level
    3.1 [libx264 @ 026d81c0] 264 - core 152 r2851 ba24899 - H.264/MPEG-4 AVC codec - Cop yleft 2003-2017 - http://www.videolan.org/x264.html -
    options : cabac=1 ref=1 deb lock=1:0:0 analyse=0x3:0x113 me=hex subme=7
    psy=1 psy_rd=1.00:0.00 mixed_ref=0 m e_range=16 chroma_me=1 trellis=1
    8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chro ma_qp_offset=-2
    threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1
    interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0
    keyint=1 ke yint_min=1 scenecut=40 intra_refresh=0 rc=abr mbtree=0
    bitrate=4000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4
    ip_ratio=1.40 aq=1:1.00 Output #0, mp4, to
    ’....\images\c411a991-46f6-400c-8bb0-77af3738559a.mp4’ :
    Stream #0:0 : Video : h264, yuv420p, 700x700, q=2-31, 4000 kb/s, 12 tbn

    [libx264 @ 026d81c0] frame I:9 Avg QP:17.83 size:111058 [libx264
    @ 026d81c0] mb I I16..4 : 1.9% 47.7% 50.5% [libx264 @ 026d81c0] final
    ratefactor : 19.14 [libx264 @ 026d81c0] 8x8 transform intra:47.7%
    [libx264 @ 026d81c0] coded y,uvDC,uvAC intra : 98.4% 96.9% 89.5%
    [libx264 @ 026d81c0] i16 v,h,dc,p : 64% 6% 2% 28% [libx264 @
    026d81c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu : 32% 15% 9% 5% 5% 6% 8%
    10% 10% [libx264 @ 026d81c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu : 28% 18%
    7% 6% 8% 8% 8% 9% 8% [libx264 @ 026d81c0] i8c dc,h,v,p : 43% 22%
    25% 10% [libx264 @ 026d81c0] kb/s:10661.53

    Code included below :

    MP4Writer.h

    #ifndef MPEG_WRITER
    #define MPEG_WRITER

    #include <iostream>
    #include <string>
    #include <vector>
    #include
    extern "C" {
       #include <libavformat></libavformat>avformat.h>
       #include <libswscale></libswscale>swscale.h>
       #include <libswresample></libswresample>swresample.h>
       #include <libswscale></libswscale>swscale.h>
    }

    typedef struct OutputStream
    {
       AVStream *st;
       AVCodecContext *enc;

       //pts of the next frame that will be generated
       int64_t next_pts;
       int samples_count;

       AVFrame *frame;
       AVFrame *tmp_frame;

       float t, tincr, tincr2;

       struct SwsContext *sws_ctx;
       struct SwrContext *swr_ctx;
    };

    class MP4Writer {
       public:
           MP4Writer();
           void Init();
           int16_t SetOutput( const std::string &amp; path );
           int16_t AddFrame( uint8_t * imgData );
           int16_t Write( std::vector<imgdata> &amp; imgData );
           int16_t Finalize();
           void SetHeight( const int height ) { _height = _width = height; } //assuming 1:1 aspect ratio

       private:
           int16_t AddStream( OutputStream * ost, AVFormatContext * formatCtx, AVCodec ** codec, enum AVCodecID codecId );
           int16_t OpenVideo( AVFormatContext * formatCtx, AVCodec *codec, OutputStream * ost, AVDictionary * optArg );
           static AVFrame * AllocPicture( enum AVPixelFormat pixFmt, int width, int height );
           static AVFrame * GetVideoFrame( uint8_t * imgData, OutputStream * ost, const int width, const int height );
           static int WriteFrame( AVFormatContext * formatCtx, const AVRational * timeBase, AVStream * stream, AVPacket * packet );

           int _width;
           int _height;
           OutputStream _ost;
           AVFormatContext * _formatCtx;
           AVDictionary * _dict;
    };

    #endif //MPEG_WRITER
    </imgdata></vector></string></iostream>

    MP4Writer.cpp

    #include
    #include <algorithm>

    MP4Writer::MP4Writer()
    {
       _width = 0;
       _height = 0;
    }

    void MP4Writer::Init()
    {
       av_register_all();
    }

    /**
    sets up output stream for the specified path.
    note that the output format is deduced automatically from the file extension passed
    @param path: output file path
    @returns: -1 = output could not be deduced, -2 = invalid codec, -3 = error opening output file,
              -4 = error writing header
    */
    int16_t MP4Writer::SetOutput( const std::string &amp; path )
    {
       int error;
       AVCodec * codec;
       AVOutputFormat * format;

       _ost = OutputStream{}; //TODO reset state in a more focused way?

       //allocate output media context
       avformat_alloc_output_context2( &amp;_formatCtx, NULL, NULL, path.c_str() );
       if ( !_formatCtx ) {
           std::cout &lt;&lt; "could not deduce output format from file extension.  aborting" &lt;&lt; std::endl;
           return -1;
       }
       //set format
       format = _formatCtx->oformat;
       if ( format->video_codec != AV_CODEC_ID_NONE ) {
           AddStream( &amp;_ost, _formatCtx, &amp;codec, format->video_codec );
       }
       else {
           std::cout &lt;&lt; "there is no video codec set.  aborting" &lt;&lt; std::endl;
           return -2;
       }

       OpenVideo( _formatCtx, codec, &amp;_ost, _dict );

       av_dump_format( _formatCtx, 0, path.c_str(), 1 );

       //open output file
       if ( !( format->flags &amp; AVFMT_NOFILE )) {
           error = avio_open( &amp;_formatCtx->pb, path.c_str(), AVIO_FLAG_WRITE );
           if ( error &lt; 0 ) {
               std::cout &lt;&lt; "there was an error opening output file " &lt;&lt; path &lt;&lt; ".  aborting" &lt;&lt; std::endl;
               return -3;
           }
       }

       //write header
       error = avformat_write_header( _formatCtx, &amp;_dict );
       if ( error &lt; 0 ) {
           std::cout &lt;&lt; "an error occurred writing header. aborting" &lt;&lt; std::endl;
           return -4;
       }

       return 0;
    }

    /**
    initialize the output stream
    @param ost: the output stream
    @param formatCtx: the context format
    @param codec: the output codec
    @param codec: the ffmpeg enumerated id of the codec
    @returns: -1 = encoder not found, -2 = stream could not be allocated, -3 = encoding context could not be allocated
    */
    int16_t MP4Writer::AddStream( OutputStream * ost, AVFormatContext * formatCtx, AVCodec ** codec, enum AVCodecID codecId )
    {
       AVCodecContext * ctx; //TODO not sure why this is here, could just set ost->enc directly
       int i;

       //detect the encoder
       *codec = avcodec_find_encoder( codecId );
       if ( (*codec) == NULL ) {
           std::cout &lt;&lt; "could not find encoder.  aborting" &lt;&lt; std::endl;
           return -1;
       }

       //allocate stream
       ost->st = avformat_new_stream( formatCtx, NULL );
       if ( ost->st == NULL ) {
           std::cout &lt;&lt; "could not allocate stream.  aborting" &lt;&lt; std::endl;
           return -2;
       }

       //allocate encoding context
       ost->st->id = formatCtx->nb_streams - 1;
       ctx = avcodec_alloc_context3( *codec );
       if ( ctx == NULL ) {
           std::cout &lt;&lt; "could not allocate encoding context.  aborting" &lt;&lt; std::endl;
           return -3;
       }

       ost->enc = ctx;

       //set context params
       ctx->codec_id = AV_CODEC_ID_H264;
       ctx->bit_rate = 4000 * 1000;
       ctx->width = _width;
       ctx->height = _height;
       ost->st->time_base = AVRational{ 1, 12 };
       ctx->time_base = ost->st->time_base;
       ctx->gop_size = 1;
       ctx->pix_fmt = AV_PIX_FMT_YUV420P;

       //if neccesary, set stream headers and formats separately
       if ( formatCtx->oformat->flags &amp; AVFMT_GLOBALHEADER ) {
           std::cout &lt;&lt; "setting stream and headers to be separate" &lt;&lt; std::endl;
           ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
       }

       return 0;
    }

    /**
    open the video for writing
    @param formatCtx: the format context
    @param codec: output codec
    @param ost: output stream
    @param optArg: dictionary
    @return: -1 = error opening codec, -2 = allocate new frame, -3 = copy stream params
    */
    int16_t MP4Writer::OpenVideo( AVFormatContext * formatCtx, AVCodec *codec, OutputStream * ost, AVDictionary * optArg )
    {
       int error;
       AVCodecContext * ctx = ost->enc;
       AVDictionary * dict = NULL;
       av_dict_copy( &amp;dict, optArg, 0 );

       //open codec
       error = avcodec_open2( ctx, codec, &amp;dict );
       av_dict_free( &amp;dict );
       if ( error &lt; 0 ) {
           std::cout &lt;&lt; "there was an error opening the codec.  aborting" &lt;&lt; std::endl;
           return -1;
       }

       //allocate new frame
       ost->frame = AllocPicture( ctx->pix_fmt, ctx->width, ctx->height );
       if ( ost->frame == NULL ) {
           std::cout &lt;&lt; "there was an error allocating a new frame.  aborting" &lt;&lt; std::endl;
           return -2;
       }

       //copy steam params
       error = avcodec_parameters_from_context( ost->st->codecpar, ctx );
       if ( error &lt; 0 ) {
           std::cout &lt;&lt; "could not copy stream parameters.  aborting" &lt;&lt; std::endl;
           return -3;
       }

       return 0;
    }

    /**
    allocate a new frame
    @param pixFmt: ffmpeg enumerated pixel format
    @param width: output width
    @param height: output height
    @returns: an inititalized frame
    */
    AVFrame * MP4Writer::AllocPicture( enum AVPixelFormat pixFmt, int width, int height )
    {
       AVFrame * picture;
       int error;

       //allocate the frame
       picture = av_frame_alloc();
       if ( picture == NULL ) {
           std::cout &lt;&lt; "there was an error allocating the picture" &lt;&lt; std::endl;
           return NULL;
       }

       picture->format = pixFmt;
       picture->width = width;
       picture->height = height;

       //allocate the frame's data buffer
       error = av_frame_get_buffer( picture, 32 );
       if ( error &lt; 0 ) {
           std::cout &lt;&lt; "could not allocate frame data" &lt;&lt; std::endl;
           return NULL;
       }
       picture->pts = 0;
       return picture;
    }

    /**
    convert raw RGB buffer to YUV frame
    @return: frame that contains image data
    */
    AVFrame * MP4Writer::GetVideoFrame( uint8_t * imgData, OutputStream * ost, const int width, const int height )
    {
       int error;
       AVCodecContext * ctx = ost->enc;

       //prepare the frame
       error = av_frame_make_writable( ost->frame );
       if ( error &lt; 0 ) {
           std::cout &lt;&lt; "could not make frame writeable" &lt;&lt; std::endl;
           return NULL;
       }

       //TODO set this context one time per run, or even better, one time at init
       //convert RGB to YUV
       struct SwsContext* fooContext = sws_getContext( width, height, AV_PIX_FMT_BGR24,
           width, height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL );
       int inLinesize[1] = { 3 * width }; // RGB stride
       uint8_t * inData[1] = { imgData };
       int sliceHeight = sws_scale( fooContext, inData, inLinesize, 0, height, ost->frame->data, ost->frame->linesize );
       sws_freeContext( fooContext );

       ost->frame->pts = ost->next_pts++;
       //TODO does the frame need to be returned here as it is available at the class level?
       return ost->frame;
    }

    /**
    write frame to file
    @param formatCtx: the output format context
    @param timeBase: the framerate
    @param stream: output stream
    @param packet: data packet
    @returns: see return values for av_interleaved_write_frame
    */
    int MP4Writer::WriteFrame( AVFormatContext * formatCtx, const AVRational * timeBase, AVStream * stream, AVPacket * packet )
    {
       av_packet_rescale_ts( packet, *timeBase, stream->time_base );
       packet->stream_index = stream->index;

       //write compressed file to media file
       return av_interleaved_write_frame( formatCtx, packet );
    }

    int16_t MP4Writer::Write( std::vector<imgdata> &amp; imgData )
    {
       int16_t errorCount = 0;
       int16_t retVal = 0;
       bool countingUp = true;
       size_t i = 0;
       while ( true ) {
           //don't show first frame again when counting back down
           if ( !countingUp &amp;&amp; i == 0 ) {
               break;
           }
           uint8_t * pixels = imgData[i].GetBits( imgData[i].mp4Input );
           AddFrame( pixels );

           //handle inc/dec without repeating last frame
           if ( countingUp ) {
               if ( i == imgData.size() -1 ) {
                   countingUp = false;
                   i--;
               }
               else {
                   i++;
               }
           }
           else {
               i--;
           }
       }
       Finalize();
       return 0; //TODO return error code
    }

    /**
    add another frame to output video
    @param imgData: the raw image data
    @returns -1 = error encoding video frame, -2 = error writing frame
    */
    int16_t MP4Writer::AddFrame( uint8_t * imgData )
    {
       int error;
       AVCodecContext * ctx;
       AVFrame * frame;
       int gotPacket = 0;
       AVPacket pkt = { 0 };

       ctx = _ost.enc;
       av_init_packet( &amp;pkt );

       frame = GetVideoFrame( imgData, &amp;_ost, _width, _height );

       //encode the image
       error = avcodec_encode_video2( ctx, &amp;pkt, frame, &amp;gotPacket );
       if ( error &lt; 0 ) {
           std::cout &lt;&lt; "there was an error encoding the video frame" &lt;&lt; std::endl;
           return -1;
       }

       //write the frame.  NOTE: this doesn't kick in until the encoder has received a certain number of frames
       if ( gotPacket ) {
           error = WriteFrame( _formatCtx, &amp;ctx->time_base, _ost.st, &amp;pkt );
           if ( error &lt; 0 ) {
               std::cout &lt;&lt; "the video frame could not be written" &lt;&lt; std::endl;
               return -2;
           }
       }
       return 0;
    }

    /**
    finalize output video and cleanup
    */
    int16_t MP4Writer::Finalize()
    {
       av_write_trailer( _formatCtx );
       avcodec_free_context( &amp;_ost.enc );
       av_frame_free( &amp;_ost.frame);
       av_frame_free( &amp;_ost.tmp_frame );
       avio_closep( &amp;_formatCtx->pb );
       avformat_free_context( _formatCtx );
       sws_freeContext( _ost.sws_ctx );
       swr_free( &amp;_ost.swr_ctx);
       return 0;
    }
    </imgdata></algorithm>

    usage

    #include
    #include
    #include <vector>

    struct ImgData
    {
       unsigned int width;
       unsigned int height;
       std::string path;
       FIBITMAP * mp4Input;

       uint8_t * GetBits( FIBITMAP * bmp ) { return FreeImage_GetBits( bmp ); }
    };

    int main()
    {
        std::vector<imgdata> imgDataVec;
        //load images and push to imgDataVec
        MP4Writer mp4Writer;
        mp4Writer.SetHeight( 1200 ); //assumes 1:1 aspect ratio
        mp4Writer.Init();
        mp4Writer.SetOutput( "test.mp4" );
        mp4Writer.Write( imgDataVec );
    }
    </imgdata></vector>