Recherche avancée

Médias (1)

Mot : - Tags -/book

Autres articles (41)

  • 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

  • Other interesting software

    13 avril 2011, par

    We don’t claim to be the only ones doing what we do ... and especially not to assert claims to be the best either ... What we do, we just try to do it well and getting better ...
    The following list represents softwares that tend to be more or less as MediaSPIP or that MediaSPIP tries more or less to do the same, whatever ...
    We don’t know them, we didn’t try them, but you can take a peek.
    Videopress
    Website : http://videopress.com/
    License : GNU/GPL v2
    Source code : (...)

  • Les formats acceptés

    28 janvier 2010, par

    Les commandes suivantes permettent d’avoir des informations sur les formats et codecs gérés par l’installation local de ffmpeg :
    ffmpeg -codecs ffmpeg -formats
    Les format videos acceptés en entrée
    Cette liste est non exhaustive, elle met en exergue les principaux formats utilisés : h264 : H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 m4v : raw MPEG-4 video format flv : Flash Video (FLV) / Sorenson Spark / Sorenson H.263 Theora wmv :
    Les formats vidéos de sortie possibles
    Dans un premier temps on (...)

Sur d’autres sites (6996)

  • How to fix ffmpeg's offical tutorials03 bug that sound does't work well ?

    31 janvier 2019, par xiaodai

    I want to learn to make a player with ffmpeg and sdl. The tutorial I used is this.[http://dranger.com/ffmpeg/tutorial03.html] 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.

  • How to overlay 2 videos, one is main second one is overlaying it, and play sound simultaneously. using FFMPEG in ANDROID STUDIO

    6 août 2020, par Dusan Lilic

    as title say I'm trying to overlay 2 videos and play sound simultaneously. So far i managed to put 1 video over another using this command :

    &#xA;

    String[] command = {"-i", mainVideoPath, "-vf",&#xA;            "movie=" &#x2B; overlayVideo &#x2B; ", scale=300:-1[inner]; [in][inner]overlay=10:10[out]" ,combinedVideoOutput};&#xA;

    &#xA;

    and this works but I have 3 problems here.&#xA;First, video is rotated by 90 degrees (overlay video), second Audio is played only from main video (I want to play sound from both videos simultaneously), and third overlay video is longer (for example : overlayVideo duration is 10 seconds and main video last 7 seconds) then mainVideo, so i want to final video last as long as mainVideo, as soon as mainVideo finish, overlayVideo should also stop (need to cut it prolly ?)

    &#xA;

    String[] command = {"-i", mainVideoPath, "-i", overlayVideo ,&#xA;            "-filter_complex", "[1:v][0:v]scale2ref=(256/256)*ih/8/sar:ih/8[wm][base];[base][wm]overlay=10:10" ,combinedVideoOutput};&#xA;

    &#xA;

    Using this command i have 2 problems same as above except video is not rotated here.&#xA;I have to say that I'm not very familiar with ffmpeg commands. I was trying to figure it out from documentation link to documentation but without any success.&#xA;I know that I'm missing some filters like -map merge or something but can't figure it out.&#xA;Thanks in advance !

    &#xA;

    EDIT1 :&#xA;This is logcat from second commad as asked

    &#xA;

    D/LISKO: ffmpeg version n4.0-39-gda39990 Copyright (c) 2000-2018 the FFmpeg developers&#xA;      built with gcc 4.9.x (GCC) 20150123 (prerelease)&#xA;D/LISKO:   configuration: --target-os=linux --cross-prefix=/root/bravobit/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/root/bravobit/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-ffprobe --enable-libopus --enable-libvorbis --enable-libfdk-aac --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-libvpx --enable-libass --enable-yasm --enable-pthreads --disable-debug --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-linux-perf --disable-doc --disable-shared --enable-static --enable-runtime-cpudetect --enable-nonfree --enable-network --enable-avresample --enable-avformat --enable-avcodec --enable-indev=lavfi --enable-hwaccels --enable-ffmpeg --enable-zlib --enable-gpl --enable-small --enable-nonfree --pkg-config=pkg-config --pkg-config-flags=--static --prefix=/root/bravobit/ffmpeg-android/build/armeabi-v7a --extra-cflags=&#x27;-I/root/bravobit/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all&#x27; --extra-ldflags=&#x27;-L/root/bravobit/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie&#x27; --extra-cxxflags=&#xA;D/LISKO:   libavutil      56. 14.100 / 56. 14.100&#xA;      libavcodec     58. 18.100 / 58. 18.100&#xA;      libavformat    58. 12.100 / 58. 12.100&#xA;      libavdevice    58.  3.100 / 58.  3.100&#xA;      libavfilter     7. 16.100 /  7. 16.100&#xA;D/LISKO:   libavresample   4.  0.  0 /  4.  0.  0&#xA;      libswscale      5.  1.100 /  5.  1.100&#xA;D/LISKO:   libswresample   3.  1.100 /  3.  1.100&#xA;      libpostproc    55.  1.100 / 55.  1.100&#xA;D/LISKO: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;/storage/emulated/0/mainVideo.mp4&#x27;:&#xA;      Metadata:&#xA;        major_brand     : iso6&#xA;        minor_version   : 1&#xA;        compatible_brands: mp42iso6avc1isom&#xA;        creation_time   : 2020-08-03T13:20:11.000000Z&#xA;      Duration: 00:00:07.04, start: 0.000000, bitrate: 1380 kb/s&#xA;        Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 140 kb/s (default)&#xA;        Metadata:&#xA;          creation_time   : 2020-07-28T08:11:36.000000Z&#xA;        Stream #0:1(und): Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 81:256 DAR 9:16], 1264 kb/s, 30 fps, 30 tbr, 90k tbn, 180k tbc (default)&#xA;        Metadata:&#xA;          creation_time   : 2020-07-28T08:11:36.000000Z&#xA;D/LISKO: Input #1, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;/storage/emulated/0/overlayVideo.mp4&#x27;:&#xA;      Metadata:&#xA;        major_brand     : mp42&#xA;        minor_version   : 0&#xA;        compatible_brands: isommp42&#xA;        creation_time   : 2020-08-04T07:27:47.000000Z&#xA;        com.android.version: 10&#xA;      Duration: 00:00:11.19, start: 0.000000, bitrate: 9993 kb/s&#xA;D/LISKO:     Stream #1:0(eng): Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720, 9238 kb/s, SAR 1:1 DAR 16:9, 28.38 fps, 29.75 tbr, 90k tbn, 180k tbc (default)&#xA;        Metadata:&#xA;          rotate          : 270&#xA;          creation_time   : 2020-08-04T07:27:47.000000Z&#xA;          handler_name    : VideoHandle&#xA;        Side data:&#xA;          displaymatrix: rotation of 90.00 degrees&#xA;        Stream #1:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 192 kb/s (default)&#xA;        Metadata:&#xA;          creation_time   : 2020-08-04T07:27:47.000000Z&#xA;          handler_name    : SoundHandle&#xA;    Stream mapping:&#xA;      Stream #0:1 (h264) -> scale2ref:ref (graph 0)&#xA;      Stream #1:0 (h264) -> scale2ref:default (graph 0)&#xA;      overlay (graph 0) -> Stream #0:0 (libx264)&#xA;      Stream #0:0 -> #0:1 (aac (native) -> aac (native))&#xA;    Press [q] to stop, [?] for help&#xA;D/LISKO: [libx264 @ 0xee986100] using SAR=81/256&#xA;D/LISKO: [libx264 @ 0xee986100] using cpu capabilities: ARMv6 NEON&#xA;    [libx264 @ 0xee986100] profile High, level 3.1&#xA;D/LISKO: [libx264 @ 0xee986100] 264 - core 152 r2851M ba24899 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - 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=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00&#xA;D/LISKO: Output #0, mp4, to &#x27;/storage/emulated/0/outputVideo.mp4&#x27;:&#xA;D/LISKO:   Metadata:&#xA;        major_brand     : iso6&#xA;D/LISKO:     minor_version   : 1&#xA;D/LISKO:     compatible_brands: mp42iso6avc1isom&#xA;D/LISKO:     encoder         : Lavf58.12.100&#xA;        Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 81:256 DAR 9:16], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)&#xA;D/LISKO:     Metadata:&#xA;          encoder         : Lavc58.18.100 libx264&#xA;        Side data:&#xA;          cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1&#xA;D/LISKO:     Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)&#xA;        Metadata:&#xA;          creation_time   : 2020-07-28T08:11:36.000000Z&#xA;D/LISKO:       encoder         : Lavc58.18.100 aac&#xA;D/LISKO: frame=   26 fps=0.0 q=0.0 size=       0kB time=00:00:00.09 bitrate=   4.1kbits/s dup=2 drop=0 speed=0.185x    &#xA;D/LISKO: frame=   41 fps= 41 q=0.0 size=       0kB time=00:00:00.58 bitrate=   0.7kbits/s dup=2 drop=0 speed=0.574x    &#xA;D/LISKO: frame=   49 fps= 32 q=0.0 size=       0kB time=00:00:00.92 bitrate=   0.4kbits/s dup=2 drop=0 speed=0.613x    &#xA;D/LISKO: frame=   59 fps= 29 q=29.0 size=       0kB time=00:00:01.97 bitrate=   0.2kbits/s dup=2 drop=0 speed=0.974x    &#xA;D/LISKO: frame=   75 fps= 29 q=29.0 size=       0kB time=00:00:01.97 bitrate=   0.2kbits/s dup=2 drop=0 speed=0.762x &#xA;

    &#xA;

    EDIT2 :&#xA;After adding "-shortest" to command i managed to cut overlay video to be the same length as main video (because overlay video is always longer then mainVideo, "-shortest" take short one duration. So now, command looks like this :

    &#xA;

        String[] command = {"-i", mainVideoPath, "-i", overlayVideo ,"-filter_complex", &#xA;"[1:v][0:v]scale2ref=(256/256)*ih/8/sar:ih/8[wm][base];[base][wm]overlay=10:10", "-shortest", combinedVideoOutput};&#xA;

    &#xA;

    Rotation is good so only need to merge their audios. For now, only mainVideo audio is playing, overlay video audio isn't

    &#xA;

    EDIT 3 :

    &#xA;

       String[] command = {"-i", mainVideoPath, "-i", overlayVideo ,&#xA;            "-strict", "experimental",&#xA;            "-filter_complex",&#xA;            "[1:v][0:v]scale2ref=(256/256)*ih/8/sar:ih/8[wm][base];" &#x2B;&#xA;                    "[base][wm]overlay=10:10; " &#x2B;&#xA;                    "pan=stereo|c0=2*c0|c1=3*c0[a0];[1:a]pan=stereo|c0=1*c0|c1=4*c0[a1];[a0][a1]amix=inputs=2:duration=first:dropout_transition=2",&#xA;            "-shortest" ,combinedVideoOutput};&#xA;

    &#xA;

    With this command i managed to overlay videos, and play sound from both of them, rotation is good, but -shortest doesn't work now. Only existing problem now is to make them to last as shorter one (mainVideo is always shorter) ???

    &#xA;

    EDIT 4 :

    &#xA;

    This is finally working command

    &#xA;

            String[] command = {"-i", mainVideoPath, "-i", overlayVideo,&#xA;            "-filter_complex",&#xA;            "[1:v][0:v]scale2ref=(256/256)*ih/8/sar:ih/8[wm][base];" &#x2B;&#xA;                    "[base][wm]overlay=10:10:shortest=1;" &#x2B;&#xA;                    "pan=stereo|c0=2*c0|c1=3*c0[a0];[1:a]pan=stereo|c0=1*c0|c1=4*c0[a1];" &#x2B;&#xA;                    "[a0][a1]amix=inputs=2:duration=first:dropout_transition=2",&#xA;            combinedVideoOutput};&#xA;

    &#xA;

    Thanks

    &#xA;

  • ffmpeg ; opus encoded sound in webm does not work with ffplay or YouTube, only VLC [on hold]

    2 août 2017, par Mockarutan

    I’m having trouble getting Opus encoded sound in the webm container to work. I’m using libopus in ffmpeg.

    The file does work in VLC. But not in ffplay or on YouTube. If I take the raw wav data in a wav file and then convert it to Opus/webm with the ffmpeg.exe that comes pre-compiled. It works in VLC, ffplay and YouTube.

    So ffmpeg can obviously do it correctly, I must be doing something wrong in my code.

    The file my code produces : https://drive.google.com/file/d/0B16rIXjPXJCqcU5HVllIYW1iODg/view?usp=sharing

    Edit, More details that I forgot in my frustration : The file can be opened by ffplay and uploaded to youtube (when I interlace it with VP9 video). But the sound is just "ticks", example : https://www.youtube.com/watch?v=j_ShBbuizeo&feature=youtu.be

    I have read though all example codes that I know of from ffmpeg, but all of them is in the old API, not the send/receive api, so a big part of the code does not apply anymore. This code works with all other Codes I’ve tested, including H.264+AAC in mp4, VP8+Opus in ogg and raw PCM F32LE in wav. I would have gone with VP8+Opus in ogg if the license was as straight forward as the webm license

    I’ve looked though the source for the ffmpeg.exe command line tool and coped everything applicable in to my code base.

    (Edit 3, reduced the code as much as I can)
    Here is my code : https://pastebin.com/HTuc0g8K

    Setup :

    int initialize(int sample_rate, int per_frame_audio_samples, int audio_bitrate, const char *filename)
       {
           int ret;

           avcodec_register_all();
           av_register_all();

           ret = avformat_alloc_output_context2(&amp;outctx, NULL, "webm", filename);

           if (ret &lt; 0)
               return ret;

           aud_codec = avcodec_find_encoder(aud_codec_id);
           avcodec_register(aud_codec);

           if (!aud_codec)
               return -1;

           // Setup Audio Stream

           aud_codec_context = avcodec_alloc_context3(aud_codec);
           if (!aud_codec_context)
               return -1;

           /* select other audio parameters supported by the encoder */
           aud_codec_context->bit_rate = audio_bitrate;
           aud_codec_context->sample_rate = sample_rate;
           aud_codec_context->sample_fmt = sample_fmt;
           aud_codec_context->channel_layout = AV_CH_LAYOUT_STEREO;
           aud_codec_context->channels = av_get_channel_layout_nb_channels(aud_codec_context->channel_layout);

           aud_codec_context->codec = aud_codec;
           aud_codec_context->codec_id = aud_codec_id;


           AVRational time_base;
           time_base.num = per_frame_audio_samples;
           time_base.den = aud_codec_context->sample_rate;
           aud_codec_context->time_base = time_base;

           ret = avcodec_open2(aud_codec_context, aud_codec, NULL);

           if (ret &lt; 0)
               return ret;

           outctx->audio_codec = aud_codec;
           outctx->audio_codec_id = aud_codec_id;

           audio_st = avformat_new_stream(outctx, aud_codec);

           avcodec_parameters_from_context(audio_st->codecpar, aud_codec_context);

           conv_time_base.num = aud_codec_context->frame_size;
           conv_time_base.den = aud_codec_context->sample_rate;

           // Setup audio frame
           aud_frame = av_frame_alloc();
           aud_frame->nb_samples = aud_codec_context->frame_size;
           aud_frame->format = aud_codec_context->sample_fmt;
           aud_frame->channel_layout = aud_codec_context->channel_layout;
           aud_frame->sample_rate = aud_codec_context->sample_rate;

           int buffer_size;
           if (aud_codec_context->frame_size == 0)
           {
               buffer_size = per_frame_audio_samples * 2 * 4;
               aud_frame->nb_samples = per_frame_audio_samples;
           }
           else
           {
               buffer_size = av_samples_get_buffer_size(NULL, aud_codec_context->channels, aud_codec_context->frame_size,
                   aud_codec_context->sample_fmt, 0);
           }

           if (av_sample_fmt_is_planar(sample_fmt))
               ret = av_frame_get_buffer(aud_frame, buffer_size / 2);
           else
               ret = av_frame_get_buffer(aud_frame, buffer_size);

           if (!aud_frame || ret &lt; 0)
               return ret;

           // Setup audio resampler

           audio_swr_ctx = swr_alloc();
           if (!audio_swr_ctx)
               return -1;

           /* set options */
           av_opt_set_int(audio_swr_ctx, "in_channel_layout", aud_codec_context->channel_layout, 0);
           av_opt_set_int(audio_swr_ctx, "in_sample_rate", sample_rate, 0);
           av_opt_set_int(audio_swr_ctx, "in_frame_size", per_frame_audio_samples, 0);
           av_opt_set_sample_fmt(audio_swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0);

           av_opt_set_int(audio_swr_ctx, "out_channel_layout", aud_codec_context->channel_layout, 0);
           av_opt_set_int(audio_swr_ctx, "out_sample_rate", aud_codec_context->sample_rate, 0);
           av_opt_set_int(audio_swr_ctx, "out_frame_size", aud_codec_context->frame_size, 0);
           av_opt_set_sample_fmt(audio_swr_ctx, "out_sample_fmt", aud_codec_context->sample_fmt, 0);

           /* initialize the resampling context */
           if ((ret = swr_init(audio_swr_ctx)) &lt; 0)
           {
               return ret;
           }

           dst_rate = aud_codec_context->sample_rate;
           src_rate = sample_rate;

           src_nb_samples = per_frame_audio_samples;
           dst_nb_samples = aud_codec_context->frame_size;

           max_dst_nb_samples = av_rescale_rnd(src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);

           dst_nb_channels = av_get_channel_layout_nb_channels(aud_codec_context->channel_layout);

           ret = av_samples_alloc_array_and_samples(&amp;dst_data, &amp;dst_linesize, dst_nb_channels, dst_nb_samples, sample_fmt, 0);

           aud_frame_counter = 0;

           if (ret &lt; 0)
               return ret;

           av_dump_format(outctx, 0, filename, 1);

           if (!(outctx->oformat->flags &amp; AVFMT_NOFILE))
           {
               ret = avio_open(&amp;outctx->pb, filename, AVIO_FLAG_WRITE);
               if (ret &lt; 0)
               {
                   return ret;
               }
           }

           ret = avformat_write_header(outctx, NULL);
           if (ret &lt; 0)
               return ret;

           return 0;
       }

    Encoding and ending :

    int process_encode_loop(AVFormatContext *local_outctx, AVCodecContext *codec_context, AVStream *stream, AVRational time_base, bool flush)
       {
           int ret;

           AVPacket pkt;
           av_init_packet(&amp;pkt);
           pkt.data = NULL;
           pkt.size = 0;

           while (true)
           {
               ret = avcodec_receive_packet(codec_context, &amp;pkt);
               if (!ret)
               {
                   pkt.stream_index = stream->index;
                   av_packet_rescale_ts(&amp;pkt, time_base, stream->time_base);
                   av_interleaved_write_frame(local_outctx, &amp;pkt);

                   av_packet_unref(&amp;pkt);
               }

               if (ret == AVERROR(EAGAIN))
                   break;
               else if (ret == AVERROR_EOF)
                   break;
               else if (ret &lt; 0)
                   return ret;
               else if (flush == false)
                   break;
           }

           return 0;
       }

       int write_audio_frame(float_t *aud_sample)
       {
           int ret;
           if (dst_nb_samples > max_dst_nb_samples)
           {
               av_free(&amp;aud_frame->data[0]);
               ret = av_samples_alloc(aud_frame->data, &amp;dst_linesize, dst_nb_channels, dst_nb_samples, sample_fmt, 1);
               if (ret &lt; 0)
                   return ret;

               max_dst_nb_samples = dst_nb_samples;
           }

           ret = swr_convert(audio_swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)&amp;aud_sample, src_nb_samples);
           if (ret &lt; 0)
           {
               return ret;
           }

           aud_frame->data[0] = (uint8_t*)dst_data[0];
           aud_frame->extended_data[0] = (uint8_t*)dst_data[0];

           aud_frame->pts = aud_frame_counter++;

           ret = avcodec_send_frame(aud_codec_context, aud_frame);

           ret = process_encode_loop(outctx, aud_codec_context, audio_st, conv_time_base, false);

           if (ret &lt; 0)
               return ret;

           return 0;
       }

       int finish_audio_encoding()
       {
           int ret = avcodec_send_frame(aud_codec_context, NULL);
           if (ret &lt; 0)
               return ret;

           ret = process_encode_loop(outctx, aud_codec_context, audio_st, conv_time_base, true);
           if (ret &lt; 0)
               return ret;

           av_write_trailer(outctx);

           return ret;
       }

    Main :

    void fill_samples(float_t *dst, int nb_samples, int nb_channels, int sample_rate, float_t *t)
       {
           int i, j;
           float_t tincr = 1.0 / sample_rate;
           const float_t c = 2 * M_PI * 440.0;
           /* generate sin tone with 440Hz frequency and duplicated channels */
           for (i = 0; i &lt; nb_samples; i++) {
               *dst = sin(c * *t);
               for (j = 1; j &lt; nb_channels; j++)
                   dst[j] = dst[0];
               dst += nb_channels;
               *t += tincr;
           }
       }

       int main()
       {
           int frame_rate = 30;
           int sec = 12;
           int bit_rate = 192000;
           float t = 0;

           int src_samples_linesize;
           int src_nb_samples = 1024;
           int src_channels = 2;
           int sample_rate = 48000;

           uint8_t **src_data = NULL;

           int ret;

           initialize(sample_rate, src_nb_samples, bit_rate, "sound_test.webm");

           ret = av_samples_alloc_array_and_samples(&amp;src_data, &amp;src_samples_linesize, src_channels,
               src_nb_samples, AV_SAMPLE_FMT_FLT, 0);

           for (size_t i = 0; i &lt; frame_rate * sec; i++)
           {
               fill_samples((float *)src_data[0], src_nb_samples, src_channels, sample_rate, &amp;t);
               write_audio_frame((float *)src_data[0]);
           }
           finish_audio_encoding();

           cleanup();

           return 0;
       }

    Edit 2, This code reproduces the issue exactly and is fully self contained, if you have the ffmpeg 3.3.x libraries. It’s tried with 3.3.1 and 3.3.2 is the same results.

    So what could I be missing ? I do not think something is wrong with the sample rates or any other specifications, else it would not work in VLC or an ogg file. I do think the audio stream itself if correct, just some part of the header or how the file is formatted (look further down for some EBML inspection) that is not correct.

    As explained earlier, the licence with VP9+Opus in webm is why I have these specifics. And the exact problem is that I want the audio stream produced to work well when I upload it to YouTube.

    Any suggestion is appreciated, thanks in Advance !

    Some other things I’ve tried :

    I’ve looked at the header with the "MediaInfo" app built in to MVKTool :
    https://i.gyazo.com/3b29b41629a28bd526bf7637ce3f2601.png
    It all looks fine to me.

    I’ve also inspected the raw EBML file with EBML-Viewer (https://code.google.com/archive/p/ebml-viewer/) and in there I can se some difference between the files ;

    My file : https://i.gyazo.com/6fa8c540a2698a8a4d3421d363aede0a.png
    File produced with ffmpeg.exe : https://i.gyazo.com/04d60e64ff3c3040ea83e98cdf507530.png

    In my file it’s "Cluster" -> "BlockGroup" -> "Block", " ?"
    In the other it’s just "Cluster" -> "SimpleBlock"
    And in the webm specs, it says both are supported (https://www.webmproject.org/docs/container/)

    But I do not know much about these specific things, just looking for anything.