create video screen cap with PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karl Groves

    create video screen cap with PHP

    Does anyone out there know of a way to capture a frame (at random, if
    possible) of a movie file with PHP?

    TIA

    --
    Karl Groves

  • Andy Jeffries

    #2
    Re: create video screen cap with PHP

    On Sun, 13 Aug 2006 16:44:11 -0500, Karl Groves wrote:
    Does anyone out there know of a way to capture a frame (at random, if
    possible) of a movie file with PHP?
    The easiest way will be to call out to mplayer to do it. You'll have to
    do it twice, once to get the length of the movie (and then optionally a
    second time if you don't want the first frame).

    mplayer -identify -ss 20:00 -vo jpeg -ao null -frames 1 video.avi

    This will print out (amongst many many other things):

    ID_LENGTH=4498. 26

    This ID_LENGTH is the the length of the movie in seconds.

    Calling mplayer like this will create a file called 000001.jpg which
    you'll need to rename (and implement some locking PHP-side to ensure you
    don't run two mplayer instances simultaneously which will whack the same
    file).

    So, along with exec($cmdline, &$output) you have all you need :-)

    Cheers,


    Andy


    --
    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
    http://www.gphpedit.org | PHP editor for Gnome 2
    http://www.andyjeffries.co.uk | Personal site and photos

    Comment

    • Andy Jeffries

      #3
      Re: create video screen cap with PHP

      On Tue, 15 Aug 2006 12:48:41 +0000, Andy Jeffries wrote:
      mplayer -identify -ss 20:00 -vo jpeg -ao null -frames 1 video.avi
      Sorry, forgot to mention -ss mm:ss is the offset. Here I use 20 minutes
      in, but you should call it with 00:00 initially to get the first frame in
      case the movie isn't 20 minutes long ;-)

      Cheers,


      Andy

      --
      Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
      http://www.gphpedit.org | PHP editor for Gnome 2
      http://www.andyjeffries.co.uk | Personal site and photos

      Comment

      • Karl Groves

        #4
        Re: create video screen cap with PHP

        Andy Jeffries <news@andyjeffr ies.co.ukwrote in
        news:pan.2006.0 8.15.12.48.50.5 0049@andyjeffri es.co.uk:
        On Sun, 13 Aug 2006 16:44:11 -0500, Karl Groves wrote:
        >Does anyone out there know of a way to capture a frame (at random, if
        >possible) of a movie file with PHP?
        >
        The easiest way will be to call out to mplayer to do it. You'll have to
        do it twice, once to get the length of the movie (and then optionally a
        second time if you don't want the first frame).
        >
        mplayer -identify -ss 20:00 -vo jpeg -ao null -frames 1 video.avi
        >
        This will print out (amongst many many other things):
        >
        ID_LENGTH=4498. 26
        >
        This ID_LENGTH is the the length of the movie in seconds.
        >
        Calling mplayer like this will create a file called 000001.jpg which
        you'll need to rename (and implement some locking PHP-side to ensure you
        don't run two mplayer instances simultaneously which will whack the same
        file).
        >
        So, along with exec($cmdline, &$output) you have all you need :-)
        >

        Thanks for the excellent response.
        Do you have, perhaps, a more comprehensive example? I don't have much
        experience with using exec(), except for using it to perform ImageMagick
        commands.



        --
        Karl Groves

        Comment

        • Andy Jeffries

          #5
          Re: create video screen cap with PHP

          On Tue, 15 Aug 2006 09:01:09 -0500, Karl Groves wrote:
          To be honest, you should have everything you need above and my normal
          response would be RTFM/try it and come back with some code even if it has
          problems for us to fix.

          However, I'm in a good mood so below is some code I've just knocked and
          had a quick test with. It works fine. The locking code could do with a
          usleep/while loop to keep trying in the event of a lock failure, but the
          basics are surely close enough now for you.

          It also gets round a bug where (on the movie I was testing it on at least)
          the first frame image is always the start of the movie, but the second
          frame is the real one.

          #!/usr/bin/env php
          <?php

          function movie_get_lengt h_in_seconds($m ovie_filename)
          {
          $cmd = "mplayer -identify -vo null -ao null -frames 1 ".
          "$movie_filenam e 2>&1 | grep ID_LENGTH";
          $output = exec($cmd);
          list(,$length) = explode("=", $output); return $length;
          }

          function movie_take_scre enshot($movie_f ilename, $time_in_second s,
          $image_filename )
          {
          if (mkdir(".movie. lck")) { // Try to get a lock
          $cmd = "mplayer -ss $time_in_second s -vo jpeg ".
          "-ao null -frames 2 $movie_filename " 2>&1 | ".
          "grep ID_LENGTH";
          exec($cmd);
          rename("0000000 2.jpg", "$image_filenam e");
          unlink("0000000 1.jpg");
          rmdir(".movie.l ck"); // Release lock
          return true;
          }
          return false; // Could not obtain lock
          }

          $movie_filename = "test.mpg";
          $movie_length = movie_get_lengt h_in_seconds($m ovie_filename);
          $random_seconds = rand(0, floor($movie_le ngth));
          $was_image_crea ted = movie_take_scre enshot($movie_f ilename,
          $random_seconds , "test_image.jpg ");
          ?>

          Cheers,


          Andy


          --
          Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
          http://www.gphpedit.org | PHP editor for Gnome 2
          http://www.andyjeffries.co.uk | Personal site and photos

          Comment

          • Andy Jeffries

            #6
            Re: create video screen cap with PHP

            On Wed, 16 Aug 2006 11:55:35 +0000, Andy Jeffries wrote:
            function movie_get_lengt h_in_seconds($m ovie_filename) {
            $cmd = "mplayer -identify -vo null -ao null -frames 1 ".
            "$movie_filenam e 2>&1 | grep ID_LENGTH";
            $output = exec($cmd);
            list(,$length) = explode("=", $output); return $length;
            Somewhere along the posting route, it seems to have snipped some line
            endings, but I'm sure you can re-prettify my code :-)

            Cheers,


            Andy

            --
            Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
            http://www.gphpedit.org | PHP editor for Gnome 2
            http://www.andyjeffries.co.uk | Personal site and photos

            Comment

            • Karl Groves

              #7
              Re: create video screen cap with PHP

              Andy Jeffries <news@andyjeffr ies.co.ukwrote in
              news:pan.2006.0 8.16.11.56.56.9 32617@andyjeffr ies.co.uk:
              On Wed, 16 Aug 2006 11:55:35 +0000, Andy Jeffries wrote:
              >function movie_get_lengt h_in_seconds($m ovie_filename) {
              > $cmd = "mplayer -identify -vo null -ao null -frames 1 ".
              > "$movie_filenam e 2>&1 | grep ID_LENGTH";
              > $output = exec($cmd);
              > list(,$length) = explode("=", $output); return $length;
              >
              Somewhere along the posting route, it seems to have snipped some line
              endings, but I'm sure you can re-prettify my code :-)
              >
              Cheers,
              >
              Thanks for this. I'll give it a shot.



              --
              Karl Groves

              Comment

              • ImOk

                #8
                Re: create video screen cap with PHP


                Andy Jeffries wrote:
                The easiest way will be to call out to mplayer to do it. You'll have to
                do it twice, once to get the length of the movie (and then optionally a
                second time if you don't want the first frame).
                >
                mplayer -identify -ss 20:00 -vo jpeg -ao null -frames 1 video.avi
                Sorry for my ignorance, is mplayer a Linux utility or Windows?

                Comment

                • Andy Jeffries

                  #9
                  Re: create video screen cap with PHP

                  On Thu, 17 Aug 2006 05:15:29 -0700, ImOk wrote:
                  >The easiest way will be to call out to mplayer to do it. You'll have to
                  >do it twice, once to get the length of the movie (and then optionally a
                  >second time if you don't want the first frame).
                  >>
                  >mplayer -identify -ss 20:00 -vo jpeg -ao null -frames 1 video.avi
                  >
                  Sorry for my ignorance, is mplayer a Linux utility or Windows?
                  Primarily Linux, but there's a Windows version too:



                  Don't forget to get the codecs pack if you want to use WMV/MOV etc.

                  Cheers,


                  Andy

                  --
                  Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                  http://www.gphpedit.org | PHP editor for Gnome 2
                  http://www.andyjeffries.co.uk | Personal site and photos

                  Comment

                  Working...