
Recherche avancée
Médias (3)
-
Valkaama DVD Cover Outside
4 octobre 2011, par
Mis à jour : Octobre 2011
Langue : English
Type : Image
-
Valkaama DVD Label
4 octobre 2011, par
Mis à jour : Février 2013
Langue : English
Type : Image
-
Valkaama DVD Cover Inside
4 octobre 2011, par
Mis à jour : Octobre 2011
Langue : English
Type : Image
Autres articles (9)
-
Organiser par catégorie
17 mai 2013, parDans MédiaSPIP, une rubrique a 2 noms : catégorie et rubrique.
Les différents documents stockés dans MédiaSPIP peuvent être rangés dans différentes catégories. On peut créer une catégorie en cliquant sur "publier une catégorie" dans le menu publier en haut à droite ( après authentification ). Une catégorie peut être rangée dans une autre catégorie aussi ce qui fait qu’on peut construire une arborescence de catégories.
Lors de la publication prochaine d’un document, la nouvelle catégorie créée sera proposée (...) -
Les thèmes de MediaSpip
4 juin 20133 thèmes sont proposés à l’origine par MédiaSPIP. L’utilisateur MédiaSPIP peut rajouter des thèmes selon ses besoins.
Thèmes MediaSPIP
3 thèmes ont été développés au départ pour MediaSPIP : * SPIPeo : thème par défaut de MédiaSPIP. Il met en avant la présentation du site et les documents média les plus récents ( le type de tri peut être modifié - titre, popularité, date) . * Arscenic : il s’agit du thème utilisé sur le site officiel du projet, constitué notamment d’un bandeau rouge en début de page. La structure (...) -
Déploiements possibles
31 janvier 2010, parDeux types de déploiements sont envisageable dépendant de deux aspects : La méthode d’installation envisagée (en standalone ou en ferme) ; Le nombre d’encodages journaliers et la fréquentation envisagés ;
L’encodage de vidéos est un processus lourd consommant énormément de ressources système (CPU et RAM), il est nécessaire de prendre tout cela en considération. Ce système n’est donc possible que sur un ou plusieurs serveurs dédiés.
Version mono serveur
La version mono serveur consiste à n’utiliser qu’une (...)
Sur d’autres sites (2816)
-
How to install ffmpeg on a Windows Dockerhub image ?
18 janvier, par Youssef KharoufiI have a program that executes a ffmpeg command to a video input, copies this video and pastes is in an output directory.


Here is my code in case you would want to duplicate it :


using Renderer.Models;
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;

public class Program
{
 public static async Task Main(string[] args)
 {
 var result = new DockerTaskResult();
 try
 {
 // Path to the JSON input file
 string jsonInputPath = @"C:\Users\ykharoufi\source\repos\Renderer\Renderer\Json\task.json";

 // Check if the JSON file exists
 if (!File.Exists(jsonInputPath))
 {
 throw new FileNotFoundException($"JSON input file not found at path: {jsonInputPath}");
 }

 // Read the JSON content
 string jsonContent = File.ReadAllText(jsonInputPath);

 try
 {
 // Deserialize the JSON into a DockerTask object
 DockerTask task = JsonSerializer.Deserialize<dockertask>(jsonContent);
 if (task == null)
 {
 throw new Exception("Failed to deserialize the task from JSON.");
 }

 // Validate the input paths
 if (string.IsNullOrEmpty(task.InputFileRepoPath) || !File.Exists(task.InputFileRepoPath))
 {
 throw new Exception($"Input file path is invalid or does not exist: {task.InputFileRepoPath}");
 }

 if (string.IsNullOrEmpty(task.OutputFileRepoPath) || !Directory.Exists(task.OutputFileRepoPath))
 {
 throw new Exception($"Output directory path is invalid or does not exist: {task.OutputFileRepoPath}");
 }

 // Initialize the Docker worker and run the task
 var worker = new DockerWorker();
 var success = await worker.RunDockerTaskAsync(task);

 if (success.Success)
 {
 result.Success = true;
 result.Message = "Command executed successfully!";

 // Check the output directory for files
 if (Directory.Exists(task.OutputFileRepoPath))
 {
 result.OutputFiles = Directory.GetFiles(task.OutputFileRepoPath);
 }
 }
 else
 {
 result.Success = false;
 result.Message = "Failed to execute the command.";
 result.ErrorDetails = success.ErrorDetails;
 }
 }
 catch (JsonException)
 {
 // Handle invalid JSON format
 result.Success = false;
 result.Message = "Invalid JSON format.";
 result.ErrorDetails = "Invalid data entry";
 }
 }
 catch (Exception ex)
 {
 result.Success = false;
 result.Message = "An error occurred during execution.";
 result.ErrorDetails = ex.Message;
 }
 finally
 {
 // Serialize the result to JSON and write to console
 string outputJson = JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true });
 Console.WriteLine(outputJson);
 }
 }
}

 using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Docker.DotNet;
using Docker.DotNet.Models;
using Renderer.Models;

public class DockerWorker
{
 private readonly DockerClient _dockerClient;

 public DockerWorker()
 {
 Console.WriteLine("Initializing Docker client...");
 _dockerClient = new DockerClientConfiguration(
 new Uri("npipe://./pipe/docker_engine")) // Windows Docker URI
 .CreateClient();
 Console.WriteLine("Docker client initialized.");
 }

 public async Task<dockertaskresult> RunDockerTaskAsync(DockerTask task)
 {
 var result = new DockerTaskResult();

 try
 {
 Console.WriteLine("Starting Docker task...");
 Console.WriteLine($"Image: {task.ImageNaming}");
 Console.WriteLine($"Container: {task.ContainerNaming}");
 Console.WriteLine($"Input Path: {task.InputFileRepoPath}");
 Console.WriteLine($"Output Path: {task.OutputFileRepoPath}");
 Console.WriteLine($"Command: {task.CommandDef}");

 // Ensure the Docker image exists
 Console.WriteLine("Checking if Docker image exists...");
 var imageCheckResult = await EnsureImageExists(task.ImageNaming);
 if (!imageCheckResult.Success)
 {
 result.Success = false;
 result.Message = imageCheckResult.Message;
 result.ErrorDetails = imageCheckResult.ErrorDetails;
 return result;
 }
 Console.WriteLine("Docker image verified.");

 // Determine platform
 var platform = await GetImagePlatform(task.ImageNaming);
 Console.WriteLine($"Detected image platform: {platform}");

 // Translate paths
 string inputVolume, outputVolume;
 if (platform == "linux")
 {
 inputVolume = $"{task.InputFileRepoPath.Replace("C:\\", "/mnt/c/").Replace("\\", "/")}:/app/input";
 outputVolume = $"{task.OutputFileRepoPath.Replace("C:\\", "/mnt/c/").Replace("\\", "/")}:/app/output";
 }
 else if (platform == "windows")
 {
 string inputDir = Path.GetFullPath(Path.GetDirectoryName(task.InputFileRepoPath)).TrimEnd('\\');
 string outputDir = Path.GetFullPath(task.OutputFileRepoPath).TrimEnd('\\');

 if (!Directory.Exists(inputDir))
 {
 throw new Exception($"Input directory does not exist: {inputDir}");
 }
 if (!Directory.Exists(outputDir))
 {
 throw new Exception($"Output directory does not exist: {outputDir}");
 }

 inputVolume = $"{inputDir}:C:\\app\\input";
 outputVolume = $"{outputDir}:C:\\app\\output";

 Console.WriteLine($"Input volume: {inputVolume}");
 Console.WriteLine($"Output volume: {outputVolume}");
 }
 else
 {
 throw new Exception($"Unsupported platform: {platform}");
 }

 // Create container
 Console.WriteLine("Creating Docker container...");
 var containerResponse = await _dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters
 {
 Image = task.ImageNaming,
 Name = task.ContainerNaming,
 HostConfig = new HostConfig
 {
 Binds = new List<string> { inputVolume, outputVolume },
 AutoRemove = true
 },
 Cmd = new[] { platform == "linux" ? "bash" : "cmd.exe", "/c", task.CommandDef }
 });

 if (string.IsNullOrEmpty(containerResponse.ID))
 {
 throw new Exception("Failed to create Docker container.");
 }
 Console.WriteLine($"Container created with ID: {containerResponse.ID}");

 // Start container
 Console.WriteLine("Starting container...");
 bool started = await _dockerClient.Containers.StartContainerAsync(containerResponse.ID, new ContainerStartParameters());
 if (!started)
 {
 throw new Exception($"Failed to start container: {task.ContainerNaming}");
 }

 // Wait for container to finish
 Console.WriteLine("Waiting for container to finish...");
 var waitResponse = await _dockerClient.Containers.WaitContainerAsync(containerResponse.ID);

 if (waitResponse.StatusCode != 0)
 {
 Console.WriteLine($"Container exited with error code: {waitResponse.StatusCode}");
 await FetchContainerLogs(containerResponse.ID);
 throw new Exception($"Container exited with non-zero status code: {waitResponse.StatusCode}");
 }

 // Fetch logs
 Console.WriteLine("Fetching container logs...");
 await FetchContainerLogs(containerResponse.ID);

 result.Success = true;
 result.Message = "Docker task completed successfully.";
 return result;
 }
 catch (Exception ex)
 {
 Console.WriteLine($"Error: {ex.Message}");
 result.Success = false;
 result.Message = "An error occurred during execution.";
 result.ErrorDetails = ex.Message;
 return result;
 }
 }

 private async Task<string> GetImagePlatform(string imageName)
 {
 try
 {
 Console.WriteLine($"Inspecting Docker image: {imageName}...");
 var imageDetails = await _dockerClient.Images.InspectImageAsync(imageName);
 Console.WriteLine($"Image platform: {imageDetails.Os}");
 return imageDetails.Os.ToLower();
 }
 catch (Exception ex)
 {
 throw new Exception($"Failed to inspect image: {ex.Message}");
 }
 }

 private async Task<dockertaskresult> EnsureImageExists(string imageName)
 {
 var result = new DockerTaskResult();
 try
 {
 Console.WriteLine($"Pulling Docker image: {imageName}...");
 await _dockerClient.Images.CreateImageAsync(
 new ImagesCreateParameters { FromImage = imageName, Tag = "latest" },
 null,
 new Progress<jsonmessage>()
 );
 result.Success = true;
 result.Message = "Docker image is available.";
 }
 catch (Exception ex)
 {
 result.Success = false;
 result.Message = $"Failed to pull Docker image: {imageName}";
 result.ErrorDetails = ex.Message;
 }
 return result;
 }

 private async Task FetchContainerLogs(string containerId)
 {
 try
 {
 Console.WriteLine($"Fetching logs for container: {containerId}...");
 var logs = await _dockerClient.Containers.GetContainerLogsAsync(
 containerId,
 new ContainerLogsParameters { ShowStdout = true, ShowStderr = true });

 using (var reader = new StreamReader(logs))
 {
 string logLine;
 while ((logLine = await reader.ReadLineAsync()) != null)
 {
 Console.WriteLine(logLine);
 }
 }
 }
 catch (Exception ex)
 {
 Console.WriteLine($"Failed to fetch logs: {ex.Message}");
 }
 }
}

 {
 "ImageNaming": "khuser/windowsimage:latest",
 "ContainerNaming": "custom-worker-container",
 "InputFileRepoPath": "C:/Users/user/source/repos/Renderer/Renderer/wwwroot/audio.mp4",
 "OutputFileRepoPath": "C:/Users/user/Desktop/output/",
 "CommandDef": "ffmpeg -i C:\\app\\input\\audio.mp4 -c copy C:\\app\\output\\copiedAudio.mp4"
</jsonmessage></dockertaskresult></string></string></dockertaskresult></dockertask>


}. My problem is what I get in the output of my program :
Initializing Docker client... Docker client initialized. Starting Docker task... Image: khyoussef/windowsimage:latest Container: custom-worker-container Input Path: C:/Users/ykharoufi/source/repos/Renderer/Renderer/wwwroot/audio.mp4 Output Path: C:/Users/ykharoufi/Desktop/output/ Command: ffmpeg -i C:\app\input\audio.mp4 -c copy C:\app\output\copiedAudio.mp4 Checking if Docker image exists... Pulling Docker image: khyoussef/windowsimage:latest... Docker image verified. Inspecting Docker image: khyoussef/windowsimage:latest... Image platform: windows Detected image platform: windows Input volume: C:\Users\ykharoufi\source\repos\Renderer\Renderer\wwwroot:C:\app\input Output volume: C:\Users\ykharoufi\Desktop\output:C:\app\output Creating Docker container... Container created with ID: 1daca99b3c76bc8c99c1aed7d2c546ae75aedd9aa1feb0f5002e54769390248e Starting container... Waiting for container to finish... Container exited with error code: 1 Fetching logs for container: 1daca99b3c76bc8c99c1aed7d2c546ae75aedd9aa1feb0f5002e54769390248e... @'ffmpeg' is not recognized as an internal or external command, !operable program or batch file. Error: Container exited with non-zero status code: 1 { "Success": false, "Message": "Failed to execute the command.", "ErrorDetails": "Container exited with non-zero status code: 1", "OutputFiles": null }
, This is the Dockerfile I am working with :

`# Use Windows Server Core as the base image
FROM mcr.microsoft.com/windows/server:ltsc2022

# Set the working directory
WORKDIR /app

# Install required tools (FFmpeg and Visual C++ Redistributable)
RUN powershell -Command \
 $ErrorActionPreference = 'Stop'; \
 # Install Visual C++ Redistributable
 Invoke-WebRequest -Uri https://aka.ms/vs/16/release/vc_redist.x64.exe -OutFile vc_redist.x64.exe; \
 Start-Process -FilePath vc_redist.x64.exe -ArgumentList '/install', '/quiet', '/norestart' -NoNewWindow -Wait; \
 Remove-Item -Force vc_redist.x64.exe; \
 # Download FFmpeg
 Invoke-WebRequest -Uri https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-win64-gpl-7.1.zip -OutFile ffmpeg.zip; \
 # Extract FFmpeg
 Expand-Archive -Path ffmpeg.zip -DestinationPath C:\ffmpeg; \
 Remove-Item -Force ffmpeg.zip; \
 # Debug step: Verify FFmpeg extraction
 Write-Output "FFmpeg extracted to C:\\ffmpeg"; \
 dir C:\ffmpeg; \
 dir C:\ffmpeg\ffmpeg-n7.1-latest-win64-gpl-7.1\bin

# Add FFmpeg to PATH permanently
ENV PATH="C:\\ffmpeg\\ffmpeg-n7.1-latest-win64-gpl-7.1\\bin;${PATH}"

# Verify FFmpeg installation
RUN ["cmd", "/S", "/C", "ffmpeg -version"]

# Copy required files to the container
COPY ./ /app/

# Default to a PowerShell session
CMD ["C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", "-NoExit"]
`



. I did build it using this command : docker build -t khuser/windowsimage:latest -f Dockerfile .


-
C++ ffmpeg - export to wav error : Invalid PCM packet, data has size 2 but at least a size of 4 was expected
9 septembre 2024, par Chris PC++ code :


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 (!codec_ctx) {
 std::cerr << "Error: Could not allocate codec context." << std::endl;
 avformat_close_input(&format_ctx);
 return AudioSegment();
 }

 if (avcodec_parameters_to_context(codec_ctx, codec_par) < 0) {
 std::cerr << "Error: Could not initialize codec context." << std::endl;
 avcodec_free_context(&codec_ctx);
 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();
 }
 codec_ctx->sample_rate = 44100;
 // Set up resampling context to convert to S16 format with 2 bytes per sample
 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", codec_ctx->sample_rate, 0); // Match input sample rate
 av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); // Force S16 format

 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,
 codec_ctx->sample_rate, 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;
 }
 // Make sure output_buffer is valid before freeing
 if (output_buffer != nullptr) {
 av_free(output_buffer);
 output_buffer = nullptr; // Prevent double-free
 }
 }
 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);
 }

 int frame_width = av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * 2; // Use 2 bytes per sample and 2 channels

 std::map metadata = {
 {"sample_width", 2}, // S16 format has 2 bytes per sample
 {"frame_rate", codec_ctx->sample_rate}, // Use the input sample rate
 {"channels", 2}, // Assuming stereo output
 {"frame_width", frame_width}
 };

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

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

std::ofstream AudioSegment::export_segment_to_wav_file(const std::string& out_f) {
 std::cout << this->get_channels() << std::endl;
 av_log_set_level(AV_LOG_ERROR);
 AVCodecContext* codec_ctx = nullptr;
 AVFormatContext* format_ctx = nullptr;
 AVStream* stream = nullptr;
 AVFrame* frame = nullptr;
 AVPacket* pkt = nullptr;
 int ret;

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

 // Find encoder for PCM
 const AVCodec* codec_ptr = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE);
 if (!codec_ptr) {
 throw std::runtime_error("PCM encoder 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 for PCM
 codec_ctx->bit_rate = 128000; // Bitrate
 codec_ctx->sample_rate = this->get_frame_rate(); // Use correct sample rate
 codec_ctx->ch_layout.nb_channels = this->get_channels(); // Set the correct channel count

 // Set the channel layout: stereo or mono
 if (this->get_channels() == 2) {
 av_channel_layout_default(&codec_ctx->ch_layout, 2); // Stereo layout
 }
 else {
 av_channel_layout_default(&codec_ctx->ch_layout, 1); // Mono layout
 }

 codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16; // PCM 16-bit format

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

 // Set codec parameters to the stream
 if (avcodec_parameters_from_context(stream->codecpar, codec_ctx) < 0) {
 throw std::runtime_error("Could not initialize stream codec parameters.");
 }

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

 if (!(format_ctx->oformat->flags & AVFMT_NOFILE)) {
 if (avio_open(&format_ctx->pb, out_f.c_str(), AVIO_FLAG_WRITE) < 0) {
 throw std::runtime_error("Could not open output file.");
 }
 }

 // Write file header
 if (avformat_write_header(format_ctx, nullptr) < 0) {
 throw std::runtime_error("Error occurred when writing file header.");
 }

 // 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.");
 }

 // Set the frame properties
 frame->format = codec_ctx->sample_fmt;
 frame->ch_layout = codec_ctx->ch_layout;

 // Number of audio samples available in the data buffer
 int total_samples = data_.size() / (av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);
 int samples_read = 0;

 // Set the number of samples per frame dynamically based on the input data
 while (samples_read < total_samples) {
 // Determine how many samples to read in this iteration (don't exceed the total sample count)
 int num_samples = std::min(codec_ctx->frame_size, total_samples - samples_read);
 if (num_samples == 0) {
 num_samples = 1024;
 codec_ctx->frame_size = 1024;
 }
 // Ensure num_samples is not zero
 if (num_samples <= 0) {
 throw std::runtime_error("Invalid number of samples in frame.");
 }

 // Set the number of samples in the frame
 frame->nb_samples = num_samples;

 // Allocate the frame buffer based on the number of samples
 ret = av_frame_get_buffer(frame, 0);
 if (ret < 0) {
 std::cerr << "Error allocating frame buffer: " << ret << std::endl;
 throw std::runtime_error("Could not allocate audio data buffers.");
 }

 // Copy the audio data into the frame's buffer (interleaving if necessary)
 /*if (codec_ctx->ch_layout.nb_channels == 2) {
 // If stereo, interleave planar data into packed format
 for (int i = 0; i < num_samples; ++i) {
 ((int16_t*)frame->data[0])[2 * i] = ((int16_t*)data_.data())[i]; // Left channel
 ((int16_t*)frame->data[0])[2 * i + 1] = ((int16_t*)data_.data())[total_samples + i]; // Right channel
 }
 }
 else {
 // For mono or packed data, directly copy the samples
 std::memcpy(frame->data[0], data_.data() + samples_read * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels,
 num_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);
 }
 */
 std::memcpy(frame->data[0], data_.data() + samples_read * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels,
 num_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * codec_ctx->ch_layout.nb_channels);

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

 // Receive and write encoded 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 during encoding.");
 }

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

 samples_read += num_samples;
 }

 // Flush the encoder
 if (avcodec_send_frame(codec_ctx, nullptr) < 0) {
 throw std::runtime_error("Error flushing the encoder.");
 }

 while (avcodec_receive_packet(codec_ctx, pkt) >= 0) {
 out_file.write(reinterpret_cast(pkt->data), pkt->size);
 av_packet_unref(pkt);
 }

 // Write file trailer
 av_write_trailer(format_ctx);

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

 if (!(format_ctx->oformat->flags & AVFMT_NOFILE)) {
 avio_closep(&format_ctx->pb);
 }
 avformat_free_context(format_ctx);

 out_file.close();
 return out_file;
}

</const></char>


Run code :


#include "audio_segment.h"
#include "effects.h"
#include "playback.h"
#include "cppaudioop.h"
#include "exceptions.h"
#include "generators.h"
#include "silence.h"
#include "utils.h"

#include <iostream>
#include <filesystem>

using namespace cppdub;

int main() {
 try {
 // Load the source audio file
 AudioSegment seg_1 = AudioSegment::from_file("../data/test10.mp3");
 std::string out_file_name = "ah-ah-ah.wav";

 // Export the audio segment to a new file with specified settings
 //seg_1.export_segment(out_file_name, "mp3");
 seg_1.export_segment_to_wav_file(out_file_name);


 // Optionally play the audio segment to verify
 // play(seg_1);

 // Load the exported audio file
 AudioSegment seg_2 = AudioSegment::from_file(out_file_name);

 // Play segments
 //play(seg_1);
 play(seg_2);
 }
 catch (const std::exception& e) {
 std::cerr << "An error occurred: " << e.what() << std::endl;
 }

 return 0;
}
</filesystem></iostream>


Error in second call of from_file function :


[pcm_s16le @ 000002d82ca5bfc0] Invalid PCM packet, data has size 2 but at least a size of 4 was expected


The process continue, i call hear the seg_2 with play(seg_2) call, but i can't directly play seg_2 export wav file (from windows explorer).


I had a guess that error may be because packed vs plannar formats missmatch but i am not quit sure. Maybe a swr_convert is necessary.


-
Java uses FFmpegRecoder to encode frames into H264 streams
5 septembre 2024, par zhang1973I want to obtain the Frame from the video stream, process it, use FFmpegRecoder to encode it into an H264 stream, and transmit it to the front-end. But I found that the AVPacket obtained directly using grabber.grabAVPacket can be converted into H264 stream and played normally. The H264 stream encoded using FFmpegRecoder cannot be played.


Here is my Code :


private FFmpegFrameRecorder recorder;
 private ByteArrayOutputStream outputStream = new ByteArrayOutputStream();;
 private boolean createRecoder(Frame frame){
 recorder = new FFmpegFrameRecorder(outputStream, frame.imageWidth, frame.imageHeight);
 recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
 recorder.setFormat("h264"); //"h264"); //
 recorder.setFrameRate(30);
 recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
 recorder.setVideoBitrate(4000 * 1000); // 设置比特率为4000 kbps
 recorder.setVideoOption("preset", "ultrafast"); // 设置编码器预设,"ultrafast"是最快的,"veryslow"是最慢但质量最好
 recorder.setAudioChannels(0);

 try {
 recorder.start();
 return recorderStatus = true;
 } catch (org.bytedeco.javacv.FrameRecorder.Exception e1) {
 log.info("启动转码录制器失败", e1);
 MediaService.cameras.remove(cameraDto.getMediaKey());
 e1.printStackTrace();
 }

 return recorderStatus = false;
 }

 private boolean slow = false;
 protected void transferStream2H264() throws FFmpegFrameGrabber.Exception {

 // 初始化和拉去图像的方法
 log.info(" create grabber ");
 if (!createGrabber()) {
 log.error(" == > ");
 return;
 }
 transferFlag = true;

 if(!createRecoder(grabber.grab())){
 return;
 }

 try {
 grabber.flush();
 } catch (Exception e) {
 log.info("清空拉流器缓存失败", e);
 e.printStackTrace();
 }

 if (header == null) {
 header = bos.toByteArray();
 slow = true;
// System.out.println("Header1");
// System.out.println(header);
 bos.reset();
 }else{
 System.out.println("Header2");
 System.out.println(header);
 }

 running = true;

 // 事实更新所有的连接数
 listenClient();

 long startTime = 0;
 long videoTS = 0;

 for (; running && grabberStatus;) {
 try {
 if (transferFlag) {
 long startGrab = System.currentTimeMillis();
 //视频采集器
// AVPacket pkt = grabber.grabPacket();
 Frame frame = grabber.grab();
 recorder.record(frame);
 byte[] videoData = outputStream.toByteArray();
 if ((System.currentTimeMillis() - startGrab) > 5000) {
 log.info("\r\n{}\r\n视频流网络异常>>>", cameraDto.getUrl());
 closeMedia();
 break;
 }

 videoTS = 1000 * (System.currentTimeMillis() - startTime);


 if (startTime == 0) {
 startTime = System.currentTimeMillis();
 }
 videoTS = 1000 * (System.currentTimeMillis() - startTime);

 byte[] rbuffer = videoData;
 readSize = videoData.length;

 if(spsdata == null || ppsdata == null){
 movePos = 0;
 lastPos = 0;
 isNewPack = true;
 while(movePos < readSize){
 if (rbuffer[movePos] == 0 && rbuffer[movePos + 1] == 0 && rbuffer[movePos + 2] == 1) {
 findCode = true;
 skipLen = 3;
 mCurFrameFirstByte = (int)(0xff & rbuffer[movePos + skipLen]);
 } else if (rbuffer[movePos] == 0 && rbuffer[movePos + 1] == 0 && rbuffer[movePos + 2] == 0 && rbuffer[movePos + 3] == 1) {
 findCode = true;
 skipLen = 4;
 mCurFrameFirstByte = (int)(0xff & rbuffer[movePos + skipLen]);
 } else {
 skipLen = 1;
 }

 if(!isFirstFind && isNewPack && findCode){
 mFrameFirstByte = mCurFrameFirstByte;
 findCode = false;
 isNewPack = false;
 mNaluType = mFrameFirstByte & 0x1f;
 if(mNaluType != MediaConstant.NALU_TYPE_SEI &&
 mNaluType != MediaConstant.NALU_TYPE_SPS &&
 mNaluType != MediaConstant.NALU_TYPE_PPS &&
 mNaluType != MediaConstant.NALU_TYPE_IDR){
 startCounter++;
 break;
 }
 }

 if(isFirstFind){
 isFirstFind = false;
 findCode = false;
 mFrameFirstByte = mCurFrameFirstByte;
 }

 if(findCode){
 startCounter++;
 mNaluType = mFrameFirstByte & 0x1f;

 findCode = false;
 mFrameLen = (movePos - lastPos);
 if(mNaluType == MediaConstant.NALU_TYPE_IDR){
 mFrameLen = readSize - movePos;
 }

 if(mNaluType != MediaConstant.NALU_TYPE_SEI &&
 mNaluType != MediaConstant.NALU_TYPE_SPS &&
 mNaluType != MediaConstant.NALU_TYPE_PPS &&
 mNaluType != MediaConstant.NALU_TYPE_IDR){
 System.out.println(" one packe many frames ---> type: " + mNaluType + " jump out ");
 break;
 }
 if(mNaluType == MediaConstant.NALU_TYPE_SPS){
 if(null == spsdata){
 spsdata = new byte[mFrameLen];
 System.arraycopy(rbuffer, lastPos, spsdata, 0, mFrameLen);
 }
 }
 if(mNaluType == MediaConstant.NALU_TYPE_PPS){

 if(null == ppsdata){
 ppsdata = new byte[mFrameLen];
 System.arraycopy(rbuffer, lastPos, ppsdata, 0, mFrameLen);
 }
 }

 lastPos = movePos;
 mFrameFirstByte = mCurFrameFirstByte;
 mNaluType = mFrameFirstByte & 0x1f;
 if(mNaluType == MediaConstant.NALU_TYPE_IDR){
 mFrameLen = readSize - movePos;
 startCounter++;

 break;
 }
 }

 movePos += skipLen;
 isNewPack = false;
 }
 }

 sendFrameData(rbuffer);
// }
// av_packet_unref(pkt);
// }

// }
 } else {
 }
 } catch (Exception e) {
 grabberStatus = false;
 MediaService.cameras.remove(cameraDto.getMediaKey());
 } catch (FFmpegFrameRecorder.Exception e) {
 throw new RuntimeException(e);
 }
 }

 try {
 grabber.close();
 bos.close();
 } catch (org.bytedeco.javacv.FrameRecorder.Exception e) {
 e.printStackTrace();
 } catch (Exception e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 closeMedia();
 }
 log.info("关闭媒体流-javacv,{} ", cameraDto.getUrl());
 }