Hi guys,

I need your help in creating a video with thumbnails. What I want is that, if I upload a video it will create automatically a thumbnail picture for it.
I'm just new to php and I don't have any idea of this.
I really need your help about this.
Thank you in advance guys.

    Hope this link helps.

    Although, I must say that, being a beginner and all, you sure pick some advanced topics. Well, good for you. 😃

      Thank you so much for sharing you knowledge.
      However I still didn't manage to have the thumbnail image.😕
      Since I'm using windows, I followed this link http://ffdshow.faireal.net/mirror/ffmpeg/ and got the latest
      I extracted it to my root folder with the folder name ffmpeg.
      version available.
      Here's what I did:

      <?php
      
      // where ffmpeg is located, such as /usr/sbin/ffmpeg
      $ffmpeg = '\ffmpeg';
      
      // the input video file
      $video  = dirname(__FILE__) . '\videos\samplevideo.mpg';
      
      // where you'll save the image
      $image  = dirname(__FILE__) . '\thumbnails\demo.jpg';
      
      // default time to get the image
      $second = 1;
      
      // get the duration and a random place within that
      $cmd = "$ffmpeg -i $video 2>&1";
      if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', $cmd, $time)) {
          $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
          $second = rand(1, ($total - 1));
      }
      
      // get the screenshot
      //$cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
      $return = $cmd;
      
      // done! <img src="http://blog.amnuts.com/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley">
      echo 'done!';
      
      ?>
      

      I'm just wondering, I found three files inside the ffmpeg folder. Do I need to do something about that? or just leave it and just point the location of this folder in the code $ffmpeg .

        What doesn't work?
        What errors do you get?
        You found what 3 files?
        Does output redirection really work on windows? If it doesn't, or if you don't know, run the executable manually and see what it says.

          johanafm;10960604 wrote:

          What doesn't work?
          What errors do you get?
          You found what 3 files?
          Does output redirection really work on windows? If it doesn't, or if you don't know, run the executable manually and see what it says.

          I didn't get any error.
          I found ffmpeg.exe, ffplay.exe and pthreadGC2.dll.
          I tried to run the ffmpeg.exe, it opens the msdos box but it's very fast, I can't read what's inside and it closed immediately.

          Can you please help me what should I do for this?

            You're actually making a shell command.

            // so this:
            $return = $cmd; 
            
            //should be this:
            $return = `$cmd`; 
            // those are backticks, not single quotes!
            

            I don't know about all 3 files, but I surely know you're using ffmpeg.exe, because you're executing it.

            Be carefull with being a beginner and executing shell commands. It's not a safe couple.

            LE:

            tinood wrote:

            I tried to run the ffmpeg.exe, it opens the msdos box but it's very fast, I can't read what's inside and it closed immediately.

            Oh, boy. Here, try this:

            • Start -> Run...

            • write "cmd" and press enter

            • go to where "ffmpeg.exe" is

            • type "ffmpeg" then press enter

            You really need to get back to the basics. You'll be doing yourself a favor.

            johanafm wrote:

            Does output redirection really work on windows?

            Yeah, it does, with ">" and ">>". But op doesn't need that.

              Because he's running on windows. Forward slashes work as well though, and I prefer using them since it works on all platforms and then it doesn't matter if you use single or double quoted string literals.

                Start -> Run...
                write "cmd" and press enter
                go to where "ffmpeg.exe" is
                type "ffmpeg" then press enter

                I tried to manage on what you told me and executed it and I don't find any problem.
                But when I try to run my code still I can't get the thumbnail image.

                I don't know really what's the problem with this.

                Is it ok if you can test my code and just give me the working code. 🙂

                Thank you so much in advance. I really need this.

                  Is it ok if you can test my code and just give me the working code

                  Actually, that kind of help I'm not willing to give.

                  I really need this.

                  2 cents for every time I read this and I'd buy Google. We all need some code really fast and really functional.

                  You can't just drop from ms word to php and start advanced scripting. Only if you have some other programming skill to back you up, which I doubt it.

                  Tell you what, though: you give me the right answer for next problem and I'll try to help you.
                  Problem: how many mistakes are in the following code?

                  <php?
                  
                  function fact(n) {
                      if ($n = 1 || $n = 0) {
                          return 1
                      } else {
                          return ( $n * factorial(n-1) )
                  }
                  
                  ?>

                  I'm not a cold-hearted bastard; it's for your own good, trust me.
                  Anyways, I'm sure someone will call me a pretentious prick, and he'll help you. 'Til then, you're stuck with me.

                    nevvermind;10960618 wrote:

                    Problem: how many mistakes are in the following code?

                    Does logical errors and lack of bounds and type checking count, i.e. are you implementing a function for n! ? 😉

                    Program Execution Functions is a good place to start reading. You may wish to read user comments as well, since there are other things than just syntax that you need to understand as well (such as php configuration consequences).
                    Backtick operator info is found here and more on executing shell command can be found at exec which in turns links to passthru, escapeshellarg and escapeshellcmd.

                    If you can get ffmpeg to work from CLI, then you should be able to make it work using the above, assuming of course that you understand the above documentation and that your php installation allows shell execution under the conditions used.

                      nevvermind;10960618 wrote:

                      Actually, that kind of help I'm not willing to give.

                      2 cents for every time I read this and I'd buy Google. We all need some code really fast and really functional.

                      You can't just drop from ms word to php and start advanced scripting. Only if you have some other programming skill to back you up, which I doubt it.

                      Tell you what, though: you give me the right answer for next problem and I'll try to help you.
                      Problem: how many mistakes are in the following code?

                      <php?
                      
                      function fact(n) {
                          if ($n = 1 || $n = 0) {
                              return 1
                          } else {
                              return ( $n * factorial(n-1) )
                      }
                      
                      ?>

                      I'm not a cold-hearted bastard; it's for your own good, trust me.
                      Anyways, I'm sure someone will call me a pretentious prick, and he'll help you. 'Til then, you're stuck with me.

                      1. <php? -> <?php
                      2. (n) -> ($n)
                      3. ($n = 1 || $n = 0) -> ($n == 1 || $n == 0)
                      4. return 1 -> return 1;
                      5. (n-1) -> ($n-1)
                      6. lacking with ; -> ( $n * factorial($n-1) );
                      7. lacking with { -> return ( $n * factorial($n-1) ); }

                        You've single handedly managed to work out those errors. I'll believe you.
                        So, you're new code looks like this:

                        <?php
                        
                        function fact($n) {
                            if ($n == 1 || $n == 0) {
                                return 1;
                            } else {
                                return ($n * factorial($n-1));
                            }
                        }
                        
                        ?>

                        Admittedly, this is error-free, but what does it do? It's functional?
                        Now try putting "echo fact(5);" below that code. Ups. You're not using that function, that's why no errors show up. You've been warned. Remember: this is not me being ****y and infatuated, this is you not knowing basic php.

                        I'll look into it and try to help you.


                        Later Edit
                        OK. Here's what to do:

                        1. Make a folder. Its name doesn't matter, but we'll name it "tinood" for guidance 😃

                        2. Copy those 3 downloaded files into "tinood" folder (pthreadGC2.dll and ffmpeg.exe are necessary)

                        3. Find a .mpg file and copy it to "tinood" (I've searched for a mpg in my comp and stumbled upon a 3 mb one: "demo.mpg". I used this in my example code. Change it to your own!)

                        4. Make a php file in "tinood" and populate it with the code below:

                          <?php
                          
                          $ffmpeg = 'ffmpeg.exe';
                          
                          // change "demo.mpg" to your mpg file name!
                          $video  = dirname(__FILE__) . '\demo.mpg';
                          
                          // change "demo.jpg" to whichever name you like or don't
                          // for this example, the name of the output jpg file does not matter 
                          $image  = dirname(__FILE__) . '\demo.jpg';
                          
                          $second = 1;
                          
                          $cmd = "$ffmpeg -i $video 2>&1";
                          if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
                              $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
                              $second = rand(1, ($total - 1));
                          }
                          
                          $cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
                          $return = `$cmd`;
                          
                          echo 'done!';
                          
                          ?>
                        5. Run the php file server-wise. It should echo "done!"

                        6. Now check "tinood" folder. There should be a demo.jpg

                        Now you can play with that file. Tamper with ffmpeg parameters. It's pretty straightforward.

                        If you want a small picture (thumbnail, 320x240), try this (I haven't):

                        //change this:
                        $cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
                        
                        //...to this:
                        $cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -s 320x240 -vcodec mjpeg -f mjpeg $image 2>&1";

                        Hope this helps. Am I a bad person, tinood? :rolleyes:

                        BTW: If you bump into troubles using this code, do be specific about errors. I can't help you if you reply: "Nope. Doesn't work".

                          Write a Reply...