PHP uses getID3 to obtain information about media files such as the playback duration of audio and video

cause
Recently, when I was working at home, I encountered some operations that need to process some video files. There are many interfaces for processing pictures in a cloud storage that the company has been using. However, some video processing needs to open other services. After making a circle, I'd better decide to process it myself to save the company some money.

When PHP processes video and other media files locally, it usually uses "ffmpeg", but the code is a little too old, and there is a moth in the extension installation. I'm tired all afternoon.

Later, we found that getID3 can also be used to process relevant media file information. This is a PHP script. You only need to simply introduce the project without installing additional extensions or tools. But the problem comes again. The download speed abroad is dead slow. I cried to death after downloading 1.4M things for more than an hour.

download

getID3 official address: https://www.getid3.org

Github download address: https://github.com/JamesHeinrich/getID3/archive/master.zip

SourceForge download address: https://sourceforge.net/projects/getid3/

Tencent cloud download address: https://reggie-1258048181.cos.ap-guangzhou.myqcloud.com/web/getID3-master.zip

Alternate download address: https://www.tokme.cn/images/web/getID3-master.zip

use

Because this is not a standard package conforming to PSR-4 specification (there is no namespace, and the class name is not very good. I didn't look at it carefully). If it is to be used in the project, directly add the whole getid3 folder, and then introduce getid3 / getid3 PHP. If it's in thinkphp5 1 can be introduced in this way.
Unzip the file and put the getid3 folder under the project. I put it under extend (there are many useless files in the package that can be deleted)
Finally, it can be introduced where it needs to be used

/**Import class library*/
require "../extend/getid3/getid3.php";

The following four demo s

 // Method 1
 $getID3 = new \getID3();
 $analyzeFileInfo = $getID3->analyze($filepath['filePath']);
 halt($analyzeFileInfo );
 $fileduration = $analyzeFileInfo['playtime_seconds']; //This is the length of the audio file
 $time = (int)ceil($fileduration);

There will be many attributes in it. Take them according to your needs. For example, playtime_seconds is the duration of the media.
halt($analyzeFileInfo ); Printed content

"data": {
		"GETID3_VERSION": "1.9.19-202002221838",
		"filesize": 2574957,
		"filepath": "D:/phpstudy_pro/WWW/mjgf-app/public/uploads/app/audioFile/20210611",
		"filename": "5eb484aff854abd8412e0737112bc067.mp3",
		"filenamepath": "D:/phpstudy_pro/WWW/mjgf-app/public/uploads/app/audioFile/20210611/5eb484aff854abd8412e0737112bc067.mp3",
		"avdataoffset": 45,
		"avdataend": 2574957,
		"fileformat": "mp3",
		"audio": {
			"dataformat": "mp3",
			"channels": 1,
			"sample_rate": 48000,
			"bitrate": 64000,
			"channelmode": "mono",
			"bitrate_mode": "cbr",
			"lossless": false,
			"encoder_options": "CBR64",
			"compression_ratio": 0.08333333333333333,
			"streams": [
				{
					"dataformat": "mp3",
					"channels": 1,
					"sample_rate": 48000,
					"bitrate": 64000,
					"channelmode": "mono",
					"bitrate_mode": "cbr",
					"lossless": false,
					"encoder_options": "CBR64",
					"compression_ratio": 0.08333333333333333
				}
			]
		},
		"tags": {
			"id3v2": {
				"encoder_settings": [
					"Lavf57.41.100"
				]
			}
		},
		"encoding": "UTF-8",
		"id3v2": {
			"header": true,
			"flags": {
				"unsynch": false,
				"exthead": false,
				"experim": false,
				"isfooter": false
			},
			"majorversion": 4,
			"minorversion": 0,
			"headerlength": 45,
			"tag_offset_start": 0,
			"tag_offset_end": 45,
			"encoding": "UTF-8",
			"comments": {
				"encoder_settings": [
					"Lavf57.41.100"
				]
			},
			"TSSE": [
				{
					"frame_name": "TSSE",
					"frame_flags_raw": 0,
					"data": "Lavf57.41.100",
					"datalength": 15,
					"dataoffset": 10,
					"framenamelong": "Software/Hardware and settings used for encoding",
					"framenameshort": "encoder_settings",
					"flags": {
						"TagAlterPreservation": false,
						"FileAlterPreservation": false,
						"ReadOnly": false,
						"GroupingIdentity": false,
						"compression": false,
						"Encryption": false,
						"Unsynchronisation": false,
						"DataLengthIndicator": false
					},
					"encodingid": 3,
					"encoding": "UTF-8"
				}
			],
			"padding": {
				"start": 35,
				"length": 10,
				"valid": true
			}
		},
		"mime_type": "audio/mpeg",
		"mpeg": {
			"audio": {
				"raw": {
					"synch": 4094,
					"version": 3,
					"layer": 1,
					"protection": 1,
					"bitrate": 5,
					"sample_rate": 1,
					"padding": 0,
					"private": 0,
					"channelmode": 3,
					"modeextension": 0,
					"copyright": 0,
					"original": 1,
					"emphasis": 0
				},
				"version": "1",
				"layer": 3,
				"channelmode": "mono",
				"channels": 1,
				"sample_rate": 48000,
				"protection": false,
				"private": false,
				"modeextension": "",
				"copyright": false,
				"original": true,
				"emphasis": "none",
				"padding": false,
				"bitrate": 64000,
				"framelength": 192,
				"bitrate_mode": "cbr"
			}
		},
		"playtime_seconds": 321.864,
		"tags_html": {
			"id3v2": {
				"encoder_settings": [
					"Lavf57.41.100"
				]
			}
		},
		"bitrate": 64000,
		"playtime_string": "5:22"
	}

Supplement:

getID3() is a PHP script that can extract useful information from MP3 or other media files, such as ID3 tag, bitrate, playback time, etc.

(formats include: Ogg, WMA, WMV, ASF, WAV, AVI, AAC, VQF, FLAC, MusePack, Real, QuickTime, Monkey's Audio, MIDI, etc.).

Official website: https://www.getid3.org/.
Function:

Using this method, the information in audio, video, pictures and other media files can be obtained,

I tested the common types of media files, and the test results are as follows:

The version of getID3 used is: 1.9.16-201810171314.

Supported types:
Audio: [mp3], [amr];
Video: [mp4], [flv], [wmv], [avi];
Pictures: [gif], [png], [jpg].

Unsupported type:

Video format [ts].

Example:
Use background: when using the chat function and sending voice, you need to know the duration of voice. At this time, you can use getID3.

Development environment: thinkphp5 1 frame.

Download getid3 class: unzip it after downloading on the official website. There are many files in it, many of which are demo s. You only need to put the getid3 directory into the project,
I put the directory under extend.

Call example code:

require "../extend/getid3/getid3.php";
$getID3 = new \getID3();
$analyzeFileInfo = $getID3->analyze($filename);
if($analyzeFileInfo && is_array($analyzeFileInfo)){
      if(isset($analyzeFileInfo['error']) && $analyzeFileInfo['error']){
               $err_msg = 'Failed to get the duration of voice message';
                    if(is_array($analyzeFileInfo['error'])){
                        foreach ($analyzeFileInfo['error'] as $key=>$value) {
                            $err_msg .= ',error'. ($key+1) . ': '.$value;
                        }
                    }else{
                        $err_msg .= ',Error:'.$analyzeFileInfo['error'];
                    }
                    log($err_msg);
                    return false;
                }else{
                    if(isset($analyzeFileInfo['playtime_seconds'])){
                        return ceil($analyzeFileInfo['playtime_seconds']);
                    }
                    return false;
                }
            }else{
                log('Failed to parse voice file:'.$analyzeFileInfo);
                return false;
            }
        }

Keywords: Back-end

Added by KeitaroHimura on Tue, 01 Feb 2022 04:53:19 +0200