
Recherche avancée
Autres articles (47)
-
Publier sur MédiaSpip
13 juin 2013Puis-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 -
Librairies et logiciels spécifiques aux médias
10 décembre 2010, parPour un fonctionnement correct et optimal, plusieurs choses sont à prendre en considération.
Il est important, après avoir installé apache2, mysql et php5, d’installer d’autres logiciels nécessaires dont les installations sont décrites dans les liens afférants. Un ensemble de librairies multimedias (x264, libtheora, libvpx) utilisées pour l’encodage et le décodage des vidéos et sons afin de supporter le plus grand nombre de fichiers possibles. Cf. : ce tutoriel ; FFMpeg avec le maximum de décodeurs et (...) -
List of compatible distributions
26 avril 2011, parThe table below is the list of Linux distributions compatible with the automated installation script of MediaSPIP. Distribution nameVersion nameVersion number Debian Squeeze 6.x.x Debian Weezy 7.x.x Debian Jessie 8.x.x Ubuntu The Precise Pangolin 12.04 LTS Ubuntu The Trusty Tahr 14.04
If you want to help us improve this list, you can provide us access to a machine whose distribution is not mentioned above or send the necessary fixes to add (...)
Sur d’autres sites (7967)
-
Decoding pcm_s16le with FFMPEG ?
9 juin, par Davide Caresiai have a problem decoding a wav file using ffmpeg. I'm new to it and i'm not quite used to it.


In my application i have to input the audio file and get an array of samples to work on.
I used ffmpeg to create a function that gets in input the path of the file, the position in time where to start to output the samples and the lenght of the chunk to decode in seconds.


When I try to decode the file harp.wav everything runs fine, and I can plot the samples as in the image plot-harp.png


The file is a WAV file encoded as : pcm_u8, 11025 Hz, 1 channels, u8, 88 kb/s


The problems comes when i try to decode the file demo-unprocessed.wav.
It outputs a series of samples that has no sense. It outputs a serie of samples plotted as the image graph1-demo.jpg shows.


The file is a WAV file encoded as : pcm_s16le, 44100 Hz, 1 channels, s16, 705 kb/s


IDK where the problem in my code is, I already checked the code before and after the decoding with FFMPEG, and it works absolutely fine.


Here is the code for the dataReader.cpp :


/* Start by including the necessary */
#include "dataReader.h"
#include <cstdlib>
#include <iostream>
#include <fstream>

#ifdef __cplusplus
extern "C" {
#endif
 #include <libavcodec></libavcodec>avcodec.h> 
 #include <libavformat></libavformat>avformat.h>
 #include <libavutil></libavutil>avutil.h>
#ifdef __cplusplus 
}
#endif

using namespace std;

/* initialization function for audioChunk */
audioChunk::audioChunk(){
 data=NULL;
 size=0;
 bitrate=0;
}

/* function to get back chunk lenght in seconds */
int audioChunk::getTimeLenght(){
 return size/bitrate;
}

/* initialization function for audioChunk_dNorm */
audioChunk_dNorm::audioChunk_dNorm(){
 data=NULL;
 size=0;
 bitrate=0;
}

/* function to get back chunk lenght in seconds */
int audioChunk_dNorm::getTimeLenght(){
 return size/bitrate;
}

/* function to normalize audioChunk into audioChunk_dNorm */
void audioChunk_dNorm::fillAudioChunk(audioChunk* cnk){

 size=cnk->size;
 bitrate=cnk->bitrate;

 double min=cnk->data[0];
 double max=cnk->data[0];

 for(int i=0;isize;i++){
 if(*(cnk->data+i)>max) max=*(cnk->data+i);
 else if(*(cnk->data+i)data+i);
 }

 data=new double[size];

 for(int i=0;i/data[i]=cnk->data[i]+256*data[i+1];
 if(data[i]!=255) data[i]=2*((cnk->data[i])-(max-min)/2)/(max-min);
 else data[i]=0;
 }
 cout<<"bitrate "<* inizialize audioChunk */
 audioChunk output;

 /* Check input times */
 if((start_time<0)||(lenght<0)) {
 cout<<"Input times should be positive";
 return output;
 }

 /* Start FFmpeg */
 av_register_all();

 /* Initialize the frame to read the data and verify memory allocation */
 AVFrame* frame = av_frame_alloc();
 if (!frame)
 {
 cout << "Error allocating the frame" << endl;
 return output;
 }

 /* Initialization of the Context, to open the file */
 AVFormatContext* formatContext = NULL;
 /* Opening the file, and check if it has opened */
 if (avformat_open_input(&formatContext, path_name, NULL, NULL) != 0)
 {
 av_frame_free(&frame);
 cout << "Error opening the file" << endl;
 return output;
 }

 /* Find the stream info, if not found, exit */
 if (avformat_find_stream_info(formatContext, NULL) < 0)
 {
 av_frame_free(&frame);
 avformat_close_input(&formatContext);
 cout << "Error finding the stream info" << endl;
 return output;
 }

 /* Check inputs to verify time input */
 if(start_time>(formatContext->duration/1000000)){
 cout<< "Error, start_time is over file duration"<* Chunk = number of samples to output */
 long long int chunk = ((formatContext->bit_rate)*lenght/8);
 /* Start = address of sample where start to read */
 long long int start = ((formatContext->bit_rate)*start_time/8);
 /* Tot_sampl = number of the samples in the file */
 long long int tot_sampl = (formatContext->bit_rate)*(formatContext->duration)/8000000;

 /* Set the lenght of chunk to avoid segfault and to read all the file */
 if (start+chunk>tot_sampl) {chunk = tot_sampl-start;}
 if (lenght==0) {start = 0; chunk = tot_sampl;}

 /* initialize the array to output */
 output.data = new unsigned char[chunk];
 output.bitrate = formatContext->bit_rate;
 output.size=chunk;

 av_dump_format(formatContext,0,NULL,0);
 cout<* Find the audio Stream, if no audio stream are found, clean and exit */
 AVCodec* cdc = NULL;
 int streamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &cdc, 0);
 if (streamIndex < 0)
 {
 av_frame_free(&frame);
 avformat_close_input(&formatContext);
 cout << "Could not find any audio stream in the file" << endl;
 return output;
 }

 /* Open the audio stream to read data in audioStream */
 AVStream* audioStream = formatContext->streams[streamIndex];

 /* Initialize the codec context */
 AVCodecContext* codecContext = audioStream->codec;
 codecContext->codec = cdc;
 /* Open the codec, and verify if it has opened */
 if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
 {
 av_frame_free(&frame);
 avformat_close_input(&formatContext);
 cout << "Couldn't open the context with the decoder" << endl;
 return output;
 }

 /* Initialize buffer to store compressed packets */
 AVPacket readingPacket;
 av_init_packet(&readingPacket);


 int j=0;
 int count = 0; 

 while(av_read_frame(formatContext, &readingPacket)==0){
 if((count+readingPacket.size)>start){
 if(readingPacket.stream_index == audioStream->index){

 AVPacket decodingPacket = readingPacket;

 // Audio packets can have multiple audio frames in a single packet
 while (decodingPacket.size > 0){
 // Try to decode the packet into a frame
 // Some frames rely on multiple packets, so we have to make sure the frame is finished before
 // we can use it
 int gotFrame = 0;
 int result = avcodec_decode_audio4(codecContext, frame, &gotFrame, &decodingPacket);

 count += result;

 if (result >= 0 && gotFrame)
 {
 decodingPacket.size -= result;
 decodingPacket.data += result;
 int a;

 for(int i=0;idata[0][i];

 j++;
 if(j>=chunk) break;
 }

 // We now have a fully decoded audio frame
 }
 else
 {
 decodingPacket.size = 0;
 decodingPacket.data = NULL;
 }
 if(j>=chunk) break;
 }
 } 
 }else count+=readingPacket.size;

 // To prevent memory leak, must free packet.
 av_free_packet(&readingPacket);
 if(j>=chunk) break;
 }

 // Some codecs will cause frames to be buffered up in the decoding process. If the CODEC_CAP_DELAY flag
 // is set, there can be buffered up frames that need to be flushed, so we'll do that
 if (codecContext->codec->capabilities & CODEC_CAP_DELAY)
 {
 av_init_packet(&readingPacket);
 // Decode all the remaining frames in the buffer, until the end is reached
 int gotFrame = 0;
 int a;
 int result=avcodec_decode_audio4(codecContext, frame, &gotFrame, &readingPacket);
 while (result >= 0 && gotFrame)
 {
 // We now have a fully decoded audio frame
 for(int i=0;idata[0][i];

 j++;
 if(j>=chunk) break;
 }
 if(j>=chunk) break;
 }
 }

 // Clean up!
 av_free(frame);
 avcodec_close(codecContext);
 avformat_close_input(&formatContext);

 cout<<"Ended Reading, "<code></fstream></iostream></cstdlib>


Here is the dataReader.h


/* 
 * File: dataReader.h
 * Author: davide
 *
 * Created on 27 luglio 2015, 11.11
 */

#ifndef DATAREADER_H
#define DATAREADER_H

/* function that reads a file and outputs an array of samples
 * @ path_name = the path of the file to read
 * @ start_time = the position where to start the data reading, 0 = start
 * the time is in seconds, it can hold to 10e-6 seconds
 * @ lenght = the lenght of the frame to extract the data, 
 * 0 = read all the file (do not use with big files)
 * if lenght > of file duration, it reads through the end of file.
 * the time is in seconds, it can hold to 10e-6 seconds 
 */

#include 

class audioChunk{
public:
 uint8_t *data;
 unsigned int size;
 int bitrate;
 int getTimeLenght();
 audioChunk();
};

class audioChunk_dNorm{
public:
 double* data;
 unsigned int size;
 int bitrate;
 int getTimeLenght();
 void fillAudioChunk(audioChunk* cnk);
 audioChunk_dNorm();
};

audioChunk readData(const char* path_name, const double start_time, const double lenght);

#endif /* DATAREADER_H */



And finally there is the main.cpp of the application.


/* 
 * File: main.cpp
 * Author: davide
 *
 * Created on 28 luglio 2015, 17.04
 */

#include <cstdlib>
#include "dataReader.h"
#include "transforms.h"
#include "tognuplot.h"
#include <fstream>
#include <iostream>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

 audioChunk *chunk1=new audioChunk;

 audioChunk_dNorm *normChunk1=new audioChunk_dNorm;

 *chunk1=readData("./audio/demo-unprocessed.wav",0,1);

 normChunk1->fillAudioChunk(chunk1);

 ofstream file1;
 file1.open("./file/2wave.txt", std::ofstream::trunc);
 if(file1.is_open()) {
 for(int i=0;isize;i++) {
 int a=chunk1->data[i];
 file1<code></iostream></fstream></cstdlib>


I can't understand why the outputs goes like this. Is it possible that the decoder can't convert the samples (pcm_16le, 16bits) into FFMPEG AVFrame.data, that stores the samples ad uint8_t ? And if it is it is there some way to make FFMPEG work for audio files that stores samples at more than 8 bits ?


The file graph1-demo_good.jpg is how the samples should be, extracted with a working LIBSNDFILE application that I made.


EDIT : Seems like the program can't convert the decoded data, couples of little endian bytes stored in a couple of uint8_t unsigned char, into the destination format (that i set as unsigned char[]), because it stores the bits as little-endian 16 bytes. So the data into audioChunk.data is right, but I have to read it not as an unsigned char, but as a couple of little-endian bytes.


-
How to Use Analytics & Reports for Marketing, Sales & More
28 septembre 2023, par Erin — Analytics Tips -
ffmpeg command to add moving text watermark to video [closed]
13 octobre 2023, par Imran Khan

// Constants for watermark movement, direction change intervals, fade intervals, and overlap duration
 const MOVE_SPEED = 3;
 const DIRECTION_CHANGE_MIN = 3000;
 const DIRECTION_CHANGE_MAX = 6000;
 const FADE_INTERVAL_MIN = 10000;
 const FADE_INTERVAL_MAX = 20000;
 const OVERLAP_DURATION = 2000;

 // Get references to the video container and watermarks
 const container = document.querySelector('.video-container');
 const watermark1 = document.getElementById('watermark1');
 const watermark2 = document.getElementById('watermark2');

 // Helper function to get a random integer between min and max (inclusive)
 function getRandomInt(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
 }

 // Helper function to get a random direction (either 1 or -1)
 function getRandomDirection() {
 return Math.random() > 0.5 ? 1 : -1;
 }

 // Set the initial position of the watermark inside the video container
 function setInitialPosition(watermark) {
 const x = getRandomInt(0, container.offsetWidth - watermark.offsetWidth);
 const y = getRandomInt(0, container.offsetHeight - watermark.offsetHeight);
 watermark.style.left = `${x}px`;
 watermark.style.top = `${y}px`;
 watermark.style.opacity = 1;
 }

 // Function to handle continuous movement of the watermark
 function continuousMove(watermark) {
 let dx = getRandomDirection() * MOVE_SPEED;
 let dy = getRandomDirection() * MOVE_SPEED;

 // Inner function to handle the actual movement logic
 function move() {
 let x = parseInt(watermark.style.left || 0) + dx;
 let y = parseInt(watermark.style.top || 0) + dy;

 // Check boundaries and reverse direction if necessary
 if (x < 0 || x > container.offsetWidth - watermark.offsetWidth) {
 dx = -dx;
 }
 if (y < 0 || y > container.offsetHeight - watermark.offsetHeight) {
 dy = -dy;
 }

 // Apply the new position
 watermark.style.left = `${x}px`;
 watermark.style.top = `${y}px`;

 // Continue moving
 setTimeout(move, 100);
 }

 move();

 // Change direction at random intervals
 setInterval(() => {
 const randomChoice = Math.random();
 if (randomChoice < 0.33) {
 dx = getRandomDirection() * MOVE_SPEED;
 dy = 0;
 } else if (randomChoice < 0.66) {
 dy = getRandomDirection() * MOVE_SPEED;
 dx = 0;
 } else {
 dx = getRandomDirection() * MOVE_SPEED;
 dy = getRandomDirection() * MOVE_SPEED;
 }
 }, getRandomInt(DIRECTION_CHANGE_MIN, DIRECTION_CHANGE_MAX));
 }

 // Handle the fading out of the old watermark and fading in of the new watermark
 function fadeOutAndIn(oldWatermark, newWatermark) {
 setTimeout(() => {
 setInitialPosition(newWatermark);
 newWatermark.style.opacity = 1;
 }, 0);

 setTimeout(() => {
 oldWatermark.style.opacity = 0;
 }, OVERLAP_DURATION);

 // Continue the cycle
 setTimeout(() => fadeOutAndIn(newWatermark, oldWatermark), getRandomInt(FADE_INTERVAL_MIN, FADE_INTERVAL_MAX));
 }

 // Initialize the watermarks
 setInitialPosition(watermark1);
 continuousMove(watermark1);
 setTimeout(() => fadeOutAndIn(watermark1, watermark2), getRandomInt(FADE_INTERVAL_MIN, FADE_INTERVAL_MAX));
 continuousMove(watermark2);



body, html {
 height: 100%;
 margin: 0;
 font-family: Arial, sans-serif;
 display: flex;
 justify-content: center;
 align-items: center;
 background-color: #eee;
 }

 .video-container {
 width: 50vw;
 height: 50vh;
 background-color: black;
 position: relative;
 overflow: hidden;
 }

 .watermark {
 font-size: 22px;
 position: absolute;
 color: white;
 opacity: 0;
 transition: opacity 2s;
 }





 
 
 


 <div class="video-container">
 <span class="watermark">watermark</span>
 <span class="watermark">watermark</span>
 </div>
 









I am trying to achieve an animation effect using ffmpeg. I am adding text watermark to an input video and animate the text diagonally, horizontally or vertically changed randomly. Here is what I have achieved so far.


ffmpeg -i video.mp4 -c:v libx264 -preset veryfast -crf 25 -tune zerolatency -vendor ap10 -pix_fmt yuv420p -filter:v "drawtext=fontfile=./fonts/Roboto/Roboto-Light.ttf:text='Watermark':fontcolor=white:alpha=0.5:fontsize=60:y=h/10*mod(t\,10):x=w/10*mod(t\,10):enable=1" -c:a copy watermark.mp4


Here is what I want it to work.


Initial Position :
The watermark randomly placed in the video the first time they appear.


Continuous Movement :
The watermark continuously moves within the video.
The direction and speed of the watermark's movement are random. It can move diagonally, purely horizontally, or purely vertically.
When the watermark reaches the boundaries of the video, it bounces back, changing its direction.


Direction Change :
During its continuous movement, the watermark will suddenly change its direction at random intervals between 3 to 6 seconds.
When changing direction, the watermark can randomly determined move diagonally, purely horizontally, or purely vertically.


Fade In and Out :
Every 10 to 20 seconds (randomly determined), the current watermark begins to fade out.
As the old watermark starts to fade out, a new watermark fades in at a random position, ensuring that there's always a visible watermark on the screen.
These two watermarks (the fading old one and the emerging new one) overlap on the screen for a duration of 2 seconds, after which the old watermark completely disappears.
These patterns and characteristics together provide a dynamic, constantly moving, and changing watermark for the video


To achieve the result I think we can use the
drawtext
multiple times. I have attached the HTML and JavaScript variant just for the reference to understand the result but I am trying to do this using ffmpeg.