skąd i jak pobrać FFmpeg? do tego przykładu:
#include <iostream>
#include <filesystem>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cctype>
namespace fs = std::filesystem;
// --------------------------------------------------
// Zamiana rozszerzenia na lowercase
// --------------------------------------------------
std::string lower(std::string text)
{
std::transform(text.begin(), text.end(), text.begin(),
[](unsigned char c)
{
return static_cast<char>(std::tolower(c));
});
return text;
}
// --------------------------------------------------
// Cytowanie ścieżki dla polecenia FFmpeg
// --------------------------------------------------
std::string quote(const std::string& text)
{
return "\"" + text + "\"";
}
// --------------------------------------------------
// Sprawdzenie czy plik jest FLAC
// --------------------------------------------------
bool isFlac(const fs::path& path)
{
return lower(path.extension().string()) == ".flac";
}
// --------------------------------------------------
// GŁÓWNY PROGRAM
// --------------------------------------------------
int main()
{
std::cout << "====================================\n";
std::cout << " FLAC TO MP3 CONVERTER\n";
std::cout << "====================================\n\n";
// ----------------------------------------------
// 1. Pobranie katalogu źródłowego
// ----------------------------------------------
std::string input;
std::cout << "Podaj katalog z plikami FLAC:\n";
std::getline(std::cin, input);
if (input.empty())
{
std::cout << "Nie podano katalogu!\n";
return 1;
}
// ----------------------------------------------
// 2. Ustalenie katalogów
// ----------------------------------------------
fs::path source = fs::absolute(input).lexically_normal();
fs::path destination = source;
destination += "_temp";
std::cout << "\nKatalog zrodlowy:\n"
<< source.string() << "\n";
std::cout << "\nKatalog docelowy:\n"
<< destination.string() << "\n";
// ----------------------------------------------
// 3. Sprawdzenie katalogu źródłowego
// ----------------------------------------------
if (!fs::exists(source) || !fs::is_directory(source))
{
std::cout << "\nBlad: katalog nie istnieje!\n";
return 1;
}
// ----------------------------------------------
// 4. Utworzenie katalogu docelowego
// ----------------------------------------------
try
{
fs::create_directories(destination);
}
catch (const fs::filesystem_error& e)
{
std::cout << "\nBlad tworzenia katalogu:\n"
<< e.what() << "\n";
return 1;
}
// ----------------------------------------------
// 5. Wyszukanie wszystkich FLAC
// ----------------------------------------------
std::vector<fs::path> files;
std::cout << "\nSzukam plikow FLAC...\n";
try
{
for (const auto& entry :
fs::recursive_directory_iterator(
source,
fs::directory_options::skip_permission_denied))
{
if (entry.is_regular_file() &&
isFlac(entry.path()))
{
files.push_back(entry.path());
}
}
}
catch (const fs::filesystem_error& e)
{
std::cout << "\nBlad przeszukiwania:\n"
<< e.what() << "\n";
}
// ----------------------------------------------
// 6. Sortowanie plików
// ----------------------------------------------
std::sort(files.begin(), files.end());
const std::size_t total = files.size();
if (total == 0)
{
std::cout << "\nNie znaleziono plikow FLAC!\n";
return 0;
}
std::cout << "Znaleziono FLAC: " << total << "\n\n";
// ----------------------------------------------
// 7. Konwersja
// ----------------------------------------------
std::size_t converted = 0;
std::size_t skipped = 0;
std::size_t errors = 0;
for (std::size_t i = 0; i < total; ++i)
{
const fs::path& inputFile = files[i];
// Ścieżka względna względem source
fs::path relative = fs::relative(inputFile, source);
// Zmiana rozszerzenia na MP3
relative.replace_extension(".mp3");
// Pełna ścieżka docelowa
fs::path outputFile = destination / relative;
// Utworzenie podkatalogu
fs::create_directories(outputFile.parent_path());
std::cout << "[" << (i + 1) << "/" << total << "] "
<< relative.string() << "\n";
// ------------------------------------------
// 8. Pomijanie istniejących MP3
// ------------------------------------------
if (fs::exists(outputFile))
{
std::cout << " POMINIETO - MP3 juz istnieje\n";
skipped++;
continue;
}
// ------------------------------------------
// 9. Polecenie FFmpeg
// ------------------------------------------
std::string command =
"ffmpeg -hide_banner -loglevel error -n "
"-i " + quote(inputFile.string()) +
" -map 0:a:0 "
"-map_metadata 0 "
"-vn "
"-codec:a libmp3lame "
"-b:a 320k "
"-id3v2_version 3 "
+ quote(outputFile.string());
// ------------------------------------------
// 10. Uruchomienie konwersji
// ------------------------------------------
int result = std::system(command.c_str());
if (result == 0 && fs::exists(outputFile))
{
std::cout << " OK\n";
converted++;
}
else
{
std::cout << " BLAD KONWERSJI\n";
errors++;
// Usunięcie niekompletnego pliku
if (fs::exists(outputFile))
fs::remove(outputFile);
}
}
// ----------------------------------------------
// 11. Podsumowanie
// ----------------------------------------------
std::cout << "\n====================================\n";
std::cout << " KONWERSJA GOTOWA\n";
std::cout << "====================================\n";
std::cout << "FLAC znalezione: " << total << "\n";
std::cout << "MP3 utworzone: " << converted << "\n";
std::cout << "MP3 pominiete: " << skipped << "\n";
std::cout << "Bledy: " << errors << "\n";
std::cout << "\nKatalog wynikowy:\n"
<< destination.string() << "\n";
std::cout << "\nNacisnij ENTER aby zakonczyc...";
std::cin.get();
return 0;
}