Thread: need to fix argument in the main function, but how ?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    4

    need to fix argument in the main function, but how ?

    Hi all,
    I need to fix some arguments or defualt arguments in the main function below, but i don't know how, all what i've test does not work, thanks for your help. It's ffmpeg.c source and i need to fix input stream and output stream as follow:
    ffmpeg -i rtsp://host/live -f flv rtmp://remotehost/live

    Code:
    int main(int argc, char **argv)
    {
    int i, ret;
    BenchmarkTimeStamps ti;
    
    init_dynload();
    
    register_exit(ffmpeg_cleanup);
    
    setvbuf(stderr,NULL,_IONBF,0);
    
    
    av_log_set_flags(AV_LOG_SKIP_REPEATED);
    parse_loglevel(argc, argv, options);
    
    
    if(argc>1 && !strcmp(argv[1], "-d")){
    run_as_daemon=1;
    av_log_set_callback(log_callback_null);
    argc--;
    argv++;
    }
    
    
    
    #if CONFIG_AVDEVICE
    avdevice_register_all();
    #endif
    avformat_network_init();
    
    show_banner(argc, argv, options);
    
    /* parse options and open all input/output files */
    ret = ffmpeg_parse_options(argc, argv);
    if (ret < 0)
    exit_program(1);
    
    if (nb_output_files <= 0 && nb_input_files == 0) {
    show_usage();
    av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
    exit_program(1);
    }
    
    /* file converter / grab */
    if (nb_output_files <= 0) {
    av_log(NULL, AV_LOG_FATAL, "At least one output file must be specified\n");
    exit_program(1);
    }
    
    for (i = 0; i < nb_output_files; i++) {
    if (strcmp(output_files[i]->ctx->oformat->name, "rtp"))
    want_sdp = 0;
    }
    
    /* current_time = ti = get_benchmark_time_stamps();*/
    if (transcode() < 0)
    exit_program(1);
    if (do_benchmark) {
    int64_t utime, stime, rtime;
    current_time = get_benchmark_time_stamps();
    utime = current_time.user_usec - ti.user_usec;
    stime = current_time.sys_usec - ti.sys_usec;
    rtime = current_time.real_usec - ti.real_usec;
    av_log(NULL, AV_LOG_INFO,
    "bench: utime=%0.3fs stime=%0.3fs rtime=%0.3fs\n",
    utime / 1000000.0, stime / 1000000.0, rtime / 1000000.0);
    }
    
    av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" frames successfully decoded, %"PRIu64" decoding errors\n",
    decode_error_stat[0], decode_error_stat[1]);
    if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1])
    exit_program(69);
    
    exit_program(received_nb_signals ? 255 : main_return_code);
    return main_return_code;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,070
    TBH, I'd do this using a 1-line bash script.
    Code:
    #!/bin/bash
    ffmpeg -i rtsp://host/live -f flv rtmp://remotehost/live $*
    But if you really want to maul the code about, then you could do
    Code:
    char **newargv = malloc(argc+5,sizeof(*newargv));
    newargv[0] = argv[0];
    newargv[1] = "-i";
    newargv[2] = "rtsp://host/live";
    //
    for ( int i = 1, j = 6 ; i < argc ; i++, j++ ) newargc[j] = argv[i];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    4

    cannot compile

    Thank you salemn for your help.

    Yes i don't want a shell script for security reason. I really need to hardcode this in the ffmpeg source.

    When i try the code you gave me i cannot compile:
    Code:
    int main(int argc, char **argv){
        int i, ret;
        BenchmarkTimeStamps ti;
    
    
        char **newargv = malloc(argc+5,sizeof(*newargv));
        newargv[0] = argv[0];
        newargv[1] = "-i";
        newargv[2] = "rtsp://host/live";
    
    
        for ( int i = 1, j = 6 ; i < argc ; i++, j++ ) newargc[j] = argv[i]
    
    
        init_dynload();
    
    
        register_exit(ffmpeg_cleanup);
    
    
        setvbuf(stderr,NULL,_IONBF,0);
    
    
    
    
        av_log_set_flags(AV_LOG_SKIP_REPEATED);
        parse_loglevel(argc, argv, options);
    fftools/ffmpeg.c: In function ‚do_streamcopy‚:
    fftools/ffmpeg.c:2074:5: warning: ‚av_copy_packet_side_data‚ is deprecated [-Wdeprecated-declarations]
    av_copy_packet_side_data(&opkt, pkt);
    ^~~~~~~~~~~~~~~~~~~~~~~~
    In file included from ./libavformat/avformat.h:317:0,
    from fftools/ffmpeg.c:43:
    ./libavcodec/avcodec.h:4463:5: note: declared here
    int av_copy_packet_side_data(AVPacket *dst, const AVPacket *src);
    ^~~~~~~~~~~~~~~~~~~~~~~~
    fftools/ffmpeg.c: In function ‚init_output_stream‚:
    fftools/ffmpeg.c:3545:9: warning: ‚avcodec_copy_context‚ is deprecated [-Wdeprecated-declarations]
    ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
    ^~~
    In file included from ./libavformat/avformat.h:317:0,
    from fftools/ffmpeg.c:43:
    ./libavcodec/avcodec.h:4235:5: note: declared here
    int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src);
    ^~~~~~~~~~~~~~~~~~~~
    fftools/ffmpeg.c:3545:9: warning: ‚codec‚ is deprecated [-Wdeprecated-declarations]
    ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
    ^~~
    In file included from fftools/ffmpeg.c:43:0:
    ./libavformat/avformat.h:885:21: note: declared here
    AVCodecContext *codec;
    ^~~~~
    fftools/ffmpeg.c:3591:9: warning: ‚codec‚ is deprecated [-Wdeprecated-declarations]
    ost->st->codec->codec= ost->enc_ctx->codec;
    ^~~
    In file included from fftools/ffmpeg.c:43:0:
    ./libavformat/avformat.h:885:21: note: declared here
    AVCodecContext *codec;
    ^~~~~
    fftools/ffmpeg.c: In function ‚check_keyboard_interaction‚:
    fftools/ffmpeg.c:3974:13: warning: ‚codec‚ is deprecated [-Wdeprecated-declarations]
    debug = input_streams[0]->st->codec->debug<<1;
    ^~~~~
    In file included from fftools/ffmpeg.c:43:0:
    ./libavformat/avformat.h:885:21: note: declared here
    AVCodecContext *codec;
    ^~~~~
    fftools/ffmpeg.c:3997:13: warning: ‚codec‚ is deprecated [-Wdeprecated-declarations]
    input_streams[i]->st->codec->debug = debug;
    ^~~~~~~~~~~~~
    In file included from fftools/ffmpeg.c:43:0:
    ./libavformat/avformat.h:885:21: note: declared here
    AVCodecContext *codec;
    ^~~~~
    fftools/ffmpeg.c: In function ‚main‚:
    fftools/ffmpeg.c:4848:22: error: too many arguments to function ‚malloc‚



    Quote Originally Posted by Salem View Post
    TBH, I'd do this using a 1-line bash script.
    Code:
    #!/bin/bash
    ffmpeg -i rtsp://host/live -f flv rtmp://remotehost/live $*
    But if you really want to maul the code about, then you could do
    Code:
    char **newargv = malloc(argc+5,sizeof(*newargv));
    newargv[0] = argv[0];
    newargv[1] = "-i";
    newargv[2] = "rtsp://host/live";
    //
    for ( int i = 1, j = 6 ; i < argc ; i++, j++ ) newargc[j] = argv[i];

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,070
    Yeah, should be
    malloc((argc+5)*sizeof(*newargv));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    4
    got sorry ..... still errors on compile:

    fftools/ffmpeg.c:4850:52: error: ‚newargc‚ undeclared (first use in this function); did you mean ‚newargv‚?
    for ( int i = 1, j = 3 ; i < argc ; i++, j++ ) newargc[j] = argv[i]
    ^~~~~~~
    newargv
    fftools/ffmpeg.c:4850:52: note: each undeclared identifier is reported only once for each function it appears in
    fftools/ffmpeg.c:4851:5: error: expected ‚;‚ before ‚int‚
    int i, ret;
    ^~~
    fftools/ffmpeg.c:4852:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
    BenchmarkTimeStamps ti;
    ^~~~~~~~~~~~~~~~~~~
    fftools/ffmpeg.c:4882:5: error: ‚ret‚ undeclared (first use in this function); did you mean ‚creat‚?
    ret = ffmpeg_parse_options(argc, argv);
    ^~~
    creat
    fftools/ffmpeg.c:4898:10: error: ‚i‚ undeclared (first use in this function); did you mean ‚ti‚?
    for (i = 0; i < nb_output_files; i++) {
    ^
    ti

  6. #6
    Registered User
    Join Date
    Mar 2020
    Posts
    12
    The error about newargc is very clear, I think you can fix the typo yourself. And for the other errors, I believe those can be fixed by moving the declarations to the beginning of function.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,070
    Converting C99 code to C90 is left as an exercise for the reader.

    Since you didn't say "I'm a completely clueless noob", I assumed a basic modicum of skill.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Pass Main Fuction argument to some Other Function
    By SimplySud in forum C Programming
    Replies: 3
    Last Post: 12-26-2014, 08:49 AM
  2. Replies: 6
    Last Post: 05-08-2014, 10:57 AM
  3. Replies: 2
    Last Post: 06-04-2013, 08:25 AM
  4. Template Argument inside the Argument of a Function Declaration
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2011, 05:53 AM
  5. Replies: 3
    Last Post: 06-01-2011, 03:08 AM

Tags for this Thread