I've been a bit bored over the past hour so i ported one of the shell scripts to C++ and added support for reading a list of files to convert, enjoy.
//C++ port of Calum and Void Main's m4a2mp3 v0.13 shell script
//Copyright (C) 2006 Daniel Collins, Calum, Void Main
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//Convert function
void m4a2mp3(std::string file) {
//Check the source file exists and can be opened
int test_open_return = open(file.c_str(), O_RDONLY | O_NONBLOCK | O_LARGEFILE);
if(test_open_return == -1) {
//Opening the file failed
std::cerr << "Cannot open file " << file << ": " << strerror(errno) << std::endl;
exit(1);
}
//We dont need the file to be open, so lets not waste an fd :)
close(test_open_return);
//Make a temporary wav file out of the m4a file
std::string command = "faad -o \"" + file + ".wav\" \"" + file + "\"";
int command_return = system(command.c_str());
if(command_return != 0) {
std::cerr << "Failed to convert m4a file to wav with `" << command << "`" << std::endl;
std::cerr << "There was a problem with the conversion process!" << std::endl;
exit(1);
}
//Convert the wav to an mp3
command = "lame -h -b 192 \"" + file + ".wav\" \"" + file + ".m4a.mp3\"";
command_return = system(command.c_str());
if(command_return != 0) {
std::cerr << "Failed to convert wav file to mp3 file with `" << command << "`" << std::endl;
std::cerr << "There was a problem with the conversion process!" << std::endl;
exit(1);
}
//Delete the temporary wav file
std::string tmp_del = file + ".wav";
int unlink_return = unlink(tmp_del.c_str());
if(unlink_return == -1) {
std::cerr << "Cannot delete temporary wav file: " << strerror(errno) << std::endl;
exit(1);
}
}
int main(int argc, char** argv) {
//Not enough command line switches
if(argc < 2) {
std::cerr << "Syntax: " << argv[0] << " [--help|--file] " << std::endl;
exit(1);
}
std::string mode = "1file";
std::string file;
//Check switches
int argd = 1;
while(argc > argd) {
std::string args = argv[argd];
if(args == "--help") {
std::cout << "m4a2mp3 v0.14" << std::endl;
std::cout << "Syntax: " << argv[0] << " [--help|--file] " << std::endl << std::endl;
std::cout << "--help\tPrint this message to STDOUT and exit" << std::endl;
std::cout << "--file\tParse a file containing a list of files to convert" << std::endl;
std::cout << "\t\t(a listfile should contain a list of sourcefiles, one per line)" << std::endl;
exit(0);
}else if(args == "--file") {
mode = "filelist";
}else{
file = args;
}
argd++;
}
if(mode == "1file") {
m4a2mp3(file);
}else if(mode == "filelist") {
//Open the file list
int list_fd = open(file.c_str(), O_RDONLY | O_NONBLOCK);
if(list_fd == -1) {
std::cerr << "Cannot open convert list: " << strerror(errno) << std::endl;
exit(1);
}
//Read the list from the file descriptor list_fd, into the temporary char buffer
char tmp_buffer[4096];
int read_return = read(list_fd, tmp_buffer, 4096);
if(read_return == -1) {
std::cerr << "Cannot read convert list: " << strerror(errno) << std::endl;
exit(1);
}
tmp_buffer[read_return] = '\0';
//Copy the char buffer into a new std::string buffer and close the file
std::string buffer = tmp_buffer;
close(list_fd);
//Remove any carridge returns incase user wrote the file in fucking notepad
boost::algorithm::erase_all(buffer, "\r");
//define boost split stuff
typedef std::vector < boost::iterator_range < std::string::iterator > > find_vector_type;
typedef std::vector < std::string > split_vector_type;
find_vector_type FindVec;
split_vector_type SplitOut;
//Split buffer at newlines
ifind_all(FindVec, buffer, "\n");
boost::algorithm::split(SplitOut, buffer, boost::is_any_of("\n"));
unsigned int lines_done = 0;
while(SplitOut.size() > lines_done) {
m4a2mp3(SplitOut[lines_done]);
lines_done++;
}
}
}
You can compile it with `g++ -Wall -I/usr/local/include/boost_1_33_1 -o m4a2mp3 whateveryousavethisfileas.cpp`
You need the boost::algorithm headers to compile it, if you have boost 1.33.0 then change -I/usr/local/include/boost_1_33_1 to -I/usr/local/include/boost_1_33_0