Skip to content

Instantly share code, notes, and snippets.

@cjhanks
Created March 31, 2012 03:12
Show Gist options
  • Save cjhanks/2258905 to your computer and use it in GitHub Desktop.
Save cjhanks/2258905 to your computer and use it in GitHub Desktop.
ffmpeg parsing
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
/**
* This simple argv parser splits a command like:
* ffmpeg -f libx264 -i input.avi -acodec libmp3lame -ab 128k -ar 44100 -vcodec
* mpeg2video -vf scale=160:128 -b 176k -r 15 -strict -1 -ab output.mpg
*
* Into its 4 constituent parts.
*
* - preamble + input_file_options
* - input file name
* - output_file_options
* - output file name
*
* This should work for any ffmpeg command, as this is a requisite of the ffmpeg
* application.
*
* Christopher J. Hanks <cjhanks@cpusage.com>
*/
int
main(int argc, char* argv[])
{
std::string input;
std::string output;
std::string inp_args;
std::string out_args;
bool in_args = true;
bool in_trigger = false;
for (int i=1; i<argc; ++i)
{
if (in_trigger)
{
input = argv[i];
in_trigger=in_args=false;
}
else if (i == argc-1)
output = argv[i];
else if (strcmp(argv[i],"-i") == 0)
in_trigger = true;
else
{
if (in_args)
inp_args += std::string(argv[i])+" ";
else
out_args += std::string(argv[i])+" ";
}
}
std::cerr<<"Input: "<<input<<std::endl;
std::cerr<<"Output: "<<output<<std::endl;
std::cerr<<"ArgsIn: " << inp_args <<std::endl;
std::cerr<<"ArgsOut:" << out_args <<std::endl;
std::cerr<<" ----------------------------"<<std::endl;
std::cerr<<inp_args << " " << input << " " << out_args << " " <<output << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment