
Recherche avancée
Médias (91)
-
Spitfire Parade - Crisis
15 mai 2011, par
Mis à jour : Septembre 2011
Langue : English
Type : Audio
-
Wired NextMusic
14 mai 2011, par
Mis à jour : Février 2012
Langue : English
Type : Video
-
Video d’abeille en portrait
14 mai 2011, par
Mis à jour : Février 2012
Langue : français
Type : Video
-
Sintel MP4 Surround 5.1 Full
13 mai 2011, par
Mis à jour : Février 2012
Langue : English
Type : Video
-
Carte de Schillerkiez
13 mai 2011, par
Mis à jour : Septembre 2011
Langue : English
Type : Texte
-
Publier une image simplement
13 avril 2011, par ,
Mis à jour : Février 2012
Langue : français
Type : Video
Autres articles (76)
-
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 ;
-
Ecrire une actualité
21 juin 2013, parPrésentez les changements dans votre MédiaSPIP ou les actualités de vos projets sur votre MédiaSPIP grâce à la rubrique actualités.
Dans le thème par défaut spipeo de MédiaSPIP, les actualités sont affichées en bas de la page principale sous les éditoriaux.
Vous pouvez personnaliser le formulaire de création d’une actualité.
Formulaire de création d’une actualité Dans le cas d’un document de type actualité, les champs proposés par défaut sont : Date de publication ( personnaliser la date de publication ) (...) -
Gestion générale des documents
13 mai 2011, parMédiaSPIP ne modifie jamais le document original mis en ligne.
Pour chaque document mis en ligne il effectue deux opérations successives : la création d’une version supplémentaire qui peut être facilement consultée en ligne tout en laissant l’original téléchargeable dans le cas où le document original ne peut être lu dans un navigateur Internet ; la récupération des métadonnées du document original pour illustrer textuellement le fichier ;
Les tableaux ci-dessous expliquent ce que peut faire MédiaSPIP (...)
Sur d’autres sites (5871)
-
C++ ffmpeg lib version 7.0 - noice in exported audio
2 septembre 2024, par Chris PI want to make a C++ lib named cppdub which will mimic the python module pydub.


One main function is to export the AudioSegment to a file with a specific format (example : mp3).


The code is :


AudioSegment AudioSegment::from_file(const std::string& file_path, const std::string& format, const std::string& codec,
 const std::map& parameters, int start_second, int duration) {

 avformat_network_init();
 av_log_set_level(AV_LOG_ERROR); // Adjust logging level as needed

 AVFormatContext* format_ctx = nullptr;
 if (avformat_open_input(&format_ctx, file_path.c_str(), nullptr, nullptr) != 0) {
 std::cerr << "Error: Could not open audio file." << std::endl;
 return AudioSegment(); // Return an empty AudioSegment on failure
 }

 if (avformat_find_stream_info(format_ctx, nullptr) < 0) {
 std::cerr << "Error: Could not find stream information." << std::endl;
 avformat_close_input(&format_ctx);
 return AudioSegment();
 }

 int audio_stream_index = -1;
 for (unsigned int i = 0; i < format_ctx->nb_streams; i++) {
 if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
 audio_stream_index = i;
 break;
 }
 }

 if (audio_stream_index == -1) {
 std::cerr << "Error: Could not find audio stream." << std::endl;
 avformat_close_input(&format_ctx);
 return AudioSegment();
 }

 AVCodecParameters* codec_par = format_ctx->streams[audio_stream_index]->codecpar;
 const AVCodec* my_codec = avcodec_find_decoder(codec_par->codec_id);
 AVCodecContext* codec_ctx = avcodec_alloc_context3(my_codec);

 if (avcodec_parameters_to_context(codec_ctx, codec_par) < 0) {
 std::cerr << "Error: Could not initialize codec context." << std::endl;
 avformat_close_input(&format_ctx);
 return AudioSegment();
 }

 if (avcodec_open2(codec_ctx, my_codec, nullptr) < 0) {
 std::cerr << "Error: Could not open codec." << std::endl;
 avcodec_free_context(&codec_ctx);
 avformat_close_input(&format_ctx);
 return AudioSegment();
 }

 SwrContext* swr_ctx = swr_alloc();
 if (!swr_ctx) {
 std::cerr << "Error: Could not allocate SwrContext." << std::endl;
 avcodec_free_context(&codec_ctx);
 avformat_close_input(&format_ctx);
 return AudioSegment();
 }

 av_opt_set_chlayout(swr_ctx, "in_chlayout", &codec_ctx->ch_layout, 0);
 av_opt_set_int(swr_ctx, "in_sample_rate", codec_ctx->sample_rate, 0);
 av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", codec_ctx->sample_fmt, 0);

 AVChannelLayout dst_ch_layout;
 av_channel_layout_copy(&dst_ch_layout, &codec_ctx->ch_layout);
 av_channel_layout_uninit(&dst_ch_layout);
 av_channel_layout_default(&dst_ch_layout, 2);

 av_opt_set_chlayout(swr_ctx, "out_chlayout", &dst_ch_layout, 0);
 av_opt_set_int(swr_ctx, "out_sample_rate", 48000, 0);
 av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);

 if (swr_init(swr_ctx) < 0) {
 std::cerr << "Error: Failed to initialize the resampling context" << std::endl;
 swr_free(&swr_ctx);
 avcodec_free_context(&codec_ctx);
 avformat_close_input(&format_ctx);
 return AudioSegment();
 }

 AVPacket packet;
 AVFrame* frame = av_frame_alloc();
 if (!frame) {
 std::cerr << "Error: Could not allocate frame." << std::endl;
 swr_free(&swr_ctx);
 avcodec_free_context(&codec_ctx);
 avformat_close_input(&format_ctx);
 return AudioSegment();
 }

 std::vector<char> output;
 while (av_read_frame(format_ctx, &packet) >= 0) {
 if (packet.stream_index == audio_stream_index) {
 if (avcodec_send_packet(codec_ctx, &packet) == 0) {
 while (avcodec_receive_frame(codec_ctx, frame) == 0) {
 if (frame->pts != AV_NOPTS_VALUE) {
 frame->pts = av_rescale_q(frame->pts, codec_ctx->time_base, format_ctx->streams[audio_stream_index]->time_base);
 }

 uint8_t* output_buffer;
 int output_samples = av_rescale_rnd(
 swr_get_delay(swr_ctx, codec_ctx->sample_rate) + frame->nb_samples,
 48000, codec_ctx->sample_rate, AV_ROUND_UP);

 int output_buffer_size = av_samples_get_buffer_size(
 nullptr, 2, output_samples, AV_SAMPLE_FMT_S16, 1);

 output_buffer = (uint8_t*)av_malloc(output_buffer_size);

 if (output_buffer) {
 memset(output_buffer, 0, output_buffer_size); // Zero padding to avoid random noise
 int converted_samples = swr_convert(swr_ctx, &output_buffer, output_samples,
 (const uint8_t**)frame->extended_data, frame->nb_samples);

 if (converted_samples >= 0) {
 output.insert(output.end(), output_buffer, output_buffer + output_buffer_size);
 }
 else {
 std::cerr << "Error: Failed to convert audio samples." << std::endl;
 }

 av_free(output_buffer);
 }
 else {
 std::cerr << "Error: Could not allocate output buffer." << std::endl;
 }
 }
 }
 else {
 std::cerr << "Error: Failed to send packet to codec context." << std::endl;
 }
 }
 av_packet_unref(&packet);
 }

 av_frame_free(&frame);
 swr_free(&swr_ctx);
 avcodec_free_context(&codec_ctx);
 avformat_close_input(&format_ctx);

 std::map metadata = {
 {"sample_width", 2},
 {"frame_rate", 48000},
 {"channels", 2},
 {"frame_width", 4}
 };

 return AudioSegment(static_cast<const>(output.data()), output.size(), metadata);
}


std::ofstream AudioSegment::export_segment(std::string& out_f,
 const std::string& format,
 const std::string& codec,
 const std::string& bitrate,
 const std::vector& parameters,
 const std::map& tags,
 const std::string& id3v2_version,
 const std::string& cover) {
 av_log_set_level(AV_LOG_DEBUG);
 AVCodecContext* codec_ctx = nullptr;
 AVFormatContext* format_ctx = nullptr;
 AVStream* stream = nullptr;
 AVFrame* frame = nullptr;
 AVPacket* pkt = nullptr;
 int ret;

 // Open output file
 std::ofstream out_file(out_f, std::ios::binary);
 if (!out_file) {
 throw std::runtime_error("Failed to open output file.");
 }

 // Initialize format context
 avformat_alloc_output_context2(&format_ctx, nullptr, format.c_str(), out_f.c_str());
 if (!format_ctx) {
 throw std::runtime_error("Could not allocate format context.");
 }

 // Find encoder
 const AVCodec* codec_ptr = avcodec_find_encoder_by_name(codec.c_str());
 if (!codec_ptr) {
 throw std::runtime_error("Codec not found.");
 }

 // Add stream
 stream = avformat_new_stream(format_ctx, codec_ptr);
 if (!stream) {
 throw std::runtime_error("Failed to create new stream.");
 }

 // Allocate codec context
 codec_ctx = avcodec_alloc_context3(codec_ptr);
 if (!codec_ctx) {
 throw std::runtime_error("Could not allocate audio codec context.");
 }

 // Set codec parameters
 codec_ctx->bit_rate = std::stoi(bitrate);
 codec_ctx->sample_fmt = codec_ptr->sample_fmts ? codec_ptr->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
 codec_ctx->sample_rate = frame_rate_;
 codec_ctx->ch_layout.nb_channels = this->get_channels();
 AVChannelLayout ch_layout_1;
 av_channel_layout_uninit(&ch_layout_1);
 av_channel_layout_default(&ch_layout_1, this->get_channels());
 codec_ctx->ch_layout = ch_layout_1;

 // Open codec
 ret = avcodec_open2(codec_ctx, codec_ptr, nullptr);
 if (ret < 0) {
 throw std::runtime_error("Could not open codec.");
 }

 // Initialize packet
 pkt = av_packet_alloc();
 if (!pkt) {
 throw std::runtime_error("Could not allocate AVPacket.");
 }

 // Initialize frame
 frame = av_frame_alloc();
 if (!frame) {
 throw std::runtime_error("Could not allocate AVFrame.");
 }

 frame->nb_samples = codec_ctx->frame_size;
 frame->format = codec_ctx->sample_fmt;
 frame->ch_layout = codec_ctx->ch_layout;
 frame->sample_rate = codec_ctx->sample_rate;

 // Allocate data buffer
 ret = av_frame_get_buffer(frame, 0);
 if (ret < 0) {
 throw std::runtime_error("Could not allocate audio data buffers.");
 }

 // Encode frames
 int samples_read = 0;
 while (samples_read < data_.size()) {
 ret = av_frame_make_writable(frame);
 if (ret < 0) {
 throw std::runtime_error("Frame not writable.");
 }

 // Determine the number of samples to copy into the frame
 int frame_size = std::min<int>(codec_ctx->frame_size, (data_.size() - samples_read) / frame_width_);
 int buffer_size = frame_size * frame_width_;

 // Clear the frame data to avoid artifacts from previous data
 std::memset(frame->data[0], 0, codec_ctx->frame_size * frame_width_);

 // Copy the actual audio data into the frame
 std::memcpy(frame->data[0], data_.data() + samples_read, buffer_size);
 samples_read += buffer_size;

 // If the frame is partially filled, pad the remaining part with zeros
 if (frame_size < codec_ctx->frame_size) {
 std::memset(frame->data[0] + buffer_size, 0, (codec_ctx->frame_size - frame_size) * frame_width_);
 }

 // Send the frame for encoding
 ret = avcodec_send_frame(codec_ctx, frame);
 if (ret < 0) {
 throw std::runtime_error("Error sending frame for encoding.");
 }

 // Receive and write packets
 while (ret >= 0) {
 ret = avcodec_receive_packet(codec_ctx, pkt);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
 break;
 }
 else if (ret < 0) {
 throw std::runtime_error("Error encoding frame.");
 }

 out_file.write(reinterpret_cast(pkt->data), pkt->size);
 av_packet_unref(pkt);
 }
 }

 // **Explicitly flush the encoder**
 ret = avcodec_send_frame(codec_ctx, nullptr);
 if (ret < 0) {
 throw std::runtime_error("Error flushing the encoder.");
 }

 // Receive and write remaining packets after flushing
 while (ret >= 0) {
 ret = avcodec_receive_packet(codec_ctx, pkt);
 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
 break;
 }
 else if (ret < 0) {
 throw std::runtime_error("Error encoding frame during flush.");
 }

 out_file.write(reinterpret_cast(pkt->data), pkt->size);
 av_packet_unref(pkt);
 }

 // Cleanup
 av_frame_free(&frame);
 av_packet_free(&pkt);
 avcodec_free_context(&codec_ctx);
 avformat_free_context(format_ctx);

 return out_file;
}
</int></const></char>


I have no run time error but i see this message in console :


[libmp3lame @ 000002d26b239ac0] Trying to remove 47 more samples than there are in the queue



I can play the exported mp3 file but there is background noise.


-
I need some help accurately trimming video using the FFmpeg C api
20 mars 2015, par Justin BradleyI need some help accurately trimming video using the FFmpeg C api.
What I’m seeing is when the input video stream has a time_base of 1/48000 it correctly selects the trimmed start and end times. However when the input video stream has a time_base other than 1/48000 the trim is incorrect.Time bases of 1/30000 and 1/24000 trim half the expected video stream length - 10s instead of 20s.
1/25 trims almost nothing at all - the output file size is only a few kb.The audio stream appears to always be trimmed correctly.
For example if I try to trim the first 20s of a video whose video stream time base is 1/30000, the output mp4’s length is 20s. It has first 10s of video and first 20s of audio.
I think I’m incorrectly calculating the end_time but I’m not sure why it’s correct for 1/48000 time_base streams.
record[i].start_time = av_rescale_q((int64_t)( start_time * AV_TIME_BASE ), default_timebase, in_stream->time_base);
record[i].end_time = av_rescale_q((int64_t)( end_time * AV_TIME_BASE ), default_timebase, in_stream->time_base);Here is a more complete sample of code :
int num_of_streams = ifmt_ctx->nb_streams;
if(num_of_streams > 0) {
// keeps track of each stream's trimmed start and end times
struct stream_pts record[num_of_streams];
for (i = 0; i < num_of_streams; i++) {
AVStream *in_stream = ifmt_ctx->streams[i];
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
if (!out_stream) {
LOGE("=> Failed allocating output stream");
ret = AVERROR_UNKNOWN;
return close_connection(ret);
}
ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
if (ret < 0) {
LOGE("=> Failed to copy context from input to output stream codec context");
return close_connection(ret);
}
out_stream->codec->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
AVRational default_timebase;
default_timebase.num = 1;
default_timebase.den = AV_TIME_BASE;
// determine start/end times for each stream
record[i].index = i;
record[i].start_time = av_rescale_q((int64_t)( start_time * AV_TIME_BASE ), default_timebase, in_stream->time_base);
record[i].end_time = av_rescale_q((int64_t)( end_time * AV_TIME_BASE ), default_timebase, in_stream->time_base);
}
av_dump_format(ofmt_ctx, 0, output_file, 1);
if (!(ofmt->flags & AVFMT_NOFILE)) {
ret = avio_open(&ofmt_ctx->pb, output_file, AVIO_FLAG_WRITE);
if (ret < 0) {
LOGE("=> Could not open output file '%s'", output_file);
return close_connection(ret);
}
}
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0) {
LOGE("=> Error occurred when opening output file");
return close_connection(ret);
}
while (1) {
AVStream *in_stream, *out_stream;
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
// copy packet
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
// write the frames we're looking for
if(pkt.pts >= record[pkt.stream_index].start_time && pkt.pts <= record[pkt.stream_index].end_time) {
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (ret < 0) {
LOGE("=> Error muxing packet");
break;
}
}
av_free_packet(&pkt);
}
} -
ffmpeg to split mp4 file into segments... after first segment, audio unsynced
13 avril 2015, par bcsteeveI’ve used the ffmpeg command line shown in this question to split MKV files perfectly for a long time. Now i have some MP4 files that i’d like to split and at first it seemed to work, but every subsequent segment after the first has the audio not synced ! And by several seconds.
I’ve tried forcing keyframes (advice I found on some other sites) and that didn’t help.
I tried a different program entirely (Avidemux) and it was able to split the file with proper output, but it was a LOT slower, taking upwards of 3 minutes vs less than 2 seconds with ffmpeg. With Avidemux I was able to determine the exact position of the i-frame where I wanted to split, so thinking perhaps that was the syncing problem I tried that exact position (ie. 00:12:17.111 instead of 00:12:16 or whatever) but that didn’t help either.
Is there an option I’m missing with ffmpeg to get it to properly sync audio to the video when splitting ?