2

I was wondering how I can write metadata/properties (comments specifically and other information) to a video file (mainly .mp4), using PHP.

Properties/metadata of a video file - Windows

Here is my function that I use to download a remote file ($sourceURL) to a local destination ($destinationURL). I am able to rename the file with the $destinationURL (just end it with /video.mp4). I am currently using cURL, but if it can only be done by something like fopen()/fwrite(), then I am open to suggestions :)

function downloadFile( $sourceURL, $destinationURL ) {
    $options = array(
        CURLOPT_FILE => is_resource( $destinationURL ) ? $destinationURL : fopen( $destinationURL, 'w' ),
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_URL => $sourceURL,
        CURLOPT_FAILONERROR => true,
    );

    $ch = curl_init();
    curl_setopt_array( $ch, $options );
    $return = curl_exec( $ch );

    if ( $return === false ) {
        return curl_error( $ch );
    } else {
        return true;
    }
}

What I have tried:

  1. getID3(): http://getid3.sourceforge.net/ Perhaps I am missing something, but when I try to write to an mp4 file, it gives me this error:

Error message from getID3 - 1

Changing the tag format to "id3v1" doesn't work either.

Error message from getID3 - 2

If I'm not mistaken, the tag format's "id3v2.3" and "id3v1" only work with .mp3 files.

  1. Answers from: writing exif data in php - These only seem to work for image files

Thank you so much for your time and I look forward to any comments/answers I may receive.

Kind Regards

Joshua Lochner

Joshua Lochner
  • 153
  • 1
  • 9

2 Answers2

3

You can use ffmpeg to extract/add metadata from/to videos, i.e.:

To Extract:

ffmpeg -i video.mp4 -f video_metadata.txt 

To Add:

ffmpeg -i video.mp4 -metadata title="my title" video_metadata.mp4

ffmpeg documentation

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
3

exiftool will help you a lot. See https://owl.phy.queensu.ca/~phil/exiftool/examples.html

On most Debian based distro's you can install this with sudo apt-get install exiftool.

After this you could use shell_exec in PHP with the example command in the link above.

 $output = shell_exec('exiftool yourfile.mp4');

Reading and Writing is possible with exiftool see the examples.

Small example copied from the site i linked:

4) Write multiple tags

exiftool -artist="Phil Harvey" -copyright="2011 Phil Harvey" a.jpg

So this means:

$command = 'exiftool -artist="Phil Harvey" -copyright="2011 Phil Harvey" a.jpg';
$output = shell_exec($command);

Of course this example is applicable to video files too.

sietse85
  • 1,488
  • 1
  • 10
  • 26
  • Thank you for your answer. I really appreciate it. Sorry for the silly question, but how would I go about getting this working on my local, xxamp, server? – Joshua Lochner Oct 15 '18 at 12:49
  • on which operating system? – sietse85 Oct 15 '18 at 12:53
  • My operating system is Windows 10 – Joshua Lochner Oct 15 '18 at 12:54
  • the exiftool website offers a standalone executable for windows, you need to download it store it somewhere and set in in your windows PATH environment variable – sietse85 Oct 15 '18 at 12:54
  • Okay thank you. I will try that and get back to you. Much appreciated. – Joshua Lochner Oct 15 '18 at 12:58
  • Okay, update :) ... So, I've got it working for .jpg files, as they show in their examples. but it doesn't seem to work for .mp4 videos. I mean, it does say mp4 videos are supported, but I've tried and although it says it has written something, I check the file and the properties remain unchanged. – Joshua Lochner Oct 15 '18 at 13:26
  • i found this https://stackoverflow.com/questions/41580820/edit-mp4-metadata-with-exiftool other then xmp tags exiftool has limited support – sietse85 Oct 15 '18 at 13:28
  • In other words also consider Pedro's answer. I wonder who downvoted that, for MP4 only it is a good option. – sietse85 Oct 15 '18 at 13:30
  • okay, I see... I'll try that. Thank you for your help. :) I assume the installation process would be similar. – Joshua Lochner Oct 15 '18 at 13:33