Question: Our database contains information about the languages used by businesses / service providers, and we have a section for "Bilingual" but we need to change that to be two separate items: "English" and "Spanish". The database is structured as a relational table:

| table | business |
| --- | --- |
| int | id |
| varchar | business\_name |

| table | business\_language |
| --- | --- |
| int (FK) | business\_id |
| int (FK) | language\_id |

| table | languages |
| --- | --- |
| int | id |
| varchar | language\_name |

We are using Laravel for the backend of the web-app, so if we were to query the DB and get the languages it would look like:

```
$business = Business::all();
foreach($business as $b) {
    //gives us a collection of languages 
    $b->language();
}

```

What I am trying to do is write a one-time SQL query that finds all instances of a business having "Bilingual" as a language, add "English" and "Spanish" as the language (if it doesn't already exist), and delete "Bilingual".

Is such a one-time-query possible, or do I have to write a script to do it for me for each business?

Answer: 
Concerning the FFMpeg part, I think a good way to start is to actually use the [PHP-FFMpeg](https://github.com/PHP-FFMpeg/PHP-FFMpeg) library. The [Basic Usage section](https://github.com/PHP-FFMpeg/PHP-FFMpeg#basic-usage) in the documentation contains an example on how to generate a frame for a given video:

```php
require 'vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');

$video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
      ->save('frame.jpg');

```

A simplified process would be as follows:

1. The user uploads a video, after which the video gets moved to a different
directory.
2. Now you can use the snippet above, with the `frame` method to get a thumbnail for your video.
3. After the image saving is done, you just need to add it to your database.
	* If the thumbnails refer to the `image` column in your table, you can get away with just inserting the filename, `frame.jpg` (or even the complete filepath, `/public/path/to/frame.jpg`).
	* If the thumbnails refer to the `screenshots` column in your table, and you want to have multiple thumbnails for your video, then you should consider creating a new table with a one-to-many relationship (from your video/application to a new table, e.g. `thumbnails`)
4. Then when the user gets to a page where the image should be displayed, just select it from the table and display it with an `<img>` tag (with the **public** filepath).

I would also strongly recommend not to save the complete `<video>` tag into your database, but instead add it to the page where you actually want to show your video.

Example:

```php
<?php
$result = $conn->query('SELECT ...');
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        ?>
        <video src="<?php echo $row['video-column-path']; ?>"</video>
        <?php
    }
} else {
    ?>
    No videos here
    <?php
}
$conn->close();
?>

```
Found solution, now need to understand how to import generated thumbnail url to database field for video...

```html
        // Velidate if files exist
        if (!empty(array_filter($_FILES['fileUpload']['name']))) {

            // Loop through file items
            foreach($_FILES['fileUpload']['name'] as $title=>$val){
                // Get files upload path
                $fileName        = $_FILES['fileUpload']['name'][$title];
                $tempLocation    = $_FILES['fileUpload']['tmp_name'][$title];
                $targetFilePath  = $uploadsDir . $fileName;
                $fileType        = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION));
                $withOutExtension = pathinfo($fileName, PATHINFO_FILENAME);
                $uploadDate      = date('Y-m-d H:i:s');
                $uploadOk = 1;
                $randomID = rand(1000, 999999);
//Get one thumbnail from the video
        $ffmpeg = "C:\\ffmpeg\\bin\\ffmpeg";
        //echo $ffmpeg;
        $imageFile = 'pic/thumb_'.time().'_'.$randomID.'.jpg';
        $size = "120x90";
        $getFromSecond = 1;

  echo $cmd = "$ffmpeg -i $tempLocation -an -ss $getFromSecond -s $size $imageFile";

  echo "<br>";
  if(!shell_exec($cmd)){
   echo "Thumbnail Created!";
  }else{
   echo "Error creating Thumbnail";
  }
```