FFmpeg: Comment mettre des données multimédia codées d'un conteneur à l'autre avec des réencodage?
-
30-09-2019 - |
Question
Ainsi, par exemple: je file.mp3, je sais que mon format désiré peut jouer son avec sortie vidéo (par exemple FLV) alors comment mettre des données mp3 codées en utilisant ffmpeg du contenant mp3 flv (où obtenir des articles / des exemples de code à ce sujet)?
Je veux dire pas de cmd mais de C ++ en utilisant ffmpeg comme bibliothèque. (Voir les tags)
La solution
Voici la commande pour convertir le fichier .mp3 à .flv (qui ne dispose pas de données vidéo).
ffmpeg -i test.mp3 -AB 32k -acodec libmp3lame -ac 1 -ar 44100 audio.flv.
Vous pouvez exécuter cette commande à partir de votre programme.
Si vous avez besoin d'aide pour installer et à utiliser ffmpeg vous pouvez aller sur leur site:
Merci,
Mahmud
Autres conseils
Avez-vous envisagé juste courir ffmpeg d'un popen () / système () appel de c ++?
Il est beaucoup plus facile que la mise en place de la bibliothèque ffmpeg, il est trivial de multithread (pas vraiment un problème dans l'exemple) et vous libère de toute liaison et LGPL questions dll-enfer.
Voici ce que vous voulez faire:
AVFormatContext *ptrFormatContext;
int i, videoStream, audioStream;
AVCodecContext *ptrCodecCtxt;
AVCodec *ptrCodec;
AVFrame *ptrFrame;
AVPacket ptrPacket;
int frameFinished;
float aspect_ratio;
AVCodecContext *aCodecCtx;
AVCodec *aCodec;
AVCodecContext *aTargetCodecCtxt;
AVCodecContext *vTargetCodecCtxt;
AVCodec *aTargetCodec;
AVCodec *vTargetCodec;
AVSampleFormat ptrSampleFormats[2] = {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32};
audioStream = videoStream = -1;
av_register_all();
avcodec_register_all();
ptrFormatContext = avformat_alloc_context();
if(avformat_open_input(&ptrFormatContext, filename, NULL, NULL) != 0 )
{
qDebug("Error opening the input");
exit(-1);
}
if(av_find_stream_info( ptrFormatContext) < 0)
{
qDebug("Could not find any stream info");
exit(-2);
}
dump_format(ptrFormatContext, 0, filename, (int) NULL);
for(i=0; i<ptrFormatContext->nb_streams; i++)
{
switch(ptrFormatContext->streams[i]->codec->codec_type)
{
case AVMEDIA_TYPE_VIDEO:
{
if(videoStream < 0) videoStream = i;
break;
}
case AVMEDIA_TYPE_AUDIO:
{
if(audioStream < 0) audioStream = i;
}
}
}
if(audioStream == -1)
{
qDebug("Could not find any audio stream");
exit(-3);
}
if(videoStream == -1)
{
qDebug("Could not find any video stream");
exit(-4);
}
aCodecCtx = ptrFormatContext->streams[audioStream]->codec;
if( (aCodec = avcodec_find_decoder(aCodecCtx->codec_id)) == NULL)
{
qDebug("Could not find the audio decoder");
exit(-5);
}
if( (avcodec_open(aCodecCtx, aCodec)) != 0 )
{
qDebug("Could not open the audio decoder");
exit(-6);
}
ptrCodecCtxt = ptrFormatContext->streams[videoStream]->codec;
if( (ptrCodec = avcodec_find_decoder(ptrCodecCtxt->codec_id)) == NULL )
{
qDebug("Could not find the video decoder");
exit(-7);
}
if((avcodec_open(ptrCodecCtxt, ptrCodec)) != 0)
{
qDebug("Could not find any video stream");
exit(-8);
}
Alors d'autres choses, plus d'importance si vous ne voulez pas réencoder ...
ptrFrame = avcodec_alloc_frame();
while(av_read_frame(ptrFormatContext,&ptrPacket) >= 0)
{
if(ptrPacket.stream_index == videoStream)
{
//do stuff with the package, for eg transcribe it into another output stream..
}
else if (ptrPacket.stream_index == audioStream)
{
//do stuff with the package, for eg transcribe it into another output stream..
}
}
Hope qui est utile. Le code est cependant seulement un extrait et ne fonctionnera pas sur lui-même, mais ça va vous aider à obtenir l'idée.
ffmpeg -i file.mp3 -acodec copy output.flv