Recherche avancée

Médias (91)

Autres articles (30)

  • Personnaliser en ajoutant son logo, sa bannière ou son image de fond

    5 septembre 2013, par

    Certains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;

  • Ecrire une actualité

    21 juin 2013, par

    Présentez les changements dans votre MédiaSPIP ou les actualités de vos projets sur votre MédiaSPIP grâce à la rubrique actualités.
    Dans le thème par défaut spipeo de MédiaSPIP, les actualités sont affichées en bas de la page principale sous les éditoriaux.
    Vous pouvez personnaliser le formulaire de création d’une actualité.
    Formulaire de création d’une actualité Dans le cas d’un document de type actualité, les champs proposés par défaut sont : Date de publication ( personnaliser la date de publication ) (...)

  • Publier sur MédiaSpip

    13 juin 2013

    Puis-je poster des contenus à partir d’une tablette Ipad ?
    Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir

Sur d’autres sites (3544)

  • Re-creating/matching ffmpeg settings

    8 novembre 2013, par Pete Helgren

    I have an MP4 file that plays pretty much on every browser using either HTML5 or flash fallback (using mediaelement.js). I also have a series of additional video files that I would like to have "match" the one file that does play consistently but I haven't been able to match the ffmpeg settings so it produces a like file. I used ffprobe and got the following results on the file that plays successfully :

    ffprobe version N-53616-g7a2edcf Copyright (c) 2007-2013 the FFmpeg developers
    built on May 29 2013 01:21:08 with gcc 4.7.3 (GCC)
    configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
    isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
    le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
    e --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --ena
    ble-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-l
    ibopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libsp
    eex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-
    amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --
    enable-libxvid --enable-zlib
    libavutil      52. 34.100 / 52. 34.100
    libavcodec     55. 12.102 / 55. 12.102
    libavformat    55.  7.100 / 55.  7.100
    libavdevice    55.  1.101 / 55.  1.101
    libavfilter     3. 72.100 /  3. 72.100
    libswscale      2.  3.100 /  2.  3.100
    libswresample   0. 17.102 /  0. 17.102
    libpostproc    52.  3.100 / 52.  3.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'MyFile.mp4':
    Metadata:
     major_brand     : M4V
     minor_version   : 1
     compatible_brands: M4V mp42isom
     creation_time   : 2013-05-29 16:08:29
    Duration: 00:01:58.10, start: 0.000000, bitrate: 1155 kb/s
    Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 854x480 [SAR 1:1 DAR 427:240], 995 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc
    Metadata:
     creation_time   : 2013-05-29 16:08:29
     handler_name    : Mainconcept MP4 Video Media Handler
    Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 157 kb/s
    Metadata:
     creation_time   : 2013-05-29 16:08:29
     handler_name    : Mainconcept MP4 Sound Media Handler

    Following a few guides I found online, I am using the following ffmpeg settings :

    ffmpeg -i {input file name} -y -acodec aac -ac 2 -strict experimental -ab 64k -s 320x240 -vcodec libx264 -pix_fmt yuv420p -preset slow -profile:v baseline -level 30 -maxrate 300k -bufsize 600k -b:v 300k -f mp4 -threads 0 -movflags +faststart {output file name}

    That command produces the following output :

    ffmpeg -i "testin.mp4" -y -acodec aac -ac 2 -strict experimental -ab 64k -s 320x240 -vcodec libx264 -pix_fmt yuv420p -preset slow-profile:v baseline -level 30 -maxrate 300k -bufsize 600k -b:v 300k -f mp4 -threads 0 -movflags +faststart testout.mp4

    ffmpeg version N-53616-g7a2edcf Copyright (c) 2000-2013 the FFmpeg developers  built on May 29 2013 01:21:08 with gcc 4.7.3 (GCC)  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
     libavutil      52. 34.100 / 52. 34.100
     libavcodec     55. 12.102 / 55. 12.102
     libavformat    55.  7.100 / 55.  7.100
     libavdevice    55.  1.101 / 55.  1.101
     libavfilter     3. 72.100 /  3. 72.100
     libswscale      2.  3.100 /  2.  3.100
     libswresample   0. 17.102 /  0. 17.102
     libpostproc    52.  3.100 / 52.  3.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'testin.mp4':
     Metadata:
       major_brand     : mp42
       minor_version   : 0
       compatible_brands: mp42mp41
       creation_time   : 2013-10-01 17:11:42
     Duration: 00:04:33.75, start: 0.000000, bitrate: 725 kb/s
       Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 596 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc
       Metadata:
         creation_time   : 2013-10-01 17:11:42
         handler_name    : ?Mainconcept Video Media Handler
       Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 125 kb/s
       Metadata:
         creation_time   : 2013-10-01 17:11:42
         handler_name    : #Mainconcept MP4 Sound Media Handler
    [libx264 @ 0000000000358160] using SAR=1/1
    [libx264 @ 0000000000358160] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2AVX
    [libx264 @ 0000000000358160] profile Constrained Baseline, level 3.0
    [libx264 @ 0000000000358160] 264 - core 133 r2334 a3ac64b - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=0 ref=5 deblock=1:0:0 analyse=0x1:0x111 me=umh subme=8 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 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=40 intra_refresh=0 rc_lookahead=50 rc=cbr mbtree=1 bitrate=300 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=300 vbv_bufsize=600 nal_hrd=none ip_ratio=1.40 aq=1:1.00

    Output #0, mp4, to 'testout.mp4':
     Metadata:
       major_brand     : mp42
       minor_version   : 0
       compatible_brands: mp42mp41
       encoder         : Lavf55.7.100
      Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 320x240 [SAR 1:1 DAR 4:3], q=-1--1, 300 kb/s, 30k tbn, 29.97 tbc
       Metadata:
         creation_time   : 2013-10-01 17:11:42
         handler_name    : ?Mainconcept Video Media Handler
       Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 64 kb/s
       Metadata:
         creation_time   : 2013-10-01 17:11:42
         handler_name    : #Mainconcept MP4 Sound Media Handler
    Stream mapping:
     Stream #0:0 -> #0:0 (h264 -> libx264)
     Stream #0:1 -> #0:1 (aac -> aac)
    Press [q] to stop, [?] for help
    frame=  283 fps=0.0 q=21.0 size=     329kB time=00:00:09.30 bitrate= 289.9kbits/
    frame=  565 fps=564 q=20.0 size=     760kB time=00:00:18.64 bitrate= 333.7kbits/
    frame=  846 fps=564 q=21.0 size=    1179kB time=00:00:28.20 bitrate= 342.5kbits/
    frame= 1123 fps=561 q=22.0 size=    1603kB time=00:00:37.33 bitrate= 351.7kbits/
    frame= 1376 fps=550 q=25.0 size=    1986kB time=00:00:45.69 bitrate= 356.1kbits/
    frame= 1651 fps=550 q=22.0 size=    2377kB time=00:00:55.01 bitrate= 353.9kbits/
    frame= 1914 fps=546 q=24.0 size=    2776kB time=00:01:03.70 bitrate= 357.0kbits/
    frame= 2181 fps=545 q=24.0 size=    3163kB time=00:01:12.70 bitrate= 356.4kbits/
    frame= 2443 fps=543 q=24.0 size=    3566kB time=00:01:21.38 bitrate= 358.9kbits/
    frame= 2702 fps=540 q=24.0 size=    3939kB time=00:01:30.04 bitrate= 358.4kbits/
    frame= 2965 fps=539 q=24.0 size=    4338kB time=00:01:38.73 bitrate= 359.9kbits/
    frame= 3236 fps=539 q=23.0 size=    4731kB time=00:01:48.03 bitrate= 358.7kbits/
    frame= 3505 fps=539 q=24.0 size=    5130kB time=00:01:56.75 bitrate= 360.0kbits/
    frame= 3765 fps=537 q=26.0 size=    5521kB time=00:02:05.41 bitrate= 360.6kbits/
    frame= 4036 fps=538 q=24.0 size=    5905kB time=00:02:14.59 bitrate= 359.4kbits/
    frame= 4326 fps=540 q=24.0 size=    6343kB time=00:02:24.10 bitrate= 360.6kbits/
    frame= 4606 fps=542 q=27.0 size=    6783kB time=00:02:33.49 bitrate= 362.0kbits/
    frame= 4886 fps=543 q=24.0 size=    7168kB time=00:02:42.81 bitrate= 360.7kbits/
    frame= 5166 fps=544 q=27.0 size=    7616kB time=00:02:52.13 bitrate= 362.5kbits/
    frame= 5456 fps=545 q=24.0 size=    8020kB time=00:03:01.84 bitrate= 361.3kbits/
    frame= 5736 fps=546 q=27.0 size=    8458kB time=00:03:11.18 bitrate= 362.4kbits/
    frame= 6001 fps=545 q=30.0 size=    8849kB time=00:03:20.17 bitrate= 362.1kbits/
    frame= 6264 fps=544 q=27.0 size=    9233kB time=00:03:28.85 bitrate= 362.2kbits/
    frame= 6526 fps=544 q=26.0 size=    9611kB time=00:03:37.55 bitrate= 361.9kbits/
    frame= 6793 fps=543 q=26.0 size=   10006kB time=00:03:46.51 bitrate= 361.9kbits/
    frame= 7076 fps=544 q=24.0 size=   10410kB time=00:03:55.86 bitrate= 361.6kbits/
    frame= 7355 fps=544 q=25.0 size=   10835kB time=00:04:05.20 bitrate= 362.0kbits/
    frame= 7615 fps=543 q=27.0 size=   11236kB time=00:04:13.88 bitrate= 362.5kbits/
    frame= 7883 fps=543 q=25.0 size=   11625kB time=00:04:22.89 bitrate= 362.3kbits/
    frame= 8166 fps=544 q=21.0 size=   12040kB time=00:04:32.31 bitrate= 362.2kbits/
    Starting second pass: moving header on top of the file
    frame= 8203 fps=540 q=-1.0 Lsize=   12417kB time=00:04:33.74 bitrate= 371.6kbits/s
    video:10035kB audio:2150kB subtitle:0 global headers:0kB muxing overhead 1.901929%
    [libx264 @ 0000000000358160] frame I:41    Avg QP:14.13  size: 18349
    [libx264 @ 0000000000358160] frame P:8162  Avg QP:17.24  size:  1167
    [libx264 @ 0000000000358160] mb I  I16..4: 24.8%  0.0% 75.2%
    [libx264 @ 0000000000358160] mb P  I16..4:  0.4%  0.0%  0.3%  P16..4: 29.1%  6.4%  3.2%  0.0%  0.0%    skip:60.7%
    [libx264 @ 0000000000358160] coded y,uvDC,uvAC intra: 66.1% 74.1% 53.1% inter: 15.3% 20.2% 4.0%
    [libx264 @ 0000000000358160] i16 v,h,dc,p: 23% 21% 11% 45%
    [libx264 @ 0000000000358160] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 21% 25% 14%  6%  7% 7%  7%  6%  7%
    [libx264 @ 0000000000358160] i8c dc,h,v,p: 43% 28% 19% 10%
    [libx264 @ 0000000000358160] ref P L0: 78.4% 12.6%  4.9%  2.4%  1.7%
    [libx264 @ 0000000000358160] kb/s:300.33

    The one problem with the incantation above is that on iPad's and iPhones and some Android tablets all you hear is the audio with no video. But the file listed above (contents of the ffprobe) does not have that problem, it WILL play on an iPad, iPhone, Android tablet. I researched causes for "audio, no video" and the recommendations were all over the deck. But, the file listed in the ffprobe DOES play, so if I can match those settings described by the ffprobe for all of the other videos, they should play consistently as well. My challenge is to match the ffprobe settings using ffmpeg and there doesn't seem to be a 1 to 1 match between what I see in ffprobe and the corresponding settings in ffmpeg (a nice utility would be to have ffprobe output the ffmpeg settings that could be used to create a file of similar format).

    I walked through the ffprobe documentation but couldn't find anything that would produce a format that could be used as input to ffmpeg. Suggestions ?

  • ffmpeg cannot grab the audio played through the speaker under windows 10

    6 mars 2024, par shangping

    I am using ffmpeg to grab screen. It used to work well under win7 using UScreenCapture and virtual-audio-capturer (all x86 version). On win10, the audio is not recorded. (I cannot hear any sound while playing back). I am using ffmpeg/uscreencapture/virtual-audio-capturer all win32 version since virtual-audio-capturer has no x64 release.

    



    So my question is : How can I record the video and the audio correctly using ffmpeg ?

    



    Here is the screen dump out :
first the direct show devices on my system

    



    C:\ffmpeg-20190403-ee16d14-win32-static\bin>ffmpeg -list_devices true -f dshow -i dummy
ffmpeg version N-93528-gee16d14b0a Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20190212
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  libavutil      56. 26.100 / 56. 26.100
  libavcodec     58. 48.101 / 58. 48.101
  libavformat    58. 26.101 / 58. 26.101
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 48.100 /  7. 48.100
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
[dshow @ 06303ac0] DirectShow video devices (some may be both video and audio devices)
[dshow @ 06303ac0]  "UScreenCapture"
[dshow @ 06303ac0]     Alternative name "@device_sw_{860BB310-5D01-11D0-BD3B-00A0C911CE86}\UScreenCapture"
[dshow @ 06303ac0] DirectShow audio devices
[dshow @ 06303ac0]  "Microphone Array (Realtek(R) Audio)"
[dshow @ 06303ac0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{9F102091-1843-44C8-B20E-C6F2E0F11A0F}"
[dshow @ 06303ac0]  "virtual-audio-capturer"
[dshow @ 06303ac0]     Alternative name "@device_sw_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\{8E14549B-DB61-4309-AFA1-3578E927E935}"
dummy: Immediate exit requested


    



    2nd, when I use virtual audio capturer, ffmpeg just failed :

    



    C:\ffmpeg-20190403-ee16d14-win32-static\bin>ffmpeg -f dshow -i video="UScreenCapture":audio="Microphone Array (Realtek(R) Audio)" -vf crop=1075:717:189:81 -vcodec mpeg4 -qscale 0 -framerate 60 -acodec libmp3lame -ab 128000 -async 1 output.aviffmpeg -f dshow -i video="UScreenCapture":audio="virtual-audio-capturer" -vf crop=1075:717:189:81 -vcodec mpeg4 -qscale 0 -framerate 60 -acodec libmp3lame -ab 128000 -async 1 output.avi
ffmpeg version N-93528-gee16d14b0a Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20190212
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  libavutil      56. 26.100 / 56. 26.100
  libavcodec     58. 48.101 / 58. 48.101
  libavformat    58. 26.101 / 58. 26.101
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 48.100 /  7. 48.100
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
Guessed Channel Layout for Input Stream #0.1 : stereo
Input #0, dshow, from 'video=UScreenCapture:audio=Microphone Array (Realtek(R) Audio)':
  Duration: N/A, start: 6333.289000, bitrate: N/A
    Stream #0:0: Video: rawvideo, bgr24, 3200x1080, 15 fps, 15 tbr, 10000k tbn, 10000k tbc
    Stream #0:1: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
IMMDeviceEnumerator::GetDefaultAudioEndpoint failed: hr = 0x80070490
[dshow @ 062f4ac0] real-time buffer [UScreenCapture] [video input] too full or near too full (340% of size: 3041280 [rtbufsize parameter])! frame dropped!


    



    When I use the other audio devices, it seems working but there is not audio recorded :

    



    ffmpeg -f dshow -i video="UScreenCapture":audio="Microphone Array (Realtek(R) Audio)" -vf crop=1075:717:189:81 -vcodec mpeg4 -qscale 0 -framerate 60 -acodec libmp3lame -ab 128000 -async 1 output.avi

    C:\ffmpeg-20190403-ee16d14-win32-static\bin>ffmpeg -f dshow -i video="UScreenCapture":audio="Microphone Array (Realtek(R) Audio)" -vf crop=1075:717:189:81 -vcodec mpeg4 -qscale 0 -framerate 60 -acodec libmp3lame -ab 128000 -async 1 output.avi
    ffmpeg version N-93528-gee16d14b0a Copyright (c) 2000-2019 the FFmpeg developers
      built with gcc 8.2.1 (GCC) 20190212
      configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
      libavutil      56. 26.100 / 56. 26.100
      libavcodec     58. 48.101 / 58. 48.101
      libavformat    58. 26.101 / 58. 26.101
      libavdevice    58.  7.100 / 58.  7.100
      libavfilter     7. 48.100 /  7. 48.100
      libswscale      5.  4.100 /  5.  4.100
      libswresample   3.  4.100 /  3.  4.100
      libpostproc    55.  4.100 / 55.  4.100
    Guessed Channel Layout for Input Stream #0.1 : stereo
    Input #0, dshow, from 'video=UScreenCapture:audio=Microphone Array (Realtek(R) Audio)':
      Duration: N/A, start: 6168.067000, bitrate: N/A
        Stream #0:0: Video: rawvideo, bgr24, 3200x1080, 15 fps, 15 tbr, 10000k tbn, 10000k tbc
        Stream #0:1: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
    Please use -q:a or -q:v, -qscale is ambiguous
    Stream mapping:
      Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg4 (native))
      Stream #0:1 -> #0:1 (pcm_s16le (native) -> mp3 (libmp3lame))
    Press [q] to stop, [?] for help
    -async is forwarded to lavfi similarly to -af aresample=async=1:min_hard_comp=0.100000:first_pts=0.
    Output #0, avi, to 'output.avi':
      Metadata:
        ISFT            : Lavf58.26.101
        Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p(progressive), 1075x717, q=2-31, 200 kb/s, 15 fps, 15 tbn, 15 tbc
        Metadata:
          encoder         : Lavc58.48.101 mpeg4
        Side data:
          cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
        Stream #0:1: Audio: mp3 (libmp3lame) (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16p, 128 kb/s
        Metadata:
          encoder         : Lavc58.48.101 libmp3lame
    frame=    8 fps=0.0 q=0.0 size=     522kB time=00:00:00.53 bitrate=8013.1kbitsframe=   16 fps= 16 q=0.0 size=    1034kB time=00:00:01.06 bitrate=7938.7kbitsframe=   23 fps= 15 q=0.0 size=    1546kB time=00:00:01.53 bitrate=8258.0kbitsframe=   31 fps= 15 q=0.0 size=    2058kB time=00:00:02.06 bitrate=8156.4kbitsframe=   38 fps= 15 q=0.0 size=    2058kB time=00:00:02.53 bitrate=6653.9kbitsframe=   46 fps= 15 q=0.0 size=    2570kB time=00:00:03.06 bitrate=6864.4kbitsframe=   54 fps= 15 q=0.0 size=    3082kB time=00:00:03.60 bitrate=7012.6kbitsframe=   61 fps= 15 q=0.0 size=    3338kB time=00:00:04.06 bitrate=6723.5kbitsframe=   69 fps= 15 q=0.0 size=    3850kB time=00:00:04.60 bitrate=6855.8kbitsframe=   76 fps= 15 q=0.0 size=    4106kB time=00:00:05.06 bitrate=6638.2kbitsframe=   84 fps= 15 q=0.0 size=    4618kB time=00:00:05.60 bitrate=6755.0kbitsframe=   91 fps= 15 q=0.0 size=    4874kB time=00:00:06.06 bitrate=6581.1kbitsframe=   99 fps= 15 q=0.0 size=    5130kB time=00:00:06.60 bitrate=6367.0kbitsframe=  106 fps= 15 q=0.0 size=    5642kB time=00:00:07.06 bitrate=6540.1kbitsframe=  114 fps= 15 q=0.0 size=    6154kB time=00:00:07.66 bitrate=6575.3kbitsframe=  121 fps= 15 q=0.0 size=    6410kB time=00:00:08.13 bitrate=6455.9kbitsframe=  129 fps= 15 q=0.0 size=    6666kB time=00:00:08.66 bitrate=6300.6kbitsframe=  136 fps= 15 q=0.0 size=    6922kB time=00:00:09.13 bitrate=6208.3kbitsframe=  144 fps= 15 q=0.0 size=    7434kB time=00:00:09.66 bitrate=6299.7kbitsframe=  151 fps= 15 q=0.0 size=    7690kB time=00:00:10.13 bitrate=6216.5kbitsframe=  159 fps= 15 q=0.0 size=    7946kB time=00:00:10.66 bitrate=6102.3kbitsframe=  167 fps= 15 q=0.0 size=    8458kB time=00:00:11.20 bitrate=6186.2kbitsframe=  174 fps= 15 q=0.0 size=    8970kB time=00:00:11.66 bitrate=6298.3kbitsframe=  182 fps= 15 q=0.0 size=    8970kB time=00:00:12.20 bitrate=6022.9kbitsframe=  189 fps= 15 q=0.0 size=    9482kB time=00:00:12.66 bitrate=6132.2kbitsframe=  190 fps= 15 q=0.0 Lsize=    9602kB time=00:00:12.73 bitrate=6177.3kbits/s speed=   1x
    video:9382kB audio:194kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.265721%
    [dshow @ 06393f40] real-time buffer [UScreenCapture] [video input] too full or near too full (340% of size: 3041280 [rtbufsize parameter])! frame dropped!
    Exiting normally, received signal 2.


    


  • C++ ffmpeg - export to wav error : Invalid PCM packet, data has size 2 but at least a size of 4 was expected

    9 septembre 2024, par Chris P

    C++ code :

    


    AudioSegment AudioSegment::from_file(const std::string&amp; file_path, const std::string&amp; format, const std::string&amp; codec,&#xA;    const std::map&amp; parameters, int start_second, int duration) {&#xA;&#xA;    avformat_network_init();&#xA;    av_log_set_level(AV_LOG_ERROR); // Adjust logging level as needed&#xA;&#xA;    AVFormatContext* format_ctx = nullptr;&#xA;    if (avformat_open_input(&amp;format_ctx, file_path.c_str(), nullptr, nullptr) != 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not open audio file." &lt;&lt; std::endl;&#xA;        return AudioSegment();  // Return an empty AudioSegment on failure&#xA;    }&#xA;&#xA;    if (avformat_find_stream_info(format_ctx, nullptr) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not find stream information." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    int audio_stream_index = -1;&#xA;    for (unsigned int i = 0; i &lt; format_ctx->nb_streams; i&#x2B;&#x2B;) {&#xA;        if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {&#xA;            audio_stream_index = i;&#xA;            break;&#xA;        }&#xA;    }&#xA;&#xA;    if (audio_stream_index == -1) {&#xA;        std::cerr &lt;&lt; "Error: Could not find audio stream." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    AVCodecParameters* codec_par = format_ctx->streams[audio_stream_index]->codecpar;&#xA;    const AVCodec* my_codec = avcodec_find_decoder(codec_par->codec_id);&#xA;    AVCodecContext* codec_ctx = avcodec_alloc_context3(my_codec);&#xA;&#xA;    if (!codec_ctx) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate codec context." &lt;&lt; std::endl;&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    if (avcodec_parameters_to_context(codec_ctx, codec_par) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not initialize codec context." &lt;&lt; std::endl;&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    if (avcodec_open2(codec_ctx, my_codec, nullptr) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Could not open codec." &lt;&lt; std::endl;&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    SwrContext* swr_ctx = swr_alloc();&#xA;    if (!swr_ctx) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate SwrContext." &lt;&lt; std::endl;&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;    codec_ctx->sample_rate = 44100;&#xA;    // Set up resampling context to convert to S16 format with 2 bytes per sample&#xA;    av_opt_set_chlayout(swr_ctx, "in_chlayout", &amp;codec_ctx->ch_layout, 0);&#xA;    av_opt_set_int(swr_ctx, "in_sample_rate", codec_ctx->sample_rate, 0);&#xA;    av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", codec_ctx->sample_fmt, 0);&#xA;&#xA;    AVChannelLayout dst_ch_layout;&#xA;    av_channel_layout_copy(&amp;dst_ch_layout, &amp;codec_ctx->ch_layout);&#xA;    av_channel_layout_uninit(&amp;dst_ch_layout);&#xA;    av_channel_layout_default(&amp;dst_ch_layout, 2);&#xA;&#xA;    av_opt_set_chlayout(swr_ctx, "out_chlayout", &amp;dst_ch_layout, 0);&#xA;    av_opt_set_int(swr_ctx, "out_sample_rate", codec_ctx->sample_rate, 0);  // Match input sample rate&#xA;    av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);  // Force S16 format&#xA;&#xA;    if (swr_init(swr_ctx) &lt; 0) {&#xA;        std::cerr &lt;&lt; "Error: Failed to initialize the resampling context" &lt;&lt; std::endl;&#xA;        swr_free(&amp;swr_ctx);&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    AVPacket packet;&#xA;    AVFrame* frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        std::cerr &lt;&lt; "Error: Could not allocate frame." &lt;&lt; std::endl;&#xA;        swr_free(&amp;swr_ctx);&#xA;        avcodec_free_context(&amp;codec_ctx);&#xA;        avformat_close_input(&amp;format_ctx);&#xA;        return AudioSegment();&#xA;    }&#xA;&#xA;    std::vector<char> output;&#xA;    while (av_read_frame(format_ctx, &amp;packet) >= 0) {&#xA;        if (packet.stream_index == audio_stream_index) {&#xA;            if (avcodec_send_packet(codec_ctx, &amp;packet) == 0) {&#xA;                while (avcodec_receive_frame(codec_ctx, frame) == 0) {&#xA;                    if (frame->pts != AV_NOPTS_VALUE) {&#xA;                        frame->pts = av_rescale_q(frame->pts, codec_ctx->time_base, format_ctx->streams[audio_stream_index]->time_base);&#xA;                    }&#xA;&#xA;                    uint8_t* output_buffer;&#xA;                    int output_samples = av_rescale_rnd(&#xA;                        swr_get_delay(swr_ctx, codec_ctx->sample_rate) &#x2B; frame->nb_samples,&#xA;                        codec_ctx->sample_rate, codec_ctx->sample_rate, AV_ROUND_UP);&#xA;&#xA;                    int output_buffer_size = av_samples_get_buffer_size(&#xA;                        nullptr, 2, output_samples, AV_SAMPLE_FMT_S16, 1);&#xA;&#xA;                    output_buffer = (uint8_t*)av_malloc(output_buffer_size);&#xA;&#xA;                    if (output_buffer) {&#xA;                        memset(output_buffer, 0, output_buffer_size); // Zero padding to avoid random noise&#xA;                        int converted_samples = swr_convert(swr_ctx, &amp;output_buffer, output_samples,&#xA;                            (const uint8_t**)frame->extended_data, frame->nb_samples);&#xA;&#xA;                        if (converted_samples >= 0) {&#xA;                            output.insert(output.end(), output_buffer, output_buffer &#x2B; output_buffer_size);&#xA;                        }&#xA;                        else {&#xA;                            std::cerr &lt;&lt; "Error: Failed to convert audio samples." &lt;&lt; std::endl;&#xA;                        }&#xA;                        // Make sure output_buffer is valid before freeing&#xA;                        if (output_buffer != nullptr) {&#xA;                            av_free(output_buffer);&#xA;                            output_buffer = nullptr; // Prevent double-free&#xA;                        }&#xA;                    }&#xA;                    else {&#xA;                        std::cerr &lt;&lt; "Error: Could not allocate output buffer." &lt;&lt; std::endl;&#xA;                    }&#xA;                }&#xA;            }&#xA;            else {&#xA;                std::cerr &lt;&lt; "Error: Failed to send packet to codec context." &lt;&lt; std::endl;&#xA;            }&#xA;        }&#xA;        av_packet_unref(&amp;packet);&#xA;    }&#xA;&#xA;    int frame_width = av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * 2;  // Use 2 bytes per sample and 2 channels&#xA;&#xA;    std::map metadata = {&#xA;        {"sample_width", 2},  // S16 format has 2 bytes per sample&#xA;        {"frame_rate", codec_ctx->sample_rate},  // Use the input sample rate&#xA;        {"channels", 2},  // Assuming stereo output&#xA;        {"frame_width", frame_width}&#xA;    };&#xA;&#xA;    av_frame_free(&amp;frame);&#xA;    swr_free(&amp;swr_ctx);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;    avformat_close_input(&amp;format_ctx);&#xA;&#xA;    return AudioSegment(static_cast<const>(output.data()), output.size(), metadata);&#xA;}&#xA;&#xA;std::ofstream AudioSegment::export_segment_to_wav_file(const std::string&amp; out_f) {&#xA;    std::cout &lt;&lt; this->get_channels() &lt;&lt; std::endl;&#xA;    av_log_set_level(AV_LOG_ERROR);&#xA;    AVCodecContext* codec_ctx = nullptr;&#xA;    AVFormatContext* format_ctx = nullptr;&#xA;    AVStream* stream = nullptr;&#xA;    AVFrame* frame = nullptr;&#xA;    AVPacket* pkt = nullptr;&#xA;    int ret;&#xA;&#xA;    // Initialize format context for WAV&#xA;    if (avformat_alloc_output_context2(&amp;format_ctx, nullptr, "wav", out_f.c_str()) &lt; 0) {&#xA;        throw std::runtime_error("Could not allocate format context.");&#xA;    }&#xA;&#xA;    // Find encoder for PCM&#xA;    const AVCodec* codec_ptr = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE);&#xA;    if (!codec_ptr) {&#xA;        throw std::runtime_error("PCM encoder not found.");&#xA;    }&#xA;&#xA;    // Add stream&#xA;    stream = avformat_new_stream(format_ctx, codec_ptr);&#xA;    if (!stream) {&#xA;        throw std::runtime_error("Failed to create new stream.");&#xA;    }&#xA;&#xA;    // Allocate codec context&#xA;    codec_ctx = avcodec_alloc_context3(codec_ptr);&#xA;    if (!codec_ctx) {&#xA;        throw std::runtime_error("Could not allocate audio codec context.");&#xA;    }&#xA;&#xA;    // Set codec parameters for PCM&#xA;    codec_ctx->bit_rate = 128000;  // Bitrate&#xA;    codec_ctx->sample_rate = this->get_frame_rate();  // Use correct sample rate&#xA;    codec_ctx->ch_layout.nb_channels = this->get_channels();  // Set the correct channel count&#xA;&#xA;    // Set the channel layout: stereo or mono&#xA;    if (this->get_channels() == 2) {&#xA;        av_channel_layout_default(&amp;codec_ctx->ch_layout, 2);  // Stereo layout&#xA;    }&#xA;    else {&#xA;        av_channel_layout_default(&amp;codec_ctx->ch_layout, 1);  // Mono layout&#xA;    }&#xA;&#xA;    codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;  // PCM 16-bit format&#xA;&#xA;    // Open codec&#xA;    if (avcodec_open2(codec_ctx, codec_ptr, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Could not open codec.");&#xA;    }&#xA;&#xA;    // Set codec parameters to the stream&#xA;    if (avcodec_parameters_from_context(stream->codecpar, codec_ctx) &lt; 0) {&#xA;        throw std::runtime_error("Could not initialize stream codec parameters.");&#xA;    }&#xA;&#xA;    // Open output file&#xA;    std::ofstream out_file(out_f, std::ios::binary);&#xA;    if (!out_file) {&#xA;        throw std::runtime_error("Failed to open output file.");&#xA;    }&#xA;&#xA;    if (!(format_ctx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        if (avio_open(&amp;format_ctx->pb, out_f.c_str(), AVIO_FLAG_WRITE) &lt; 0) {&#xA;            throw std::runtime_error("Could not open output file.");&#xA;        }&#xA;    }&#xA;&#xA;    // Write file header&#xA;    if (avformat_write_header(format_ctx, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Error occurred when writing file header.");&#xA;    }&#xA;&#xA;    // Initialize packet&#xA;    pkt = av_packet_alloc();&#xA;    if (!pkt) {&#xA;        throw std::runtime_error("Could not allocate AVPacket.");&#xA;    }&#xA;&#xA;    // Initialize frame&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        throw std::runtime_error("Could not allocate AVFrame.");&#xA;    }&#xA;&#xA;    // Set the frame properties&#xA;    frame->format = codec_ctx->sample_fmt;&#xA;    frame->ch_layout = codec_ctx->ch_layout;&#xA;&#xA;    // Number of audio samples available in the data buffer&#xA;    int total_samples = data_.size() / (av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;    int samples_read = 0;&#xA;&#xA;    // Set the number of samples per frame dynamically based on the input data&#xA;    while (samples_read &lt; total_samples) {&#xA;        // Determine how many samples to read in this iteration (don&#x27;t exceed the total sample count)&#xA;        int num_samples = std::min(codec_ctx->frame_size, total_samples - samples_read);&#xA;        if (num_samples == 0) {&#xA;            num_samples = 1024;&#xA;            codec_ctx->frame_size = 1024;&#xA;        }&#xA;        // Ensure num_samples is not zero&#xA;        if (num_samples &lt;= 0) {&#xA;            throw std::runtime_error("Invalid number of samples in frame.");&#xA;        }&#xA;&#xA;        // Set the number of samples in the frame&#xA;        frame->nb_samples = num_samples;&#xA;&#xA;        // Allocate the frame buffer based on the number of samples&#xA;        ret = av_frame_get_buffer(frame, 0);&#xA;        if (ret &lt; 0) {&#xA;            std::cerr &lt;&lt; "Error allocating frame buffer: " &lt;&lt; ret &lt;&lt; std::endl;&#xA;            throw std::runtime_error("Could not allocate audio data buffers.");&#xA;        }&#xA;&#xA;        // Copy the audio data into the frame&#x27;s buffer (interleaving if necessary)&#xA;        /*if (codec_ctx->ch_layout.nb_channels == 2) {&#xA;            // If stereo, interleave planar data into packed format&#xA;            for (int i = 0; i &lt; num_samples; &#x2B;&#x2B;i) {&#xA;                ((int16_t*)frame->data[0])[2 * i] = ((int16_t*)data_.data())[i];                // Left channel&#xA;                ((int16_t*)frame->data[0])[2 * i &#x2B; 1] = ((int16_t*)data_.data())[total_samples &#x2B; i]; // Right channel&#xA;            }&#xA;        }&#xA;        else {&#xA;            // For mono or packed data, directly copy the samples&#xA;            std::memcpy(frame->data[0], data_.data() &#x2B; samples_read * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels,&#xA;                num_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;        }&#xA;        */&#xA;        std::memcpy(frame->data[0], data_.data() &#x2B; samples_read * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels,&#xA;            num_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);&#xA;&#xA;        // Send the frame for encoding&#xA;        ret = avcodec_send_frame(codec_ctx, frame);&#xA;        if (ret &lt; 0) {&#xA;            std::cerr &lt;&lt; "Error sending frame for encoding: " &lt;&lt; ret &lt;&lt; std::endl;&#xA;            throw std::runtime_error("Error sending frame for encoding.");&#xA;        }&#xA;&#xA;        // Receive and write encoded packets&#xA;        while (ret >= 0) {&#xA;            ret = avcodec_receive_packet(codec_ctx, pkt);&#xA;            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {&#xA;                break;&#xA;            }&#xA;            else if (ret &lt; 0) {&#xA;                throw std::runtime_error("Error during encoding.");&#xA;            }&#xA;&#xA;            out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;            av_packet_unref(pkt);&#xA;        }&#xA;&#xA;        samples_read &#x2B;= num_samples;&#xA;    }&#xA;&#xA;    // Flush the encoder&#xA;    if (avcodec_send_frame(codec_ctx, nullptr) &lt; 0) {&#xA;        throw std::runtime_error("Error flushing the encoder.");&#xA;    }&#xA;&#xA;    while (avcodec_receive_packet(codec_ctx, pkt) >= 0) {&#xA;        out_file.write(reinterpret_cast(pkt->data), pkt->size);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;&#xA;    // Write file trailer&#xA;    av_write_trailer(format_ctx);&#xA;&#xA;    // Cleanup&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;    avcodec_free_context(&amp;codec_ctx);&#xA;&#xA;    if (!(format_ctx->oformat->flags &amp; AVFMT_NOFILE)) {&#xA;        avio_closep(&amp;format_ctx->pb);&#xA;    }&#xA;    avformat_free_context(format_ctx);&#xA;&#xA;    out_file.close();&#xA;    return out_file;&#xA;}&#xA;&#xA;</const></char>

    &#xA;

    Run code :

    &#xA;

    #include "audio_segment.h"&#xA;#include "effects.h"&#xA;#include "playback.h"&#xA;#include "cppaudioop.h"&#xA;#include "exceptions.h"&#xA;#include "generators.h"&#xA;#include "silence.h"&#xA;#include "utils.h"&#xA;&#xA;#include <iostream>&#xA;#include <filesystem>&#xA;&#xA;using namespace cppdub;&#xA;&#xA;int main() {&#xA;    try {&#xA;        // Load the source audio file&#xA;        AudioSegment seg_1 = AudioSegment::from_file("../data/test10.mp3");&#xA;        std::string out_file_name = "ah-ah-ah.wav";&#xA;&#xA;        // Export the audio segment to a new file with specified settings&#xA;        //seg_1.export_segment(out_file_name, "mp3");&#xA;        seg_1.export_segment_to_wav_file(out_file_name);&#xA;&#xA;&#xA;        // Optionally play the audio segment to verify&#xA;        // play(seg_1);&#xA;&#xA;        // Load the exported audio file&#xA;        AudioSegment seg_2 = AudioSegment::from_file(out_file_name);&#xA;&#xA;        // Play segments&#xA;        //play(seg_1);&#xA;        play(seg_2);&#xA;    }&#xA;    catch (const std::exception&amp; e) {&#xA;        std::cerr &lt;&lt; "An error occurred: " &lt;&lt; e.what() &lt;&lt; std::endl;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;</filesystem></iostream>

    &#xA;

    Error in second call of from_file function :

    &#xA;

    [pcm_s16le @ 000002d82ca5bfc0] Invalid PCM packet, data has size 2 but at least a size of 4 was expected

    &#xA;

    The process continue, i call hear the seg_2 with play(seg_2) call, but i can't directly play seg_2 export wav file (from windows explorer).

    &#xA;

    I had a guess that error may be because packed vs plannar formats missmatch but i am not quit sure. Maybe a swr_convert is necessary.

    &#xA;