Recherche avancée

Médias (39)

Mot : - Tags -/audio

Autres articles (42)

  • La file d’attente de SPIPmotion

    28 novembre 2010, par

    Une file d’attente stockée dans la base de donnée
    Lors de son installation, SPIPmotion crée une nouvelle table dans la base de donnée intitulée spip_spipmotion_attentes.
    Cette nouvelle table est constituée des champs suivants : id_spipmotion_attente, l’identifiant numérique unique de la tâche à traiter ; id_document, l’identifiant numérique du document original à encoder ; id_objet l’identifiant unique de l’objet auquel le document encodé devra être attaché automatiquement ; objet, le type d’objet auquel (...)

  • Support de tous types de médias

    10 avril 2011

    Contrairement à beaucoup de logiciels et autres plate-formes modernes de partage de documents, MediaSPIP a l’ambition de gérer un maximum de formats de documents différents qu’ils soient de type : images (png, gif, jpg, bmp et autres...) ; audio (MP3, Ogg, Wav et autres...) ; vidéo (Avi, MP4, Ogv, mpg, mov, wmv et autres...) ; contenu textuel, code ou autres (open office, microsoft office (tableur, présentation), web (html, css), LaTeX, Google Earth) (...)

  • Supporting all media types

    13 avril 2011, par

    Unlike most software and media-sharing platforms, MediaSPIP aims to manage as many different media types as possible. The following are just a few examples from an ever-expanding list of supported formats : images : png, gif, jpg, bmp and more audio : MP3, Ogg, Wav and more video : AVI, MP4, OGV, mpg, mov, wmv and more text, code and other data : OpenOffice, Microsoft Office (Word, PowerPoint, Excel), web (html, CSS), LaTeX, Google Earth and (...)

Sur d’autres sites (7316)

  • Frame sliced into 2 under some degree. How fix it ?

    26 novembre 2020, par Алекс Аникей

    I trying use h264 codec in videochat application. And in some reason frame sliced into 2 triangle (picture below). I try send my desktop image to another person and get this image on another client.

    


    What settings i set wrong ?
My code :

    


    Init :

    


    VCSession *vc_new_x264(Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data,
                       VCSession *vc)
{

    if (x264_param_default_preset(&param, "slow", "zerolatency") < 0) {
        // goto fail;
    }

    param.i_csp = X264_CSP_I420;
    param.i_width  = 1920;
    param.i_height = 1080;
    vc->h264_enc_width = param.i_width;
    vc->h264_enc_height = param.i_height;

    param.i_keyint_max = 30;

    param.b_vfr_input = 1; /* VFR input.  If 1, use timebase and timestamps for ratecontrol purposes.
                            * If 0, use fps only. */
    param.i_timebase_num = 1;       // 1 ms = timebase units = (1/1000)s
    param.i_timebase_den = 1000;   // 1 ms = timebase units = (1/1000)s
    param.b_repeat_headers = 1;
    param.b_annexb = 1;

    param.rc.f_rate_tolerance = VIDEO_F_RATE_TOLERANCE_H264;
    param.rc.i_vbv_buffer_size = 1500;
    param.rc.i_vbv_max_bitrate = VIDEO_BITRATE_INITIAL_VALUE_H264 * 1;

    vc->h264_enc_bitrate = VIDEO_BITRATE_INITIAL_VALUE_H264;

    param.rc.i_qp_min = 13;
    param.rc.i_qp_max = 35; // max quantizer for x264

    vc->h264_enc_bitrate = VIDEO_BITRATE_INITIAL_VALUE_H264;

    param.rc.b_stat_read = 0;
    param.rc.b_stat_write = 0;


    if (x264_param_apply_profile(&param,
                                 "high") < 0) { // "baseline", "main", "high", "high10", "high422", "high444"
        // goto fail;
    }


    if (x264_picture_alloc(&(vc->h264_in_pic), param.i_csp, param.i_width, param.i_height) < 0) {
        // goto fail;
    }

    vc->h264_encoder = x264_encoder_open(&param);

    AVCodec *codec = NULL;
    vc->h264_decoder = NULL;
    avcodec_register_all();
    codec = NULL;

    codec = avcodec_find_decoder(AV_CODEC_ID_H264);

    if (!codec) {
        LOGGER_WARNING(log, "codec not found H264 on decoder");
    }

    vc->h264_decoder = avcodec_alloc_context3(codec);

    if (codec->capabilities & AV_CODEC_CAP_TRUNCATED) {
        vc->h264_decoder->flags |= AV_CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
    }


    vc->h264_decoder->delay = 5;

    if (avcodec_open2(vc->h264_decoder, codec, NULL) < 0) {
        LOGGER_WARNING(log, "could not open codec H264 on decoder");
    }


    return vc;
}


    


    Get frame and decoding him :

    


    void vc_iterate_x264(VCSession *vc)
{

    if (!vc) {
        return;
    }

    pthread_mutex_lock(vc->queue_mutex);

    struct RTPMessage *p;

    if (!rb_read(vc->vbuf_raw, (void **)&p)) {
        LOGGER_TRACE(vc->log, "no Video frame data available");
        pthread_mutex_unlock(vc->queue_mutex);
        return;
    }

    pthread_mutex_unlock(vc->queue_mutex);
    const struct RTPHeader *const header = &p->header;

    uint32_t full_data_len;

    if (header->flags & RTP_LARGE_FRAME) {
        full_data_len = header->data_length_full;
        LOGGER_WARNING(vc->log, "vc_iterate:001:full_data_len=%d", (int)full_data_len);
    } else {
        full_data_len = p->len;
        if (header->data_length_lower != full_data_len)
        {
            LOGGER_ERROR("Data header and packet don't equal: %d - header %d - packet", header->data_length_lower, full_data_len);
        }
        LOGGER_DEBUG(vc->log, "vc_iterate:002");
    }

    decode_frame_h264(vc, p, full_data_len);
}

void decode_frame_h264(VCSession *vc,
                       struct RTPMessage *p,
                       uint32_t full_data_len)
{

    AVPacket *compr_data;
    compr_data = av_packet_alloc();


    uint8_t *tmp_buf = calloc(1, full_data_len + FF_INPUT_BUFFER_PADDING_SIZE);
    memcpy(tmp_buf, p->data, full_data_len);

    compr_data->data = tmp_buf; // p->data;
    compr_data->size = (int)full_data_len; // hmm, "int" again

    avcodec_send_packet(vc->h264_decoder, compr_data);

    int ret_ = 0;
    while (ret_ >= 0) {
        AVFrame *frame = av_frame_alloc();
        ret_ = avcodec_receive_frame(vc->h264_decoder, frame);
        if (ret_ == AVERROR(EAGAIN) || ret_ == AVERROR_EOF) {
            // error
            break;
        } else if (ret_ < 0) {
            // Error during decoding
            break;
        } else if (ret_ == 0) {
            vc->vcb(vc->av, vc->friend_number, frame->width, frame->height,
                          (const uint8_t *)frame->data[0],
                          (const uint8_t *)frame->data[1],
                          (const uint8_t *)frame->data[2],
                          frame->linesize[0], frame->linesize[1],
                          frame->linesize[2], vc->vcb_user_data);
        } else {
            // some other error
        }
        av_frame_free(&frame);
    }
    av_packet_free(&compr_data);
    free(tmp_buf);
    free(p);
}


    


    Send frame and encoding :

    


    bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, uint16_t height, const uint8_t *y,
                            const uint8_t *u, const uint8_t *v, Toxav_Err_Send_Frame *error, int16_t kf_max_dist, vpx_codec_er_flags_t error_resilient,
                            unsigned int my_lag_in_frames, uint16_t kf_mode, uint16_t quality_mode)
{
    Toxav_Err_Send_Frame rc = TOXAV_ERR_SEND_FRAME_OK;
    ToxAVCall *call;
    uint64_t video_frame_record_timestamp = current_time_monotonic(av->m->mono_time);

    int vpx_encode_flags = 0;

    pthread_mutex_lock(call->mutex_video);
    pthread_mutex_unlock(av->mutex);

    if (y == nullptr || u == nullptr || v == nullptr) {
        pthread_mutex_unlock(call->mutex_video);
        rc = TOXAV_ERR_SEND_FRAME_NULL;
        goto RETURN;
    }


    if (call->video_rtp->ssrc < VIDEO_SEND_X_KEYFRAMES_FIRST) {
        // Key frame flag for first frames
        vpx_encode_flags = VPX_EFLAG_FORCE_KF;
        LOGGER_INFO(av->m->log, "I_FRAME_FLAG:%d only-i-frame mode", call->video_rtp->ssrc);

        ++call->video_rtp->ssrc;
    } else if (call->video_rtp->ssrc == VIDEO_SEND_X_KEYFRAMES_FIRST) {
        // normal keyframe placement
        vpx_encode_flags = 0;
        LOGGER_INFO(av->m->log, "I_FRAME_FLAG:%d normal mode", call->video_rtp->ssrc);

        ++call->video_rtp->ssrc;
    }


    x264_nal_t *nal = NULL;
    int i_frame_size = 0;

    uint32_t result = encode_frame_h264(av, friend_number, width, height,
                                        y, u, v,
                                        &video_frame_record_timestamp,
                                        vpx_encode_flags,
                                        &nal,
                                        &i_frame_size);
    if (result != 0) {
        pthread_mutex_unlock(call->mutex_video);
        rc = TOXAV_ERR_SEND_FRAME_INVALID;
        goto RETURN;
    }

    ++call->video->frame_counter;

    rc = send_frames_h264(av, friend_number, width, height,
                          y, u, v, call,
                          &video_frame_record_timestamp,
                          vpx_encode_flags,
                          &nal,
                          &i_frame_size,
                          &rc);

    pthread_mutex_unlock(call->mutex_video);

RETURN:

    if (error) {
        *error = rc;
    }

    return rc == TOXAV_ERR_SEND_FRAME_OK;
}

uint32_t send_frames_h264(ToxAV *av, uint32_t friend_number, uint16_t width, uint16_t height,
                          const uint8_t *y,
                          const uint8_t *u, const uint8_t *v, ToxAVCall *call,
                          uint64_t *video_frame_record_timestamp,
                          int vpx_encode_flags,
                          x264_nal_t **nal,
                          int *i_frame_size,
                          TOXAV_ERR_SEND_FRAME *rc)
{

    if (*i_frame_size > 0) {

        // use the record timestamp that was actually used for this frame
        *video_frame_record_timestamp = (uint64_t)call->video->h264_in_pic.i_pts;
        const uint32_t frame_length_in_bytes = *i_frame_size;
        const int keyframe = (int)call->video->h264_out_pic.b_keyframe;

        LOGGER_DEBUG(av->m->log, "video packet record time: %lu", (*video_frame_record_timestamp));

        int res = rtp_send_data
                  (
                      call->video_rtp,
                      (const uint8_t *)((*nal)->p_payload),
                      frame_length_in_bytes,
                      keyframe,
                      *video_frame_record_timestamp,
                      av->m->log
                  );

        (*video_frame_record_timestamp)++;

        if (res < 0) {
            LOGGER_WARNING(av->m->log, "Could not send video frame: %s", strerror(errno));
            *rc = TOXAV_ERR_SEND_FRAME_RTP_FAILED;
            return 1;
        }

        return 0;
    } else {
        *rc = TOXAV_ERR_SEND_FRAME_RTP_FAILED;
        return 1;
    }

}


    


    I get image like this :

    


    enter image description here

    


  • Audio Video Mixing - Sync issue in Android with FFMPEG, Media Codec in different devices

    24 novembre 2020, par khushbu

    I have already tried everything for Audio Video mixing and it's not working perfectly as in processing while mixing audio into the recorded video, sometimes the audio is ahead of video and vice-versa.

    


    Using FFMPEG :

    


    This is for add an Audio file to the Video file and generated the final Video where audio is replaced in the video.

    


    val cmd ="-i $inputVideoPath -i ${inputAudio.absolutePath} -map 0:v -map 1:a -c:v copy -shortest ${outputVideo.absolutePath}"


    


    After generating the final video, found some delay based on device performance so added delay in the below two cases :

    


    1)Added delay in Audio if audio is ahead of the video.

    


    val  cmd =  "-i ${tmpVideo.absolutePath} -itsoffset $hms -i ${tmpVideo.absolutePath} -map 0:v -map 1:a -c copy -preset veryfast ${createdVideo1?.absolutePath}"


    


    2)Added delay in Video if the video is ahead of the audio.

    


    val  cmd =   "-i ${tmpVideo.absolutePath} -itsoffset $hms -i ${tmpVideo.absolutePath} -map 1:v -map 0:a -c copy -preset veryfast ${createdVideo1?.absolutePath}"


    


    NOTE : Here $hms is delay in 00:00:00.000 formate

    


    but still, it's not working on all the devices like readmi, oneplus etc.

    


    Using Media Codec :

    


    Found some better performance in this solution but still not working on all the devices.

    


    In this process, It's supporting .aac format so first if the user selected .mp3 formate than i have to convert it into .aac format using the below function :

    


    fun Convert_Mp3_to_acc() {

   
    AndroidAudioConverter.load(requireActivity(), object : ILoadCallback {
        override fun onSuccess() {

            val callback: IConvertCallback = object : IConvertCallback {
                override fun onSuccess(convertedFile: File) {
                    toggleLoader(false)
                    audioLink = convertedFile.absolutePath
                    append()
                 

                }

                override fun onFailure(error: java.lang.Exception) {
                    toggleLoader(false)
                    Toast.makeText(requireActivity(), "" + error, Toast.LENGTH_SHORT).show()
                }
            }
            AndroidAudioConverter.with(requireActivity())
                .setFile(File(audioLink))
                .setFormat(AudioFormat.AAC)
                .setCallback(callback)
                .convert()
        }

        override fun onFailure(error: java.lang.Exception) {
            toggleLoader(false)
        }
    })
}


    


    After successful conversion from .mp3 to .aac formate, It's extracting audio track and video track for merge

    


     private fun append(): Boolean {&#xA;&#xA;    val progressDialog = ProgressDialog(requireContext())&#xA;    Thread {&#xA;        requireActivity().runOnUiThread {&#xA;            progressDialog.setMessage("Please wait..")&#xA;            progressDialog.show()&#xA;        }&#xA;        val video_list = ArrayList<string>()&#xA;        for (i in videopaths.indices) {&#xA;            val file: File = File(videopaths.get(i))&#xA;            if (file.exists()) {&#xA;                val retriever = MediaMetadataRetriever()&#xA;                retriever.setDataSource(requireActivity(), Uri.fromFile(file))&#xA;                val hasVideo =&#xA;                    retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO)&#xA;                val isVideo = "yes" == hasVideo&#xA;                if (isVideo /*&amp;&amp; file.length() > 1000*/) {&#xA;                    Log.d("resp", videopaths.get(i))&#xA;                    video_list.add(videopaths.get(i))&#xA;                }&#xA;            }&#xA;        }&#xA;        try {&#xA;            val inMovies = arrayOfNulls<movie>(video_list.size)&#xA;            for (i in video_list.indices) {&#xA;                inMovies[i] = MovieCreator.build(video_list[i])&#xA;            }&#xA;            val videoTracks: MutableList<track> =&#xA;                LinkedList()&#xA;            val audioTracks: MutableList<track> =&#xA;                LinkedList()&#xA;            for (m in inMovies) {&#xA;                for (t in m!!.tracks) {&#xA;                    if (t.handler == "soun") {&#xA;                        audioTracks.add(t)&#xA;                    }&#xA;                    if (t.handler == "vide") {&#xA;                        videoTracks.add(t)&#xA;                    }&#xA;                }&#xA;            }&#xA;            val result = Movie()&#xA;            if (audioTracks.size > 0) {&#xA;                result.addTrack(AppendTrack(*audioTracks.toTypedArray()))&#xA;            }&#xA;            if (videoTracks.size > 0) {&#xA;                result.addTrack(AppendTrack(*videoTracks.toTypedArray()))&#xA;            }&#xA;            val out = DefaultMp4Builder().build(result)&#xA;            var outputFilePath: String? = null&#xA;            outputFilePath =  Variables.outputfile&#xA;&#xA;            /*if (audio != null) {&#xA;                Variables.outputfile&#xA;            } else {&#xA;                Variables.outputfile2&#xA;            }*/&#xA;&#xA;            val fos = FileOutputStream(File(outputFilePath))&#xA;            out.writeContainer(fos.channel)&#xA;            fos.close()&#xA;&#xA;            requireActivity().runOnUiThread {&#xA;                progressDialog.dismiss()&#xA;&#xA;                Merge_withAudio()&#xA;&#xA;                /* if (audio != null) else {&#xA;                     //Go_To_preview_Activity()&#xA;                 }*/&#xA;            }&#xA;        } catch (e: java.lang.Exception) {&#xA;        }&#xA;    }.start()&#xA;&#xA;    return true&#xA;}&#xA;</track></track></movie></string>

    &#xA;

    This will add the selected audio with the recorded video

    &#xA;

    fun Merge_withAudio() {&#xA;    val root = Environment.getExternalStorageDirectory().toString()&#xA;&#xA;    // Uri mediaPath = Uri.parse("android.resource://" &#x2B; getPackageName() &#x2B; "/" &#x2B; R.raw.file_copy);&#xA;    //String audio_file =Variables.app_folder&#x2B;Variables.SelectedAudio_AAC;&#xA;&#xA;    //String filename = "android.resource://" &#x2B; getPackageName() &#x2B; "/raw/file_copy.aac";&#xA;    val audio_file: String = audioLink!!&#xA;    Log.e("Merge ", audio_file)&#xA;    val video = "$root/output.mp4"&#xA;&#xA;    val bundle=Bundle()&#xA;    bundle.putString("FinalVideo", createdVideo?.absolutePath)&#xA;&#xA;    val merge_video_audio = Merge_Video_Audio(this, bundle, object : AsyncResponse {&#xA;        override fun processFinish(output: Bundle?) {&#xA;&#xA;            requireActivity().runOnUiThread {&#xA;                finalVideo = bundle.getString("FinalVideo")&#xA;                createdVideo = File(finalVideo)&#xA;&#xA;                Log.e("Final Path ", finalVideo)&#xA;&#xA;                createThumb {&#xA;                    setUpExoPlayer()&#xA;                }&#xA;            }&#xA;&#xA;        }&#xA;    })&#xA;    merge_video_audio.doInBackground(audio_file, video, createdVideo?.absolutePath)&#xA;}&#xA;&#xA;&#xA; public class Merge_Video_Audio extends AsyncTask {&#xA;&#xA;   ProgressDialog progressDialog;&#xA;   RecentCompletedVideoFragment context;&#xA;   public AsyncResponse delegate = null;&#xA;&#xA;&#xA;Bundle bundleValue;&#xA;&#xA;String audio,video,output;&#xA;&#xA;public Merge_Video_Audio(RecentCompletedVideoFragment context, Bundle bundle , AsyncResponse delegate ){&#xA;    this.context=context;&#xA;    this.bundleValue=bundle;&#xA;    this.delegate=delegate;&#xA;    progressDialog=new ProgressDialog(context.requireContext());&#xA;    progressDialog.setMessage("Please Wait...");&#xA;}&#xA;&#xA;@Override&#xA;protected void onPreExecute() {&#xA;    super.onPreExecute();&#xA;}&#xA;&#xA;@Override&#xA;public String doInBackground(String... strings) {&#xA;    try {&#xA;        progressDialog.show();&#xA;    }catch (Exception e){&#xA;&#xA;    }&#xA;     audio=strings[0];&#xA;     video=strings[1];&#xA;     output=strings[2];&#xA;&#xA;     Log.d("resp",audio&#x2B;"----"&#x2B;video&#x2B;"-----"&#x2B;output);&#xA;&#xA;    Thread thread = new Thread(runnable);&#xA;    thread.start();&#xA;&#xA;    return null;&#xA;}&#xA;&#xA;&#xA;@Override&#xA;protected void onPostExecute(String s) {&#xA;    super.onPostExecute(s);&#xA;    Log.e("On Post Execute ", "True");&#xA;&#xA;&#xA;}&#xA;&#xA;&#xA;   public void Go_To_preview_Activity(){&#xA;&#xA;    delegate.processFinish(bundleValue);&#xA;   }&#xA;&#xA;  public Track CropAudio(String videopath, Track fullAudio){&#xA;    try {&#xA;&#xA;        IsoFile isoFile = new IsoFile(videopath);&#xA;&#xA;        double lengthInSeconds = (double)&#xA;                isoFile.getMovieBox().getMovieHeaderBox().getDuration() /&#xA;                isoFile.getMovieBox().getMovieHeaderBox().getTimescale();&#xA;&#xA;&#xA;        Track audioTrack = (Track) fullAudio;&#xA;&#xA;&#xA;        double startTime1 = 0;&#xA;        double endTime1 = lengthInSeconds;&#xA;&#xA;&#xA;        long currentSample = 0;&#xA;        double currentTime = 0;&#xA;        double lastTime = -1;&#xA;        long startSample1 = -1;&#xA;        long endSample1 = -1;&#xA;&#xA;&#xA;        for (int i = 0; i &lt; audioTrack.getSampleDurations().length; i&#x2B;&#x2B;) {&#xA;&#xA;            long delta = audioTrack.getSampleDurations()[i];&#xA;&#xA;            if (currentTime > lastTime &amp;&amp; currentTime &lt;= startTime1) {&#xA;                // current sample is still before the new starttime&#xA;                startSample1 = currentSample;&#xA;            }&#xA;            if (currentTime > lastTime &amp;&amp; currentTime &lt;= endTime1) {&#xA;                // current sample is after the new start time and still before the new endtime&#xA;                endSample1 = currentSample;&#xA;            }&#xA;&#xA;            lastTime = currentTime;&#xA;            currentTime &#x2B;= (double) delta / (double) audioTrack.getTrackMetaData().getTimescale();&#xA;            currentSample&#x2B;&#x2B;;&#xA;        }&#xA;&#xA;        CroppedTrack cropperAacTrack = new CroppedTrack(fullAudio, startSample1, endSample1);&#xA;&#xA;        return cropperAacTrack;&#xA;&#xA;    } catch (IOException e) {&#xA;        e.printStackTrace();&#xA;    }&#xA;&#xA;    return fullAudio;&#xA;}&#xA;&#xA;&#xA;&#xA;  public Runnable runnable =new Runnable() {&#xA;    @Override&#xA;    public void run() {&#xA;&#xA;        try {&#xA;&#xA;            Movie m = MovieCreator.build(video);&#xA;&#xA;&#xA;            List nuTracks = new ArrayList&lt;>();&#xA;&#xA;            for (Track t : m.getTracks()) {&#xA;                if (!"soun".equals(t.getHandler())) {&#xA;&#xA;                    Log.e("Track  ",t.getName());&#xA;                    nuTracks.add(t);&#xA;                }&#xA;            }&#xA;&#xA;            Log.e("Path ",audio.toString());&#xA;&#xA;&#xA;            try {&#xA;                // Track nuAudio = new AACTrackImpl();&#xA;                Track nuAudio = new AACTrackImpl(new FileDataSourceImpl(audio));&#xA;&#xA;                Track crop_track = CropAudio(video, nuAudio);&#xA;&#xA;                nuTracks.add(crop_track);&#xA;&#xA;                m.setTracks(nuTracks);&#xA;&#xA;                Container mp4file = new DefaultMp4Builder().build(m);&#xA;&#xA;                FileChannel fc = new FileOutputStream(new File(output)).getChannel();&#xA;                mp4file.writeContainer(fc);&#xA;                fc.close();&#xA;&#xA;            }catch (FileNotFoundException fnfe){&#xA;                fnfe.printStackTrace();&#xA;            }catch(IOException ioe){&#xA;                ioe.printStackTrace();&#xA;           }&#xA;&#xA;&#xA;            try {&#xA;&#xA;                progressDialog.dismiss();&#xA;            }catch (Exception e){&#xA;                Log.d("resp",e.toString());&#xA;&#xA;            }finally {&#xA;                Go_To_preview_Activity();&#xA;&#xA;            }&#xA;&#xA;        } catch (IOException e) {&#xA;            e.printStackTrace();&#xA;            Log.d("resp",e.toString());&#xA;&#xA;        }&#xA;&#xA;    }&#xA;&#xA; };&#xA;&#xA; }&#xA;

    &#xA;

    This solution is also not working in all the devices.

    &#xA;

    Can anyone suggest where i am going wrong or any solution for it ?

    &#xA;

  • How to play and seek fragmented MP4 audio using MSE SourceBuffer ?

    29 juin 2024, par Stefan Falk

    Note :

    &#xA;

    &#xA;

    If you end up here, you might want to take a look at shaka-player and the accompanying shaka-streamer. Use it. Don't implement this yourself unless you really have to.

    &#xA;

    &#xA;

    I am trying for quite some time now to be able to play an audio track on Chrome, Firefox, Safari, etc. but I keep hitting brick walls. My problem is currently that I am just not able to seek within a fragmented MP4 (or MP3).

    &#xA;

    At the moment I am converting audio files such as MP3 to fragmented MP4 (fMP4) and send them chunk-wise to the client. What I do is defining a CHUNK_DURACTION_SEC (chunk duration in seconds) and compute a chunk size like this :

    &#xA;

    chunksTotal = Math.ceil(this.track.duration / CHUNK_DURATION_SEC);&#xA;chunkSize = Math.ceil(this.track.fileSize / this.chunksTotal);&#xA;

    &#xA;

    With this I partition the audio file and can fetch it entirely jumping chunkSize-many bytes for each chunk :

    &#xA;

    -----------------------------------------&#xA;| chunk 1 | chunk 2 |   ...   | chunk n |&#xA;-----------------------------------------&#xA;

    &#xA;

    How audio files are converted to fMP4

    &#xA;

    ffmpeg -i input.mp3 -acodec aac -b:a 256k -f mp4 \&#xA;       -movflags faststart&#x2B;frag_every_frame&#x2B;empty_moov&#x2B;default_base_moof \&#xA;        output.mp4&#xA;

    &#xA;

    This seems to work with Chrome and Firefox (so far).

    &#xA;

    How chunks are appended

    &#xA;

    After following this example, and realizing that it's simply not working as it is explained here, I threw it away and started over from scratch. Unfortunately without success. It's still not working.

    &#xA;

    The following code is supposed to play a track from the very beginning to the very end. However, I also need to be able to seek. So far, this is simply not working. Seeking will just stop the audio after the seeking event got triggered.

    &#xA;

    The code

    &#xA;

    /* Desired chunk duration in seconds. */&#xA;const CHUNK_DURATION_SEC = 20;&#xA;&#xA;const AUDIO_EVENTS = [&#xA;  &#x27;ended&#x27;,&#xA;  &#x27;error&#x27;,&#xA;  &#x27;play&#x27;,&#xA;  &#x27;playing&#x27;,&#xA;  &#x27;seeking&#x27;,&#xA;  &#x27;seeked&#x27;,&#xA;  &#x27;pause&#x27;,&#xA;  &#x27;timeupdate&#x27;,&#xA;  &#x27;canplay&#x27;,&#xA;  &#x27;loadedmetadata&#x27;,&#xA;  &#x27;loadstart&#x27;,&#xA;  &#x27;updateend&#x27;,&#xA;];&#xA;&#xA;&#xA;class ChunksLoader {&#xA;&#xA;  /** The total number of chunks for the track. */&#xA;  public readonly chunksTotal: number;&#xA;&#xA;  /** The length of one chunk in bytes */&#xA;  public readonly chunkSize: number;&#xA;&#xA;  /** Keeps track of requested chunks. */&#xA;  private readonly requested: boolean[];&#xA;&#xA;  /** URL of endpoint for fetching audio chunks. */&#xA;  private readonly url: string;&#xA;&#xA;  constructor(&#xA;    private track: Track,&#xA;    private sourceBuffer: SourceBuffer,&#xA;    private logger: NGXLogger,&#xA;  ) {&#xA;&#xA;    this.chunksTotal = Math.ceil(this.track.duration / CHUNK_DURATION_SEC);&#xA;    this.chunkSize = Math.ceil(this.track.fileSize / this.chunksTotal);&#xA;&#xA;    this.requested = [];&#xA;    for (let i = 0; i &lt; this.chunksTotal; i&#x2B;&#x2B;) {&#xA;      this.requested[i] = false;&#xA;    }&#xA;&#xA;    this.url = `${environment.apiBaseUrl}/api/tracks/${this.track.id}/play`;&#xA;  }&#xA;&#xA;  /**&#xA;   * Fetch the first chunk.&#xA;   */&#xA;  public begin() {&#xA;    this.maybeFetchChunk(0);&#xA;  }&#xA;&#xA;  /**&#xA;   * Handler for the "timeupdate" event. Checks if the next chunk should be fetched.&#xA;   *&#xA;   * @param currentTime&#xA;   *  The current time of the track which is currently played.&#xA;   */&#xA;  public handleOnTimeUpdate(currentTime: number) {&#xA;&#xA;    const nextChunkIndex = Math.floor(currentTime / CHUNK_DURATION_SEC) &#x2B; 1;&#xA;    const hasAllChunks = this.requested.every(val => !!val);&#xA;&#xA;    if (nextChunkIndex === (this.chunksTotal - 1) &amp;&amp; hasAllChunks) {&#xA;      this.logger.debug(&#x27;Last chunk. Calling mediaSource.endOfStream();&#x27;);&#xA;      return;&#xA;    }&#xA;&#xA;    if (this.requested[nextChunkIndex] === true) {&#xA;      return;&#xA;    }&#xA;&#xA;    if (currentTime &lt; CHUNK_DURATION_SEC * (nextChunkIndex - 1 &#x2B; 0.25)) {&#xA;      return;&#xA;    }&#xA;&#xA;    this.maybeFetchChunk(nextChunkIndex);&#xA;  }&#xA;&#xA;  /**&#xA;   * Fetches the chunk if it hasn&#x27;t been requested yet. After the request finished, the returned&#xA;   * chunk gets appended to the SourceBuffer-instance.&#xA;   *&#xA;   * @param chunkIndex&#xA;   *  The chunk to fetch.&#xA;   */&#xA;  private maybeFetchChunk(chunkIndex: number) {&#xA;&#xA;    const start = chunkIndex * this.chunkSize;&#xA;    const end = start &#x2B; this.chunkSize - 1;&#xA;&#xA;    if (this.requested[chunkIndex] == true) {&#xA;      return;&#xA;    }&#xA;&#xA;    this.requested[chunkIndex] = true;&#xA;&#xA;    if ((end - start) == 0) {&#xA;      this.logger.warn(&#x27;Nothing to fetch.&#x27;);&#xA;      return;&#xA;    }&#xA;&#xA;    const totalKb = ((end - start) / 1000).toFixed(2);&#xA;    this.logger.debug(`Starting to fetch bytes ${start} to ${end} (total ${totalKb} kB). Chunk ${chunkIndex &#x2B; 1} of ${this.chunksTotal}`);&#xA;&#xA;    const xhr = new XMLHttpRequest();&#xA;    xhr.open(&#x27;get&#x27;, this.url);&#xA;    xhr.setRequestHeader(&#x27;Authorization&#x27;, `Bearer ${AuthenticationService.getJwtToken()}`);&#xA;    xhr.setRequestHeader(&#x27;Range&#x27;, &#x27;bytes=&#x27; &#x2B; start &#x2B; &#x27;-&#x27; &#x2B; end);&#xA;    xhr.responseType = &#x27;arraybuffer&#x27;;&#xA;    xhr.onload = () => {&#xA;      this.logger.debug(`Range ${start} to ${end} fetched`);&#xA;      this.logger.debug(`Requested size:        ${end - start &#x2B; 1}`);&#xA;      this.logger.debug(`Fetched size:          ${xhr.response.byteLength}`);&#xA;      this.logger.debug(&#x27;Appending chunk to SourceBuffer.&#x27;);&#xA;      this.sourceBuffer.appendBuffer(xhr.response);&#xA;    };&#xA;    xhr.send();&#xA;  };&#xA;&#xA;}&#xA;&#xA;export enum StreamStatus {&#xA;  NOT_INITIALIZED,&#xA;  INITIALIZING,&#xA;  PLAYING,&#xA;  SEEKING,&#xA;  PAUSED,&#xA;  STOPPED,&#xA;  ERROR&#xA;}&#xA;&#xA;export class PlayerState {&#xA;  status: StreamStatus = StreamStatus.NOT_INITIALIZED;&#xA;}&#xA;&#xA;&#xA;/**&#xA; *&#xA; */&#xA;@Injectable({&#xA;  providedIn: &#x27;root&#x27;&#xA;})&#xA;export class MediaSourcePlayerService {&#xA;&#xA;  public track: Track;&#xA;&#xA;  private mediaSource: MediaSource;&#xA;&#xA;  private sourceBuffer: SourceBuffer;&#xA;&#xA;  private audioObj: HTMLAudioElement;&#xA;&#xA;  private chunksLoader: ChunksLoader;&#xA;&#xA;  private state: PlayerState = new PlayerState();&#xA;&#xA;  private state$ = new BehaviorSubject<playerstate>(this.state);&#xA;&#xA;  public stateChange = this.state$.asObservable();&#xA;&#xA;  private currentTime$ = new BehaviorSubject<number>(null);&#xA;&#xA;  public currentTimeChange = this.currentTime$.asObservable();&#xA;&#xA;  constructor(&#xA;    private httpClient: HttpClient,&#xA;    private logger: NGXLogger&#xA;  ) {&#xA;  }&#xA;&#xA;  get canPlay() {&#xA;    const state = this.state$.getValue();&#xA;    const status = state.status;&#xA;    return status == StreamStatus.PAUSED;&#xA;  }&#xA;&#xA;  get canPause() {&#xA;    const state = this.state$.getValue();&#xA;    const status = state.status;&#xA;    return status == StreamStatus.PLAYING || status == StreamStatus.SEEKING;&#xA;  }&#xA;&#xA;  public playTrack(track: Track) {&#xA;    this.logger.debug(&#x27;playTrack&#x27;);&#xA;    this.track = track;&#xA;    this.startPlayingFrom(0);&#xA;  }&#xA;&#xA;  public play() {&#xA;    this.logger.debug(&#x27;play()&#x27;);&#xA;    this.audioObj.play().then();&#xA;  }&#xA;&#xA;  public pause() {&#xA;    this.logger.debug(&#x27;pause()&#x27;);&#xA;    this.audioObj.pause();&#xA;  }&#xA;&#xA;  public stop() {&#xA;    this.logger.debug(&#x27;stop()&#x27;);&#xA;    this.audioObj.pause();&#xA;  }&#xA;&#xA;  public seek(seconds: number) {&#xA;    this.logger.debug(&#x27;seek()&#x27;);&#xA;    this.audioObj.currentTime = seconds;&#xA;  }&#xA;&#xA;  private startPlayingFrom(seconds: number) {&#xA;    this.logger.info(`Start playing from ${seconds.toFixed(2)} seconds`);&#xA;    this.mediaSource = new MediaSource();&#xA;    this.mediaSource.addEventListener(&#x27;sourceopen&#x27;, this.onSourceOpen);&#xA;&#xA;    this.audioObj = document.createElement(&#x27;audio&#x27;);&#xA;    this.addEvents(this.audioObj, AUDIO_EVENTS, this.handleEvent);&#xA;    this.audioObj.src = URL.createObjectURL(this.mediaSource);&#xA;&#xA;    this.audioObj.play().then();&#xA;  }&#xA;&#xA;  private onSourceOpen = () => {&#xA;&#xA;    this.logger.debug(&#x27;onSourceOpen&#x27;);&#xA;&#xA;    this.mediaSource.removeEventListener(&#x27;sourceopen&#x27;, this.onSourceOpen);&#xA;    this.mediaSource.duration = this.track.duration;&#xA;&#xA;    this.sourceBuffer = this.mediaSource.addSourceBuffer(&#x27;audio/mp4; codecs="mp4a.40.2"&#x27;);&#xA;    // this.sourceBuffer = this.mediaSource.addSourceBuffer(&#x27;audio/mpeg&#x27;);&#xA;&#xA;    this.chunksLoader = new ChunksLoader(&#xA;      this.track,&#xA;      this.sourceBuffer,&#xA;      this.logger&#xA;    );&#xA;&#xA;    this.chunksLoader.begin();&#xA;  };&#xA;&#xA;  private handleEvent = (e) => {&#xA;&#xA;    const currentTime = this.audioObj.currentTime.toFixed(2);&#xA;    const totalDuration = this.track.duration.toFixed(2);&#xA;    this.logger.warn(`MediaSource event: ${e.type} (${currentTime} of ${totalDuration} sec)`);&#xA;&#xA;    this.currentTime$.next(this.audioObj.currentTime);&#xA;&#xA;    const currentStatus = this.state$.getValue();&#xA;&#xA;    switch (e.type) {&#xA;      case &#x27;playing&#x27;:&#xA;        currentStatus.status = StreamStatus.PLAYING;&#xA;        this.state$.next(currentStatus);&#xA;        break;&#xA;      case &#x27;pause&#x27;:&#xA;        currentStatus.status = StreamStatus.PAUSED;&#xA;        this.state$.next(currentStatus);&#xA;        break;&#xA;      case &#x27;timeupdate&#x27;:&#xA;        this.chunksLoader.handleOnTimeUpdate(this.audioObj.currentTime);&#xA;        break;&#xA;      case &#x27;seeking&#x27;:&#xA;        currentStatus.status = StreamStatus.SEEKING;&#xA;        this.state$.next(currentStatus);&#xA;        if (this.mediaSource.readyState == &#x27;open&#x27;) {&#xA;          this.sourceBuffer.abort();&#xA;        }&#xA;        this.chunksLoader.handleOnTimeUpdate(this.audioObj.currentTime);&#xA;        break;&#xA;    }&#xA;  };&#xA;&#xA;  private addEvents(obj, events, handler) {&#xA;    events.forEach(event => obj.addEventListener(event, handler));&#xA;  }&#xA;&#xA;}&#xA;</number></playerstate>

    &#xA;

    Running it will give me the following output :

    &#xA;

    enter image description here

    &#xA;

    &#xA;

    Apologies for the screenshot but it's not possible to just copy the output without all the stack traces in Chrome.

    &#xA;

    &#xA;

    What I also tried was following this example and call sourceBuffer.abort() but that didn't work. It looks more like a hack that used to work years ago but it's still referenced in the docs (see "Example" -> "You can see something similar in action in Nick Desaulnier's bufferWhenNeeded demo ..").

    &#xA;

    case &#x27;seeking&#x27;:&#xA;  currentStatus.status = StreamStatus.SEEKING;&#xA;  this.state$.next(currentStatus);        &#xA;  if (this.mediaSource.readyState === &#x27;open&#x27;) {&#xA;    this.sourceBuffer.abort();&#xA;  } &#xA;  break;&#xA;

    &#xA;

    Trying with MP3

    &#xA;

    I have tested the above code under Chrome by converting tracks to MP3 :

    &#xA;

    ffmpeg -i input.mp3 -acodec aac -b:a 256k -f mp3 output.mp3&#xA;

    &#xA;

    and creating a SourceBuffer using audio/mpeg as type :

    &#xA;

    this.mediaSource.addSourceBuffer(&#x27;audio/mpeg&#x27;)&#xA;

    &#xA;

    I have the same problem when seeking.

    &#xA;

    The issue wihout seeking

    &#xA;

    The above code has another issue :

    &#xA;

    After two minutes of playing, the audio playback starts to stutter and comes to a halt prematurely. So, the audio plays up to a point and then it stops without any obvious reason.

    &#xA;

    For whatever reason there is another canplay and playing event. A few seconds after, the audio simply stops..

    &#xA;

    enter image description here

    &#xA;