Recherche avancée

Médias (0)

Mot : - Tags -/organisation

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

Autres articles (111)

  • Participer à sa traduction

    10 avril 2011

    Vous pouvez nous aider à améliorer les locutions utilisées dans le logiciel ou à traduire celui-ci dans n’importe qu’elle nouvelle langue permettant sa diffusion à de nouvelles communautés linguistiques.
    Pour ce faire, on utilise l’interface de traduction de SPIP où l’ensemble des modules de langue de MediaSPIP sont à disposition. ll vous suffit de vous inscrire sur la liste de discussion des traducteurs pour demander plus d’informations.
    Actuellement MediaSPIP n’est disponible qu’en français et (...)

  • MediaSPIP 0.1 Beta version

    25 avril 2011, par

    MediaSPIP 0.1 beta is the first version of MediaSPIP proclaimed as "usable".
    The zip file provided here only contains the sources of MediaSPIP in its standalone version.
    To get a working installation, you must manually install all-software dependencies on the server.
    If you want to use this archive for an installation in "farm mode", you will also need to proceed to other manual (...)

  • Formulaire personnalisable

    21 juin 2013, par

    Cette page présente les champs disponibles dans le formulaire de publication d’un média et il indique les différents champs qu’on peut ajouter. Formulaire de création d’un Media
    Dans le cas d’un document de type média, les champs proposés par défaut sont : Texte Activer/Désactiver le forum ( on peut désactiver l’invite au commentaire pour chaque article ) Licence Ajout/suppression d’auteurs Tags
    On peut modifier ce formulaire dans la partie :
    Administration > Configuration des masques de formulaire. (...)

Sur d’autres sites (13816)

  • ffmpeg, v4l, snd_aloop ... sound asyncron (alsa buffer xrun)

    28 janvier 2019, par Tobias

    I’m trying to create a stream that automatically reloads random inputs. I would like to extend this to a database later.

    Each time ffmpeg finishes and starts again, so the input changes, the connection to the rtmp is interrupted briefly causing the whole connection breaks down. I then tried to separate audio and video, to send them to virtual devices and read from there again. Split the stream on virtual devices, reassemble them directly and send them to rtmp. If the input is then exchanged, the sending to the devices interrupts what does not bother the second ffmpeg. As soon as I stop sending to the devices the fps go very slowly (10 - 20 sec) from 25 to 0. Only then does the transmitter ffmpeg break the connection to the rtmp. The script which exchanges the inputs needs only one second. A practical test showed that everything works as desired.

    I can quite comfortably change the input while the second ffmpeg maintains the stream ...

    The joy did not last long. The sound is good 1 sec delayed. But sporadically. Sometimes everything works great. Sometimes the sound is offset.

    I wrote several scripts for this.

    Background :

    1. File is selected by random
    2. Media file is split and written to / dev / video0 (v4l loopback) and alsa default (snd_aloop loopback)
    3. Put the splits together again and stream them to a rtmp server

    Code that selects the input and sends to / dev / video0 and alsa default

    #!/bin/bash

    cat /dev/null > log

    while true;
    do

     WATERMARK="watermark.png";
     dir='/homeXXXXXXXXXX/mix'
     file=`/bin/ls -1 "$dir" | sort --random-sort | head -1`
     DATEI=`readlink --canonicalize "$dir/$file"` # Converts to full path

    if [ -z $DATEI ]
    then
     echo "Keine Datei gefunden" >> log;
    else
     START=$(date +%s);
     echo "Sende $DATEI" >> log;
     ffmpeg -re -y -i "$DATEI" -c:v libx264 -vf "fps=25,scale=640:480,setdar=4:3" -async 1 -pix_fmt yuv420p -preset ultrafast -map 0:0 -f v4l2 -vcodec rawvideo /dev/video0 -f alsa default
    fi

    DOKILL=`cat kill`;
    if [ "$DOKILL" = "1"]
    then
     break;
    fi

    done

    The Output

    ./run.sh
    ffmpeg version 3.2.12-1~deb9u1 Copyright (c) 2000-2018 the FFmpeg developers
     built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
     configuration: --prefix=/usr --extra-version='1~deb9u1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
     libavutil      55. 34.101 / 55. 34.101
     libavcodec     57. 64.101 / 57. 64.101
     libavformat    57. 56.101 / 57. 56.101
     libavdevice    57.  1.100 / 57.  1.100
     libavfilter     6. 65.100 /  6. 65.100
     libavresample   3.  1.  0 /  3.  1.  0
     libswscale      4.  2.100 /  4.  2.100
     libswresample   2.  3.100 /  2.  3.100
     libpostproc    54.  1.100 / 54.  1.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/mix/XXXXXXXXXXXXX.mp4':
     Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       title           : XXXXXXXXXXXXXXX

       encoder         : Lavf57.41.100
     Duration: 00:03:53.48, start: 0.000000, bitrate: 2705 kb/s
       Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 2573 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
       Metadata:
         handler_name    : VideoHandler
       Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 127 kb/s (default)
       Metadata:
         handler_name    : SoundHandler
    Codec AVOption preset (Configuration preset) specified for output file #0 (/dev/video0) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream.
    [Parsed_setdar_2 @ 0x5571234fe020] num:den syntax is deprecated, please use num/den or named options instead
    -async is forwarded to lavfi similarly to -af aresample=async=1:min_hard_comp=0.100000:first_pts=0.
    Output #0, v4l2, to '/dev/video0':
     Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       title           : XXXXXXXXXXX

       encoder         : Lavf57.56.101
       Stream #0:0(und): Video: rawvideo (I420 / 0x30323449), yuv420p, 640x480 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc (default)
       Metadata:
         handler_name    : VideoHandler
         encoder         : Lavc57.64.101 rawvideo
    Output #1, alsa, to 'default':
     Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       title           : XXXXXXXXXX

       encoder         : Lavf57.56.101
       Stream #1:0(und): Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s (default)
       Metadata:
         handler_name    : SoundHandler
         encoder         : Lavc57.64.101 pcm_s16le
    Stream mapping:
     Stream #0:0 -> #0:0 (h264 (native) -> rawvideo (native))
     Stream #0:1 -> #1:0 (aac (native) -> pcm_s16le (native))
    Press [q] to stop, [?] for help
    frame=  736 fps= 24 q=-0.0 Lsize=N/A time=00:00:29.67 bitrate=N/A speed=0.979x    
    video:331200kB audio:5112kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
    Exiting normally, received signal 2.

    The send script

    #!/bin/bash

    IP="XXXXXXXXX";

    ffmpeg -f video4linux2 -i /dev/video0 -f alsa -acodec pcm_s16le -i default -f flv -async 1 -pix_fmt yuv420p -preset ultrafast -vcodec libx264 -r 25 -s 640x260 -acodec aac rtmp://$IP:1935/live/test

    The Output

    ./send_stream.sh
    ffmpeg version 3.2.12-1~deb9u1 Copyright (c) 2000-2018 the FFmpeg developers
     built with gcc 6.3.0 (Debian 6.3.0-18+deb9u1) 20170516
     configuration: --prefix=/usr --extra-version='1~deb9u1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
     libavutil      55. 34.101 / 55. 34.101
     libavcodec     57. 64.101 / 57. 64.101
     libavformat    57. 56.101 / 57. 56.101
     libavdevice    57.  1.100 / 57.  1.100
     libavfilter     6. 65.100 /  6. 65.100
     libavresample   3.  1.  0 /  3.  1.  0
     libswscale      4.  2.100 /  4.  2.100
     libswresample   2.  3.100 /  2.  3.100
     libpostproc    54.  1.100 / 54.  1.100
    Input #0, video4linux2,v4l2, from '/dev/video0':
     Duration: N/A, start: 1548393682.674066, bitrate: 110592 kb/s
       Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 640x480, 110592 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
    Guessed Channel Layout for Input Stream #1.0 : stereo
    Input #1, alsa, from 'default':
     Duration: N/A, start: 1548393682.677901, bitrate: 1536 kb/s
       Stream #1:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb/s
    -async is forwarded to lavfi similarly to -af aresample=async=1:min_hard_comp=0.100000:first_pts=0.
    [libx264 @ 0x55e22cfa4f00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
    [libx264 @ 0x55e22cfa4f00] profile Constrained Baseline, level 2.1
    [libx264 @ 0x55e22cfa4f00] 264 - core 148 r2748 97eaef2 - H.264/MPEG-4 AVC codec - Copyleft 2003-2016 - 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=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=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
    Output #0, flv, to 'rtmp://XXXXXXXXXXX:1935/live/test':
     Metadata:
       encoder         : Lavf57.56.101
       Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 640x260, q=-1--1, 25 fps, 1k tbn, 25 tbc
       Metadata:
         encoder         : Lavc57.64.101 libx264
       Side data:
         cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
       Stream #0:1: Audio: aac (LC) ([10][0][0][0] / 0x000A), 48000 Hz, stereo, fltp, 128 kb/s
       Metadata:
         encoder         : Lavc57.64.101 aac
    Stream mapping:
     Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
     Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))
    Press [q] to stop, [?] for help
    [alsa @ 0x55e22cf87300] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
    [video4linux2,v4l2 @ 0x55e22cf84fe0] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
    Past duration 0.613319 too large    7344kB time=00:01:05.85 bitrate= 913.5kbits/s speed=1.04x    
    Past duration 0.614372 too large    7644kB time=00:01:08.39 bitrate= 915.6kbits/s speed=1.04x    
    Past duration 0.609749 too large    7834kB time=00:01:10.91 bitrate= 905.0kbits/s speed=1.04x    
    Past duration 0.604362 too large    8038kB time=00:01:12.92 bitrate= 903.0kbits/s speed=1.04x    
    Past duration 0.609489 too large    8070kB time=00:01:13.45 bitrate= 900.1kbits/s speed=1.04x    
    Past duration 0.615013 too large    8094kB time=00:01:13.94 bitrate= 896.8kbits/s speed=1.04x    
    Past duration 0.610893 too large    8179kB time=00:01:14.94 bitrate= 894.0kbits/s speed=1.04x    
    Past duration 0.664711 too large
    Past duration 0.639565 too large    8263kB time=00:01:15.47 bitrate= 896.8kbits/s speed=1.04x    
    Past duration 0.668999 too large    8339kB time=00:01:15.94 bitrate= 899.5kbits/s speed=1.04x    
    Past duration 0.605766 too large
    Past duration 0.633049 too large    8399kB time=00:01:16.48 bitrate= 899.6kbits/s speed=1.04x    
    Past duration 0.674599 too large
    Past duration 0.616035 too large    8451kB time=00:01:16.95 bitrate= 899.7kbits/s speed=1.04x    
    Past duration 0.656136 too large
    Past duration 0.604195 too large
    Past duration 0.601387 too large    8512kB time=00:01:17.46 bitrate= 900.2kbits/s speed=1.04x    
    Past duration 0.621895 too large    8565kB time=00:01:17.95 bitrate= 900.1kbits/s speed=1.04x    
    Past duration 0.670937 too large    8605kB time=00:01:18.46 bitrate= 898.4kbits/s speed=1.04x    
    Past duration 0.604500 too large    8642kB time=00:01:18.99 bitrate= 896.2kbits/s speed=1.04x    
    frame= 1913 fps= 25 q=-1.0 Lsize=    8670kB time=00:01:19.48 bitrate= 893.6kbits/s speed=1.04x    
    video:7290kB audio:1280kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.160292%
    [libx264 @ 0x55e22cfa4f00] frame I:8     Avg QP:18.25  size: 15502
    [libx264 @ 0x55e22cfa4f00] frame P:1905  Avg QP:20.95  size:  3853
    [libx264 @ 0x55e22cfa4f00] mb I  I16..4: 100.0%  0.0%  0.0%
    [libx264 @ 0x55e22cfa4f00] mb P  I16..4:  6.4%  0.0%  0.0%  P16..4: 38.1%  0.0%  0.0%  0.0%  0.0%    skip:55.5%
    [libx264 @ 0x55e22cfa4f00] coded y,uvDC,uvAC intra: 46.0% 30.3% 13.4% inter: 20.1% 9.8% 1.1%
    [libx264 @ 0x55e22cfa4f00] i16 v,h,dc,p: 47% 34% 10%  9%
    [libx264 @ 0x55e22cfa4f00] i8c dc,h,v,p: 45% 28% 22%  5%
    [libx264 @ 0x55e22cfa4f00] kb/s:750.98
    [aac @ 0x55e22cfa62a0] Qavg: 579.067
    Exiting normally, received signal 2.

    First everything is fine and then comes

    Past duration 0.616035 too large    8451kB time=00:01:16.95 bitrate= 899.7kbits/s speed=1.04x    
    Past duration 0.656136 too large
    Past duration 0.604195 too large
    Past duration 0.601387 too large    8512kB time=00:01:17.46 bitrate= 900.2kbits/s speed=1.04x

    And then when that comes, dives in the first window, so in the ffmpeg sends the input :

    Stream mapping:
     Stream #0:0 -> #0:0 (h264 (native) -> rawvideo (native))
     Stream #0:1 -> #1:0 (aac (native) -> pcm_s16le (native))
    Press [q] to stop, [?] for help
    frame=    9 fps=0.0 q=-0.0 size=N/A time=00:00:00.36 bitrate=N/A dup=1 drop=0 spframe=   21 fps= 21 q=-0.0 size=N/A time=00:00:00.84 bitrate=N/A dup=1 drop=0 sp[alsa @ 0x5643b3293160] ALSA buffer xrun.
       Last message repeated 1 times
    frame=   33 fps= 22 q=-0.0 size=N/A time=00:00:01.32 bitrate=N/A dup=1 drop=0 sp[alsa @ 0x5643b3293160] ALSA buffer xrun.
       Last message repeated 1 times
    frame=   46 fps= 23 q=-0.0 size=N/A time=00:00:01.84 bitrate=N/A dup=1 drop=0 spframe=   58 fps= 23 q=-0.0 size=N/A time=00:00:02.32 bitrate=N/A dup=1 drop=0 spframe=   71 fps= 24 q=-0.0 size=N/A time=00:00:02.84 bitrate=N/A dup=1 drop=0 spframe=   83 fps= 24 q=-0.0 size=N/A time=00:00:03.32 bitrate=N/A dup=1 drop=0 sp[alsa @ 0x5643b3293160] ALSA buffer xrun.
    frame=   96 fps= 24 q=-0.0 size=N/A time=00:00:03.84 bitrate=N/A dup=1 drop=0 sp[alsa @ 0x5643b3293160] ALSA buffer xrun.

    The sound is then absolutely unsynchronized ...

    Does anyone have any advice and can help me ?

  • How to fix ffmpeg's official tutorials03 bug that sound does't work well ? [on hold]

    31 janvier 2019, par xiaodai

    I want to make a player with ffmpeg and sdl. The tutorial I used is this though I have resampled the audio from decode stream, the sound still plays with loud noise.

    I have no ideas to fix it anymore.

    I used the following :

    • the latest ffmpeg and sdl1
    • Visual Studio 2010
    // tutorial03.c
    // A pedagogical video player that will stream through every video frame as fast as it can
    // and play audio (out of sync).
    //
    // This tutorial was written by Stephen Dranger (dranger@gmail.com).
    //
    // Code based on FFplay, Copyright (c) 2003 Fabrice Bellard,
    // and a tutorial by Martin Bohme (boehme@inb.uni-luebeckREMOVETHIS.de)
    // Tested on Gentoo, CVS version 5/01/07 compiled with GCC 4.1.1
    //
    // Use the Makefile to build all examples.
    //
    // Run using
    // tutorial03 myvideofile.mpg
    //
    // to play the stream on your screen.

    extern "C"{
    #include <libavcodec></libavcodec>avcodec.h>
    #include <libavformat></libavformat>avformat.h>
    #include <libswscale></libswscale>swscale.h>
    #include <libavutil></libavutil>channel_layout.h>
    #include <libavutil></libavutil>common.h>
    #include <libavutil></libavutil>frame.h>
    #include <libavutil></libavutil>samplefmt.h>
    #include "libswresample/swresample.h"

    #include <sdl></sdl>SDL.h>
    #include <sdl></sdl>SDL_thread.h>
    };
    #ifdef __WIN32__
    #undef main /* Prevents SDL from overriding main() */
    #endif

    #include

    #define SDL_AUDIO_BUFFER_SIZE 1024
    #define MAX_AUDIO_FRAME_SIZE 192000

    struct SwrContext *audio_swrCtx;
    FILE *pFile=fopen("output.pcm", "wb");
    FILE *pFile_stream=fopen("output_stream.pcm","wb");
    int audio_len;
    typedef struct PacketQueue {
       AVPacketList *first_pkt, *last_pkt;
       int nb_packets;
       int size;
       SDL_mutex *mutex;
       SDL_cond *cond;
    } PacketQueue;

    PacketQueue audioq;

    int quit = 0;

    void packet_queue_init(PacketQueue *q) {
       memset(q, 0, sizeof(PacketQueue));
       q->mutex = SDL_CreateMutex();
       q->cond = SDL_CreateCond();
    }

    int packet_queue_put(PacketQueue *q, AVPacket *pkt) {

       AVPacketList *pkt1;

       if(av_dup_packet(pkt) &lt; 0) {
           return -1;
       }

       pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));

       if(!pkt1) {
           return -1;
       }

       pkt1->pkt = *pkt;
       pkt1->next = NULL;


       SDL_LockMutex(q->mutex);

       if(!q->last_pkt) {
           q->first_pkt = pkt1;
       }

       else {
           q->last_pkt->next = pkt1;
       }

       q->last_pkt = pkt1;
       q->nb_packets++;
       q->size += pkt1->pkt.size;
       SDL_CondSignal(q->cond);

       SDL_UnlockMutex(q->mutex);
       return 0;
    }

    static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block) {
       AVPacketList *pkt1;
       int ret;

       SDL_LockMutex(q->mutex);

       for(;;) {

           if(quit) {
               ret = -1;
               break;
           }

           pkt1 = q->first_pkt;

           if(pkt1) {
               q->first_pkt = pkt1->next;

               if(!q->first_pkt) {
                   q->last_pkt = NULL;
               }

               q->nb_packets--;
               q->size -= pkt1->pkt.size;
               *pkt = pkt1->pkt;
               av_free(pkt1);
               ret = 1;
               break;

           } else if(!block) {
               ret = 0;
               break;

           } else {
               SDL_CondWait(q->cond, q->mutex);
           }
       }

       SDL_UnlockMutex(q->mutex);
       return ret;
    }

    int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size) {


        static AVPacket pkt;
        static uint8_t *audio_pkt_data = NULL;
        static int audio_pkt_size = 0;
        static AVFrame frame;

        int len1, data_size = 0;

        for(;;) {
            while(audio_pkt_size > 0) {
                int got_frame = 0;
                len1 = avcodec_decode_audio4(aCodecCtx, &amp;frame, &amp;got_frame, &amp;pkt);

                if(len1 &lt; 0) {
                    /* if error, skip frame */
                    audio_pkt_size = 0;
                    break;
                }
                audio_pkt_data += len1;
                audio_pkt_size -= len1;
                data_size = 0;
                /*

                au_convert_ctx = swr_alloc();
                au_convert_ctx=swr_alloc_set_opts(au_convert_ctx,out_channel_layout, out_sample_fmt, out_sample_rate,
                in_channel_layout,pCodecCtx->sample_fmt , pCodecCtx->sample_rate,0, NULL);
                swr_init(au_convert_ctx);

                swr_convert(au_convert_ctx,&amp;out_buffer, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)pFrame->data , pFrame->nb_samples);


                */
                if( got_frame ) {
                    audio_swrCtx=swr_alloc();
                    audio_swrCtx=swr_alloc_set_opts(audio_swrCtx,  // we're allocating a new context
                        AV_CH_LAYOUT_STEREO,//AV_CH_LAYOUT_STEREO,     // out_ch_layout
                        AV_SAMPLE_FMT_S16,         // out_sample_fmt
                        44100, // out_sample_rate
                        aCodecCtx->channel_layout, // in_ch_layout
                        aCodecCtx->sample_fmt,     // in_sample_fmt
                        aCodecCtx->sample_rate,    // in_sample_rate
                        0,                         // log_offset
                        NULL);                     // log_ctx
                    int ret=swr_init(audio_swrCtx);
                    int out_samples = av_rescale_rnd(swr_get_delay(audio_swrCtx, aCodecCtx->sample_rate) + 1024, 44100, aCodecCtx->sample_rate, AV_ROUND_UP);
                    ret=swr_convert(audio_swrCtx,&amp;audio_buf, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)frame.data ,frame.nb_samples);
                    data_size =
                        av_samples_get_buffer_size
                        (
                        &amp;data_size,
                        av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO),
                        ret,
                        AV_SAMPLE_FMT_S16,
                        1
                        );
                     fwrite(audio_buf, 1, data_size, pFile);
                    //memcpy(audio_buf, frame.data[0], data_size);
                    swr_free(&amp;audio_swrCtx);
                }

                if(data_size &lt;= 0) {
                    /* No data yet, get more frames */
                    continue;
                }

                /* We have data, return it and come back for more later */
                return data_size;
            }

            if(pkt.data) {
                av_free_packet(&amp;pkt);
            }

            if(quit) {
                return -1;
            }

            if(packet_queue_get(&amp;audioq, &amp;pkt, 1) &lt; 0) {
                return -1;
            }

            audio_pkt_data = pkt.data;
            audio_pkt_size = pkt.size;
        }
    }



    void audio_callback(void *userdata, Uint8 *stream, int len) {

       AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
       int /*audio_len,*/ audio_size;

       static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
       static unsigned int audio_buf_size = 0;
       static unsigned int audio_buf_index = 0;

       //SDL_memset(stream, 0, len);
       while(len > 0) {

           if(audio_buf_index >= audio_buf_size) {
               /* We have already sent all our data; get more */
               audio_size = audio_decode_frame(aCodecCtx, audio_buf, audio_buf_size);

               if(audio_size &lt; 0) {
                   /* If error, output silence */
                   audio_buf_size = 1024; // arbitrary?
                   memset(audio_buf, 0, audio_buf_size);

               } else {
                   audio_buf_size = audio_size;
               }

               audio_buf_index = 0;
           }

           audio_len = audio_buf_size - audio_buf_index;

           if(audio_len > len) {
               audio_len = len;
           }

           memcpy(stream, (uint8_t *)audio_buf , audio_len);
           //SDL_MixAudio(stream,(uint8_t*)audio_buf,audio_len,SDL_MIX_MAXVOLUME);
           fwrite(audio_buf, 1, audio_len, pFile_stream);
           len -= audio_len;
           stream += audio_len;
           audio_buf_index += audio_len;
           audio_len=len;
       }
    }

    int main(int argc, char *argv[]) {
       AVFormatContext *pFormatCtx = NULL;
       int             i, videoStream, audioStream;
       AVCodecContext  *pCodecCtx = NULL;
       AVCodec         *pCodec = NULL;
       AVFrame         *pFrame = NULL;
       AVPacket        packet;
       int             frameFinished;

       //float           aspect_ratio;

       AVCodecContext  *aCodecCtx = NULL;
       AVCodec         *aCodec = NULL;

       SDL_Overlay     *bmp = NULL;
       SDL_Surface     *screen = NULL;
       SDL_Rect        rect;
       SDL_Event       event;
       SDL_AudioSpec   wanted_spec, spec;

       struct SwsContext   *sws_ctx            = NULL;
       AVDictionary        *videoOptionsDict   = NULL;
       AVDictionary        *audioOptionsDict   = NULL;

       if(argc &lt; 2) {
               fprintf(stderr, "Usage: test <file>\n");
               exit(1);
           }

           // Register all formats and codecs
       av_register_all();

       if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
           fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
           exit(1);
       }

       // Open video file
       if(avformat_open_input(&amp;pFormatCtx, argv[1]/*"file.mov"*/, NULL, NULL) != 0) {
           return -1;    // Couldn't open file
       }

       // Retrieve stream information
       if(avformat_find_stream_info(pFormatCtx, NULL) &lt; 0) {
           return -1;    // Couldn't find stream information
       }

       // Dump information about file onto standard error
       av_dump_format(pFormatCtx, 0, argv[1], 0);

       // Find the first video stream
       videoStream = -1;
       audioStream = -1;

       for(i = 0; i &lt; pFormatCtx->nb_streams; i++) {
           if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO &amp;&amp;
               videoStream &lt; 0) {
                   videoStream = i;
           }

           if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO &amp;&amp;
               audioStream &lt; 0) {
                   audioStream = i;
           }
       }

       if(videoStream == -1) {
           return -1;    // Didn't find a video stream
       }

       if(audioStream == -1) {
           return -1;
       }

       aCodecCtx = pFormatCtx->streams[audioStream]->codec;
       // Set audio settings from codec info
       wanted_spec.freq = 44100;
       wanted_spec.format = AUDIO_S16SYS;
       wanted_spec.channels = av_get_channel_layout_nb_channels(AV_CH_LAYOUT_STEREO);;
       wanted_spec.silence = 0;
       wanted_spec.samples = 1024;
       wanted_spec.callback = audio_callback;
       wanted_spec.userdata = aCodecCtx;

       if(SDL_OpenAudio(&amp;wanted_spec, &amp;spec) &lt; 0) {
           fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
           return -1;
       }


       aCodec = avcodec_find_decoder(aCodecCtx->codec_id);

       if(!aCodec) {
           fprintf(stderr, "Unsupported codec!\n");
           return -1;
       }

       avcodec_open2(aCodecCtx, aCodec, &amp;audioOptionsDict);

       // audio_st = pFormatCtx->streams[index]
       packet_queue_init(&amp;audioq);
       SDL_PauseAudio(0);

       // Get a pointer to the codec context for the video stream
       pCodecCtx = pFormatCtx->streams[videoStream]->codec;

       // Find the decoder for the video stream
       pCodec = avcodec_find_decoder(pCodecCtx->codec_id);

       if(pCodec == NULL) {
           fprintf(stderr, "Unsupported codec!\n");
           return -1; // Codec not found
       }

       // Open codec
       if(avcodec_open2(pCodecCtx, pCodec, &amp;videoOptionsDict) &lt; 0) {
           return -1;    // Could not open codec
       }

       // Allocate video frame
       pFrame = av_frame_alloc();

       // Make a screen to put our video

    #ifndef __DARWIN__
       screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0, 0);
    #else
       screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 24, 0);
    #endif

       if(!screen) {
           fprintf(stderr, "SDL: could not set video mode - exiting\n");
           exit(1);
       }

       // Allocate a place to put our YUV image on that screen
       bmp = SDL_CreateYUVOverlay(pCodecCtx->width,
           pCodecCtx->height,
           SDL_YV12_OVERLAY,
           screen);
       sws_ctx =
           sws_getContext
           (
           pCodecCtx->width,
           pCodecCtx->height,
           pCodecCtx->pix_fmt,
           pCodecCtx->width,
           pCodecCtx->height,
           PIX_FMT_YUV420P,
           SWS_BILINEAR,
           NULL,
           NULL,
           NULL
           );


       // Read frames and save first five frames to disk
       i = 0;

       while(av_read_frame(pFormatCtx, &amp;packet) >= 0) {
           // Is this a packet from the video stream?
           if(packet.stream_index == videoStream) {
               // Decode video frame
               avcodec_decode_video2(pCodecCtx, pFrame, &amp;frameFinished,
                   &amp;packet);

               // Did we get a video frame?
               if(frameFinished) {
                   SDL_LockYUVOverlay(bmp);

                   AVPicture pict;
                   pict.data[0] = bmp->pixels[0];
                   pict.data[1] = bmp->pixels[2];
                   pict.data[2] = bmp->pixels[1];

                   pict.linesize[0] = bmp->pitches[0];
                   pict.linesize[1] = bmp->pitches[2];
                   pict.linesize[2] = bmp->pitches[1];

                   // Convert the image into YUV format that SDL uses
                   sws_scale
                       (
                       sws_ctx,
                       (uint8_t const * const *)pFrame->data,
                       pFrame->linesize,
                       0,
                       pCodecCtx->height,
                       pict.data,
                       pict.linesize
                       );

                   SDL_UnlockYUVOverlay(bmp);

                   rect.x = 0;
                   rect.y = 0;
                   rect.w = pCodecCtx->width;
                   rect.h = pCodecCtx->height;
                   SDL_DisplayYUVOverlay(bmp, &amp;rect);
                   SDL_Delay(40);
                   av_free_packet(&amp;packet);
               }

           } else if(packet.stream_index == audioStream) {
               packet_queue_put(&amp;audioq, &amp;packet);

           } else {
               av_free_packet(&amp;packet);
           }

           // Free the packet that was allocated by av_read_frame
           SDL_PollEvent(&amp;event);

           switch(event.type) {
           case SDL_QUIT:
               quit = 1;
               SDL_Quit();
               exit(0);
               break;

           default:
               break;
           }

       }

       // Free the YUV frame
       av_free(pFrame);
       /*swr_free(&amp;audio_swrCtx);*/
       // Close the codec
       avcodec_close(pCodecCtx);
       fclose(pFile);
       fclose(pFile_stream);
       // Close the video file
       avformat_close_input(&amp;pFormatCtx);

       return 0;
    }
    </file>

    I hope to play normally.

  • FFMPEG. Read frame, process it, put it to output video. Copy sound stream unchanged

    9 décembre 2016, par Andrey Smorodov

    I want to apply processing to a video clip with sound track, extract and process frame by frame and write result to output file. Number of frames, size of frame and speed remains unchanged in output clip. Also I want to keep the same audio track as I have in source.

    I can read clip, decode frames and process then using opencv. Audio packets are also writes fine. I’m stuck on forming output video stream.

    The minimal runnable code I have for now (sorry it not so short, but cant do it shorter) :

    extern "C" {
    #include <libavutil></libavutil>timestamp.h>
    #include <libavformat></libavformat>avformat.h>
    #include "libavcodec/avcodec.h"
    #include <libavutil></libavutil>opt.h>
    #include <libavdevice></libavdevice>avdevice.h>
    #include <libswscale></libswscale>swscale.h>
    }
    #include "opencv2/opencv.hpp"

    #if LIBAVCODEC_VERSION_INT &lt; AV_VERSION_INT(55,28,1)
    #define av_frame_alloc  avcodec_alloc_frame
    #endif

    using namespace std;
    using namespace cv;

    static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag)
    {
       AVRational *time_base = &amp;fmt_ctx->streams[pkt->stream_index]->time_base;

       char buf1[AV_TS_MAX_STRING_SIZE] = { 0 };
       av_ts_make_string(buf1, pkt->pts);
       char buf2[AV_TS_MAX_STRING_SIZE] = { 0 };
       av_ts_make_string(buf1, pkt->dts);
       char buf3[AV_TS_MAX_STRING_SIZE] = { 0 };
       av_ts_make_string(buf1, pkt->duration);

       char buf4[AV_TS_MAX_STRING_SIZE] = { 0 };
       av_ts_make_time_string(buf1, pkt->pts, time_base);
       char buf5[AV_TS_MAX_STRING_SIZE] = { 0 };
       av_ts_make_time_string(buf1, pkt->dts, time_base);
       char buf6[AV_TS_MAX_STRING_SIZE] = { 0 };
       av_ts_make_time_string(buf1, pkt->duration, time_base);

       printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
           buf1, buf4,
           buf2, buf5,
           buf3, buf6,
           pkt->stream_index);

    }


    int main(int argc, char **argv)
    {
       AVOutputFormat *ofmt = NULL;
       AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
       AVPacket pkt;
       AVFrame *pFrame = NULL;
       AVFrame *pFrameRGB = NULL;
       int frameFinished = 0;
       pFrame = av_frame_alloc();
       pFrameRGB = av_frame_alloc();

       const char *in_filename, *out_filename;
       int ret, i;
       in_filename = "../../TestClips/Audio Video Sync Test.mp4";
       out_filename = "out.mp4";

       // Initialize FFMPEG
       av_register_all();
       // Get input file format context
       if ((ret = avformat_open_input(&amp;ifmt_ctx, in_filename, 0, 0)) &lt; 0)
       {
           fprintf(stderr, "Could not open input file '%s'", in_filename);
           goto end;
       }
       // Extract streams description
       if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) &lt; 0)
       {
           fprintf(stderr, "Failed to retrieve input stream information");
           goto end;
       }
       // Print detailed information about the input or output format,
       // such as duration, bitrate, streams, container, programs, metadata, side data, codec and time base.
       av_dump_format(ifmt_ctx, 0, in_filename, 0);

       // Allocate an AVFormatContext for an output format.
       avformat_alloc_output_context2(&amp;ofmt_ctx, NULL, NULL, out_filename);
       if (!ofmt_ctx)
       {
           fprintf(stderr, "Could not create output context\n");
           ret = AVERROR_UNKNOWN;
           goto end;
       }

       // The output container format.
       ofmt = ofmt_ctx->oformat;

       // Allocating output streams
       for (i = 0; i &lt; ifmt_ctx->nb_streams; i++)
       {
           AVStream *in_stream = ifmt_ctx->streams[i];
           AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
           if (!out_stream)
           {
               fprintf(stderr, "Failed allocating output stream\n");
               ret = AVERROR_UNKNOWN;
               goto end;
           }
           ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
           if (ret &lt; 0)
           {
               fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
               goto end;
           }
           out_stream->codec->codec_tag = 0;
           if (ofmt_ctx->oformat->flags &amp; AVFMT_GLOBALHEADER)
           {
               out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
           }
       }

       // Show output format info
       av_dump_format(ofmt_ctx, 0, out_filename, 1);

       // Open output file
       if (!(ofmt->flags &amp; AVFMT_NOFILE))
       {
           ret = avio_open(&amp;ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
           if (ret &lt; 0)
           {
               fprintf(stderr, "Could not open output file '%s'", out_filename);
               goto end;
           }
       }
       // Write output file header
       ret = avformat_write_header(ofmt_ctx, NULL);
       if (ret &lt; 0)
       {
           fprintf(stderr, "Error occurred when opening output file\n");
           goto end;
       }

       // Search for input video codec info
       AVCodec *in_codec = nullptr;
       AVCodecContext* avctx = nullptr;

       int video_stream_index = -1;
       for (int i = 0; i &lt; ifmt_ctx->nb_streams; i++)
       {
           if (ifmt_ctx->streams[i]->codec->coder_type == AVMEDIA_TYPE_VIDEO)
           {
               video_stream_index = i;
               avctx = ifmt_ctx->streams[i]->codec;
               in_codec = avcodec_find_decoder(avctx->codec_id);
               if (!in_codec)
               {
                   fprintf(stderr, "in codec not found\n");
                   exit(1);
               }
               break;
           }
       }

       // Search for output video codec info
       AVCodec *out_codec = nullptr;
       AVCodecContext* o_avctx = nullptr;

       int o_video_stream_index = -1;
       for (int i = 0; i &lt; ofmt_ctx->nb_streams; i++)
       {
           if (ofmt_ctx->streams[i]->codec->coder_type == AVMEDIA_TYPE_VIDEO)
           {
               o_video_stream_index = i;
               o_avctx = ofmt_ctx->streams[i]->codec;
               out_codec = avcodec_find_encoder(o_avctx->codec_id);
               if (!out_codec)
               {
                   fprintf(stderr, "out codec not found\n");
                   exit(1);
               }
               break;
           }
       }

       // openCV pixel format
       AVPixelFormat pFormat = AV_PIX_FMT_RGB24;
       // Data size
       int numBytes = avpicture_get_size(pFormat, avctx->width, avctx->height);
       // allocate buffer
       uint8_t *buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
       // fill frame structure
       avpicture_fill((AVPicture *)pFrameRGB, buffer, pFormat, avctx->width, avctx->height);
       // frame area
       int y_size = avctx->width * avctx->height;
       // Open input codec
       avcodec_open2(avctx, in_codec, NULL);
       // Main loop
       while (1)
       {
           AVStream *in_stream, *out_stream;
           ret = av_read_frame(ifmt_ctx, &amp;pkt);
           if (ret &lt; 0)
           {
               break;
           }
           in_stream = ifmt_ctx->streams[pkt.stream_index];
           out_stream = ofmt_ctx->streams[pkt.stream_index];
           log_packet(ifmt_ctx, &amp;pkt, "in");
           // copy packet
           pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
           pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
           pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
           pkt.pos = -1;

           log_packet(ofmt_ctx, &amp;pkt, "out");
           if (pkt.stream_index == video_stream_index)
           {
               avcodec_decode_video2(avctx, pFrame, &amp;frameFinished, &amp;pkt);
               if (frameFinished)
               {
                   struct SwsContext *img_convert_ctx;
                   img_convert_ctx = sws_getCachedContext(NULL,
                       avctx->width,
                       avctx->height,
                       avctx->pix_fmt,
                       avctx->width,
                       avctx->height,
                       AV_PIX_FMT_BGR24,
                       SWS_BICUBIC,
                       NULL,
                       NULL,
                       NULL);
                   sws_scale(img_convert_ctx,
                       ((AVPicture*)pFrame)->data,
                       ((AVPicture*)pFrame)->linesize,
                       0,
                       avctx->height,
                       ((AVPicture *)pFrameRGB)->data,
                       ((AVPicture *)pFrameRGB)->linesize);

                   sws_freeContext(img_convert_ctx);

                   // Do some image processing
                   cv::Mat img(pFrame->height, pFrame->width, CV_8UC3, pFrameRGB->data[0],false);
                   cv::GaussianBlur(img,img,Size(5,5),3);
                   cv::imshow("Display", img);
                   cv::waitKey(5);
                   // --------------------------------
                   // Transform back to initial format
                   // --------------------------------
                   img_convert_ctx = sws_getCachedContext(NULL,
                       avctx->width,
                       avctx->height,
                       AV_PIX_FMT_BGR24,
                       avctx->width,
                       avctx->height,
                       avctx->pix_fmt,
                       SWS_BICUBIC,
                       NULL,
                       NULL,
                       NULL);
                   sws_scale(img_convert_ctx,
                       ((AVPicture*)pFrameRGB)->data,
                       ((AVPicture*)pFrameRGB)->linesize,
                       0,
                       avctx->height,
                       ((AVPicture *)pFrame)->data,
                       ((AVPicture *)pFrame)->linesize);
                       // --------------------------------------------
                       // Something must be here
                       // --------------------------------------------
                       //
                       // Write fideo frame (How to write frame to output stream ?)
                       //
                       // --------------------------------------------
                        sws_freeContext(img_convert_ctx);
               }

           }
           else // write sound frame
           {
               ret = av_interleaved_write_frame(ofmt_ctx, &amp;pkt);
           }
           if (ret &lt; 0)
           {
               fprintf(stderr, "Error muxing packet\n");
               break;
           }
           // Decrease packet ref counter
           av_packet_unref(&amp;pkt);
       }
       av_write_trailer(ofmt_ctx);
    end:
       avformat_close_input(&amp;ifmt_ctx);
       // close output
       if (ofmt_ctx &amp;&amp; !(ofmt->flags &amp; AVFMT_NOFILE))
       {
           avio_closep(&amp;ofmt_ctx->pb);
       }
       avformat_free_context(ofmt_ctx);
       if (ret &lt; 0 &amp;&amp; ret != AVERROR_EOF)
       {
           char buf_err[AV_ERROR_MAX_STRING_SIZE] = { 0 };
           av_make_error_string(buf_err, AV_ERROR_MAX_STRING_SIZE, ret);
           fprintf(stderr, "Error occurred: %s\n", buf_err);
           return 1;
       }

       avcodec_close(avctx);
       av_free(pFrame);
       av_free(pFrameRGB);

       return 0;
    }