List image in two different sections with different templates

Wrap your code in an if else statement:

    'hooks' => [
      'file.create:after' => function ($file) {
        // change cover template to media, so images are also listed in the gallery
        if ($file->template() == 'cover') {
          $file->update([
            'template' => 'media'
          ]);
        } else {

        $filetype = $file->type();
  
        if($filetype === 'video') {
          try {
            exec('ffmpeg -y -i ' . $file->root() . ' -ss 3 -t 1 ' . $file->parent()->root() . '/' . F::name($file->filename()) . '.jpg');
  
          } catch(Exception $e) {
            echo 'Video thumbnail could not be extracted.';
            echo $e->getMessage();
  
          }
  
        }
  
        $file->update(['filetype' => $filetype]);
        
        }
  
      }
    ],

and it should work.

Ok, now it works, thank you! I just wonder, is this not some sort of bug? Should it not be possible to use more than one update statement within one hook?

No, it’s not a bug.

The if-else make sense in this case, alternatively, you can place your second update within the second if

 if($filetype === 'video') {
          try {
            exec('ffmpeg -y -i ' . $somefile->root() . ' -ss 3 -t 1 ' . $file->parent()->root() . '/' . F::name($somefile->filename()) . '.jpg');
  
          } catch(Exception $e) {
            echo 'Video thumbnail could not be extracted.';
            echo $e->getMessage();
  
          }
          $file->update(['filetype' => $filetype]);

        }

No, I need the filetype thing for all files, but I structured it in this way now:

[
    'file.create:after' => function ($file) {
      // change cover template to media, so images are also listed in the gallery
      $filetype = $file->type();

      if ($file->template() == 'cover') {
        $file->update([
          'template' => 'media',
          'filetype' => $filetype
        ]);

      } else {

        if($filetype === 'video') {
          try {
            exec('ffmpeg -y -i ' . $file->root() . ' -ss 3 -t 1 ' . $file->parent()->root() . '/' . F::name($file->filename()) . '.jpg');

          } catch(Exception $e) {
            echo 'Video thumbnail could not be extracted.';
            echo $e->getMessage();

          }

        }

        $file->update(['filetype' => $filetype]);
      }
    }
  ]

However, when I update the template to media via the hook, it does change the template value in the meta file, but it does not write all the fields of the meta template to this file. I thought maybe I could place a $file->save() statement after the $file->update() to enforce this, but not only does this not work, this even makes the change of templates not work anymore. Is there any way to force updating all the fields in the meta file to the new template?

What fields? At the time of upload there shouldn’t be any fields yet.

If I upload an image file to my gallery section (which uses the media template), the meta file gets auto-populated with all the template’s fields (most of them empty at this point in time).

Now, is there a way to auto-populate the image file that I upload in the cover field and then assign the media template to it with all the media template fields? (Example, why I need this: I have the Focus plugin installed which writes a field Focus: {"x":0.5,"y":0.5} during the auto-population, so it would be good to have such fields after I convert the template from cover to media).

But you can do that in your hook? Instead of only updating the template, you can populate as many fields as you want. You already add the filetype.

Ah, yes, that was now a bit too obvious for me to see. :wink:
Ok, I think now I should have all my problems regarding this actually solved. This was a long one. Thank you so much for bearing with me, @texnixe. I can’t praise the Kirby support enough!