
Recherche avancée
Autres articles (99)
-
Personnaliser en ajoutant son logo, sa bannière ou son image de fond
5 septembre 2013, parCertains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;
-
Les autorisations surchargées par les plugins
27 avril 2010, parMediaspip core
autoriser_auteur_modifier() afin que les visiteurs soient capables de modifier leurs informations sur la page d’auteurs -
Support de tous types de médias
10 avril 2011Contrairement à 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) (...)
Sur d’autres sites (7574)
-
How to new & delete AVPacket ?
18 octobre 2020, par PatrickSCLinI'm working with a FFmpeg project right now, I have to store the data of
AVPacket
which fromav_read_frame
, and fill data to newAVPacket
for following decoding.

Here is my problem : when I try to new & free an
AVPacket
, memory leaks always happen.

I am just doing a simple testing :


for(;;) {
 AVPacket pkt;
 av_new_packet(&pkt, 1000);
 av_init_packet(&pkt);
 av_free_packet(&pkt);
}



What am I doing wrong ?


-
How to play and seek fragmented MP4 audio using MSE SourceBuffer ?
29 juin 2024, par Stefan FalkNote :




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.




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).


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 :

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



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

-----------------------------------------
| chunk 1 | chunk 2 | ... | chunk n |
-----------------------------------------



How audio files are converted to fMP4


ffmpeg -i input.mp3 -acodec aac -b:a 256k -f mp4 \
 -movflags faststart+frag_every_frame+empty_moov+default_base_moof \
 output.mp4



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


How chunks are appended


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.


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.

The code


/* Desired chunk duration in seconds. */
const CHUNK_DURATION_SEC = 20;

const AUDIO_EVENTS = [
 'ended',
 'error',
 'play',
 'playing',
 'seeking',
 'seeked',
 'pause',
 'timeupdate',
 'canplay',
 'loadedmetadata',
 'loadstart',
 'updateend',
];


class ChunksLoader {

 /** The total number of chunks for the track. */
 public readonly chunksTotal: number;

 /** The length of one chunk in bytes */
 public readonly chunkSize: number;

 /** Keeps track of requested chunks. */
 private readonly requested: boolean[];

 /** URL of endpoint for fetching audio chunks. */
 private readonly url: string;

 constructor(
 private track: Track,
 private sourceBuffer: SourceBuffer,
 private logger: NGXLogger,
 ) {

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

 this.requested = [];
 for (let i = 0; i < this.chunksTotal; i++) {
 this.requested[i] = false;
 }

 this.url = `${environment.apiBaseUrl}/api/tracks/${this.track.id}/play`;
 }

 /**
 * Fetch the first chunk.
 */
 public begin() {
 this.maybeFetchChunk(0);
 }

 /**
 * Handler for the "timeupdate" event. Checks if the next chunk should be fetched.
 *
 * @param currentTime
 * The current time of the track which is currently played.
 */
 public handleOnTimeUpdate(currentTime: number) {

 const nextChunkIndex = Math.floor(currentTime / CHUNK_DURATION_SEC) + 1;
 const hasAllChunks = this.requested.every(val => !!val);

 if (nextChunkIndex === (this.chunksTotal - 1) && hasAllChunks) {
 this.logger.debug('Last chunk. Calling mediaSource.endOfStream();');
 return;
 }

 if (this.requested[nextChunkIndex] === true) {
 return;
 }

 if (currentTime < CHUNK_DURATION_SEC * (nextChunkIndex - 1 + 0.25)) {
 return;
 }

 this.maybeFetchChunk(nextChunkIndex);
 }

 /**
 * Fetches the chunk if it hasn't been requested yet. After the request finished, the returned
 * chunk gets appended to the SourceBuffer-instance.
 *
 * @param chunkIndex
 * The chunk to fetch.
 */
 private maybeFetchChunk(chunkIndex: number) {

 const start = chunkIndex * this.chunkSize;
 const end = start + this.chunkSize - 1;

 if (this.requested[chunkIndex] == true) {
 return;
 }

 this.requested[chunkIndex] = true;

 if ((end - start) == 0) {
 this.logger.warn('Nothing to fetch.');
 return;
 }

 const totalKb = ((end - start) / 1000).toFixed(2);
 this.logger.debug(`Starting to fetch bytes ${start} to ${end} (total ${totalKb} kB). Chunk ${chunkIndex + 1} of ${this.chunksTotal}`);

 const xhr = new XMLHttpRequest();
 xhr.open('get', this.url);
 xhr.setRequestHeader('Authorization', `Bearer ${AuthenticationService.getJwtToken()}`);
 xhr.setRequestHeader('Range', 'bytes=' + start + '-' + end);
 xhr.responseType = 'arraybuffer';
 xhr.onload = () => {
 this.logger.debug(`Range ${start} to ${end} fetched`);
 this.logger.debug(`Requested size: ${end - start + 1}`);
 this.logger.debug(`Fetched size: ${xhr.response.byteLength}`);
 this.logger.debug('Appending chunk to SourceBuffer.');
 this.sourceBuffer.appendBuffer(xhr.response);
 };
 xhr.send();
 };

}

export enum StreamStatus {
 NOT_INITIALIZED,
 INITIALIZING,
 PLAYING,
 SEEKING,
 PAUSED,
 STOPPED,
 ERROR
}

export class PlayerState {
 status: StreamStatus = StreamStatus.NOT_INITIALIZED;
}


/**
 *
 */
@Injectable({
 providedIn: 'root'
})
export class MediaSourcePlayerService {

 public track: Track;

 private mediaSource: MediaSource;

 private sourceBuffer: SourceBuffer;

 private audioObj: HTMLAudioElement;

 private chunksLoader: ChunksLoader;

 private state: PlayerState = new PlayerState();

 private state$ = new BehaviorSubject<playerstate>(this.state);

 public stateChange = this.state$.asObservable();

 private currentTime$ = new BehaviorSubject<number>(null);

 public currentTimeChange = this.currentTime$.asObservable();

 constructor(
 private httpClient: HttpClient,
 private logger: NGXLogger
 ) {
 }

 get canPlay() {
 const state = this.state$.getValue();
 const status = state.status;
 return status == StreamStatus.PAUSED;
 }

 get canPause() {
 const state = this.state$.getValue();
 const status = state.status;
 return status == StreamStatus.PLAYING || status == StreamStatus.SEEKING;
 }

 public playTrack(track: Track) {
 this.logger.debug('playTrack');
 this.track = track;
 this.startPlayingFrom(0);
 }

 public play() {
 this.logger.debug('play()');
 this.audioObj.play().then();
 }

 public pause() {
 this.logger.debug('pause()');
 this.audioObj.pause();
 }

 public stop() {
 this.logger.debug('stop()');
 this.audioObj.pause();
 }

 public seek(seconds: number) {
 this.logger.debug('seek()');
 this.audioObj.currentTime = seconds;
 }

 private startPlayingFrom(seconds: number) {
 this.logger.info(`Start playing from ${seconds.toFixed(2)} seconds`);
 this.mediaSource = new MediaSource();
 this.mediaSource.addEventListener('sourceopen', this.onSourceOpen);

 this.audioObj = document.createElement('audio');
 this.addEvents(this.audioObj, AUDIO_EVENTS, this.handleEvent);
 this.audioObj.src = URL.createObjectURL(this.mediaSource);

 this.audioObj.play().then();
 }

 private onSourceOpen = () => {

 this.logger.debug('onSourceOpen');

 this.mediaSource.removeEventListener('sourceopen', this.onSourceOpen);
 this.mediaSource.duration = this.track.duration;

 this.sourceBuffer = this.mediaSource.addSourceBuffer('audio/mp4; codecs="mp4a.40.2"');
 // this.sourceBuffer = this.mediaSource.addSourceBuffer('audio/mpeg');

 this.chunksLoader = new ChunksLoader(
 this.track,
 this.sourceBuffer,
 this.logger
 );

 this.chunksLoader.begin();
 };

 private handleEvent = (e) => {

 const currentTime = this.audioObj.currentTime.toFixed(2);
 const totalDuration = this.track.duration.toFixed(2);
 this.logger.warn(`MediaSource event: ${e.type} (${currentTime} of ${totalDuration} sec)`);

 this.currentTime$.next(this.audioObj.currentTime);

 const currentStatus = this.state$.getValue();

 switch (e.type) {
 case 'playing':
 currentStatus.status = StreamStatus.PLAYING;
 this.state$.next(currentStatus);
 break;
 case 'pause':
 currentStatus.status = StreamStatus.PAUSED;
 this.state$.next(currentStatus);
 break;
 case 'timeupdate':
 this.chunksLoader.handleOnTimeUpdate(this.audioObj.currentTime);
 break;
 case 'seeking':
 currentStatus.status = StreamStatus.SEEKING;
 this.state$.next(currentStatus);
 if (this.mediaSource.readyState == 'open') {
 this.sourceBuffer.abort();
 }
 this.chunksLoader.handleOnTimeUpdate(this.audioObj.currentTime);
 break;
 }
 };

 private addEvents(obj, events, handler) {
 events.forEach(event => obj.addEventListener(event, handler));
 }

}
</number></playerstate>


Running it will give me the following output :






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




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 ..").

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



Trying with MP3


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


ffmpeg -i input.mp3 -acodec aac -b:a 256k -f mp3 output.mp3



and creating a
SourceBuffer
usingaudio/mpeg
as type :

this.mediaSource.addSourceBuffer('audio/mpeg')



I have the same problem when seeking.


The issue wihout seeking


The above code has another issue :


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.


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



-
Audio Video Mixing - Sync issue in Android with FFMPEG, Media Codec in different devices
24 novembre 2020, par khushbuI 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 {

 val progressDialog = ProgressDialog(requireContext())
 Thread {
 requireActivity().runOnUiThread {
 progressDialog.setMessage("Please wait..")
 progressDialog.show()
 }
 val video_list = ArrayList<string>()
 for (i in videopaths.indices) {
 val file: File = File(videopaths.get(i))
 if (file.exists()) {
 val retriever = MediaMetadataRetriever()
 retriever.setDataSource(requireActivity(), Uri.fromFile(file))
 val hasVideo =
 retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO)
 val isVideo = "yes" == hasVideo
 if (isVideo /*&& file.length() > 1000*/) {
 Log.d("resp", videopaths.get(i))
 video_list.add(videopaths.get(i))
 }
 }
 }
 try {
 val inMovies = arrayOfNulls<movie>(video_list.size)
 for (i in video_list.indices) {
 inMovies[i] = MovieCreator.build(video_list[i])
 }
 val videoTracks: MutableList<track> =
 LinkedList()
 val audioTracks: MutableList<track> =
 LinkedList()
 for (m in inMovies) {
 for (t in m!!.tracks) {
 if (t.handler == "soun") {
 audioTracks.add(t)
 }
 if (t.handler == "vide") {
 videoTracks.add(t)
 }
 }
 }
 val result = Movie()
 if (audioTracks.size > 0) {
 result.addTrack(AppendTrack(*audioTracks.toTypedArray()))
 }
 if (videoTracks.size > 0) {
 result.addTrack(AppendTrack(*videoTracks.toTypedArray()))
 }
 val out = DefaultMp4Builder().build(result)
 var outputFilePath: String? = null
 outputFilePath = Variables.outputfile

 /*if (audio != null) {
 Variables.outputfile
 } else {
 Variables.outputfile2
 }*/

 val fos = FileOutputStream(File(outputFilePath))
 out.writeContainer(fos.channel)
 fos.close()

 requireActivity().runOnUiThread {
 progressDialog.dismiss()

 Merge_withAudio()

 /* if (audio != null) else {
 //Go_To_preview_Activity()
 }*/
 }
 } catch (e: java.lang.Exception) {
 }
 }.start()

 return true
}
</track></track></movie></string>


This will add the selected audio with the recorded video


fun Merge_withAudio() {
 val root = Environment.getExternalStorageDirectory().toString()

 // Uri mediaPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.file_copy);
 //String audio_file =Variables.app_folder+Variables.SelectedAudio_AAC;

 //String filename = "android.resource://" + getPackageName() + "/raw/file_copy.aac";
 val audio_file: String = audioLink!!
 Log.e("Merge ", audio_file)
 val video = "$root/output.mp4"

 val bundle=Bundle()
 bundle.putString("FinalVideo", createdVideo?.absolutePath)

 val merge_video_audio = Merge_Video_Audio(this, bundle, object : AsyncResponse {
 override fun processFinish(output: Bundle?) {

 requireActivity().runOnUiThread {
 finalVideo = bundle.getString("FinalVideo")
 createdVideo = File(finalVideo)

 Log.e("Final Path ", finalVideo)

 createThumb {
 setUpExoPlayer()
 }
 }

 }
 })
 merge_video_audio.doInBackground(audio_file, video, createdVideo?.absolutePath)
}


 public class Merge_Video_Audio extends AsyncTask {

 ProgressDialog progressDialog;
 RecentCompletedVideoFragment context;
 public AsyncResponse delegate = null;


Bundle bundleValue;

String audio,video,output;

public Merge_Video_Audio(RecentCompletedVideoFragment context, Bundle bundle , AsyncResponse delegate ){
 this.context=context;
 this.bundleValue=bundle;
 this.delegate=delegate;
 progressDialog=new ProgressDialog(context.requireContext());
 progressDialog.setMessage("Please Wait...");
}

@Override
protected void onPreExecute() {
 super.onPreExecute();
}

@Override
public String doInBackground(String... strings) {
 try {
 progressDialog.show();
 }catch (Exception e){

 }
 audio=strings[0];
 video=strings[1];
 output=strings[2];

 Log.d("resp",audio+"----"+video+"-----"+output);

 Thread thread = new Thread(runnable);
 thread.start();

 return null;
}


@Override
protected void onPostExecute(String s) {
 super.onPostExecute(s);
 Log.e("On Post Execute ", "True");


}


 public void Go_To_preview_Activity(){

 delegate.processFinish(bundleValue);
 }

 public Track CropAudio(String videopath, Track fullAudio){
 try {

 IsoFile isoFile = new IsoFile(videopath);

 double lengthInSeconds = (double)
 isoFile.getMovieBox().getMovieHeaderBox().getDuration() /
 isoFile.getMovieBox().getMovieHeaderBox().getTimescale();


 Track audioTrack = (Track) fullAudio;


 double startTime1 = 0;
 double endTime1 = lengthInSeconds;


 long currentSample = 0;
 double currentTime = 0;
 double lastTime = -1;
 long startSample1 = -1;
 long endSample1 = -1;


 for (int i = 0; i < audioTrack.getSampleDurations().length; i++) {

 long delta = audioTrack.getSampleDurations()[i];

 if (currentTime > lastTime && currentTime <= startTime1) {
 // current sample is still before the new starttime
 startSample1 = currentSample;
 }
 if (currentTime > lastTime && currentTime <= endTime1) {
 // current sample is after the new start time and still before the new endtime
 endSample1 = currentSample;
 }

 lastTime = currentTime;
 currentTime += (double) delta / (double) audioTrack.getTrackMetaData().getTimescale();
 currentSample++;
 }

 CroppedTrack cropperAacTrack = new CroppedTrack(fullAudio, startSample1, endSample1);

 return cropperAacTrack;

 } catch (IOException e) {
 e.printStackTrace();
 }

 return fullAudio;
}



 public Runnable runnable =new Runnable() {
 @Override
 public void run() {

 try {

 Movie m = MovieCreator.build(video);


 List nuTracks = new ArrayList<>();

 for (Track t : m.getTracks()) {
 if (!"soun".equals(t.getHandler())) {

 Log.e("Track ",t.getName());
 nuTracks.add(t);
 }
 }

 Log.e("Path ",audio.toString());


 try {
 // Track nuAudio = new AACTrackImpl();
 Track nuAudio = new AACTrackImpl(new FileDataSourceImpl(audio));

 Track crop_track = CropAudio(video, nuAudio);

 nuTracks.add(crop_track);

 m.setTracks(nuTracks);

 Container mp4file = new DefaultMp4Builder().build(m);

 FileChannel fc = new FileOutputStream(new File(output)).getChannel();
 mp4file.writeContainer(fc);
 fc.close();

 }catch (FileNotFoundException fnfe){
 fnfe.printStackTrace();
 }catch(IOException ioe){
 ioe.printStackTrace();
 }


 try {

 progressDialog.dismiss();
 }catch (Exception e){
 Log.d("resp",e.toString());

 }finally {
 Go_To_preview_Activity();

 }

 } catch (IOException e) {
 e.printStackTrace();
 Log.d("resp",e.toString());

 }

 }

 };

 }



This solution is also not working in all the devices.


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