On Wed, 14 Jul 2004, Jérémie Knuesel wrote:

> Hello,
> 
> How can I skip the first frame in a yuv stream?
> 
> I pipe the data from mplayer through y4mscaler to mpeg2enc. The problem
> is that mplayer often puts a plain green frame at the beginning of the
> stream. How can I skip it in the encoding?

Here is a utility that I use to trim yuv4mpeg streams. Link it with 
-lmjpegutils and try 'y4mcut -h' for usage.

Selva
/*
 *    Copyright (C) 2001 Selva Nair <[EMAIL PROTECTED]>
 *
 *    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
 *
 */
/* Build instructions
 * gcc -Wall -O2 -DHAVE_STDINT_H -I /usr/local/include/mjpegtools -o y4mcut \
 *      y4mcut.c -lmjpegutils
 */ 
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <mjpegtools/yuv4mpeg.h>
#include <mjpegtools/mjpeg_logging.h>

static void usage(char *name )
{
        fprintf(stderr,
                       "A filter to select specified number of YUV4MPEG\n"
                       "frames from input stream. Reads input from stdin\n"
                       "and outputs to stdout as a YUV4MPEG stream.\n"
                       "Usage: %s: [-h] [-v num] [-s num] [-o num]\n"
                       "           -h     - print this help\n"
                       "           -v     - verbosity [0..2]\n\n"
                       "           -s num - skip num frames (0)\n"
                       "           -o num - output num frames only -- use\n"
                       "              a negative  number for all (-1)\n"
                        "Author: Selva Nair\n", name);
}

int main(int argc, char *argv[])
{
        int verbose = 1;
        int nskip = 0;
        int nout = -1;

        char *prog = "y4mcut";
        int     i;
        int     input_fd = 0;
        int     output_fd = 1;
        int     c;
        int     frames_in, frames_out;
        int     horz, vert;
        unsigned char   *frame[3];
        y4m_stream_info_t istream;
        y4m_frame_info_t iframe;

        while((c = getopt(argc, argv, "hv:s:o:")) != EOF) {
                switch(c) {
                case 'v':
                        verbose = atoi (optarg);
                        if( verbose < 0 || verbose >2 )
                        {
                                usage (argv[0]);
                                exit (1);
                        }
                        (void)mjpeg_default_handler_verbosity(verbose);
                        break;            
                        
                case 'h':
                        usage (argv[0]);
                        exit(0);                  
                case 's':
                        if(sscanf(optarg, "%d", 
                             &nskip) != 1) usage(argv[0]);
                        break;
                case 'o':
                        if(sscanf(optarg, "%d", 
                             &nout) != 1) usage(argv[0]);
                        break;
                default:
                        exit(0);
                }
        }
        
        y4m_init_stream_info(&istream);
        y4m_init_frame_info(&iframe);

        i = y4m_read_stream_header(input_fd, &istream);
        i = y4m_write_stream_header(output_fd, &istream);
        if (i != Y4M_OK)
                mjpeg_error_exit1("Input stream error: %s", y4m_strerr(i));

        horz = istream.width;
        vert = istream.height;
        frame[0] = alloca(horz * vert);
        frame[1] = alloca((horz / 2) * (vert / 2));
        frame[2] = alloca((horz / 2) * (vert / 2));

        frames_in = 0;
        frames_out = 0;
        while (y4m_read_frame(input_fd, &istream, &iframe, frame) == Y4M_OK) {
           frames_in++;
           if(frames_in <= nskip) continue;
           if (nout < 0 || frames_out < nout ) {
             if(y4m_write_frame(output_fd, &istream, &iframe, frame) == Y4M_OK) 
frames_out++;
             else {
                    mjpeg_warn("%s: error writing output frame", prog);
                    break;
            }
           }
           else {
                 mjpeg_info("%s: Skipped %d frames", prog, nskip);
                 mjpeg_info("%s: Output %d frames", prog, frames_out);
                 break;
           }
       }
       if(nout > 0 && frames_out < nout ) mjpeg_warn("%s: not enough frames in input", 
prog);

       y4m_fini_stream_info(&istream);
       y4m_fini_frame_info(&iframe);
       exit(0);
}

Reply via email to