php 检查音频文件是否损坏

问题描述 投票:0回答:1

我写了以下条件来检查文件是否损坏。即使文件已损坏,它也显示未损坏

function checkAudio($filename) {
    
    // Open the file and read the first few bytes (usually enough to determine file type)
    $fileContents = file_get_contents($filename, false, null, 0, 100); // Read first 100 bytes
    
    // Analyze file content (adjust the checks based on the format)
    // For example, check for ID3 tag presence in MP3 files
    
    if (strpos($fileContents, 'ID3') === 0) {
        // If the file starts with ID3 tag, but is shorter than expected for a valid ID3 tag, consider it corrupted
       
        if (strlen($fileContents) < 10) {
            return false;
        }

        // Check if the ID3 version is supported (adjust as needed)
        $version = ord($fileContents[3]);
        
        if ($version < 2 || $version > 4) {
            return false;
        }

        // Check if the size field of the ID3 tag header is valid
        $tagSize = (ord($fileContents[6]) & 0x7F) << 21 |
                   (ord($fileContents[7]) & 0x7F) << 14 |
                   (ord($fileContents[8]) & 0x7F) << 7 |
                   (ord($fileContents[9]) & 0x7F);

        if ($tagSize < 10) { // Minimum ID3 tag size is 10 bytes
            return false;
        }

    }

    // Add more checks for other audio formats as needed

    
    return true;
}

音频文件输出:是否损坏

php
1个回答
0
投票

我找到了一些需要修复的东西。该代码对我来说有点晦涩,在某些测试中,您所做的与注释中所写的相反。这是一种常见的行为,特别是对于初学者来说,您不必担心这一点,只需比平时更加小心地编写代码即可。

我做的第一件事是消除函数中的一些复杂性,并进行一些重命名以使事情更清晰。

你的函数被称为

checkAudio
,这让人感到困惑。如果函数返回 true,则表示文件是否已损坏? 为了澄清这一点,我在
isMp3Corrupted
中重命名了你的函数,需要输入更多字符,但需要阅读更少的代码来了解内部发生的情况。

在聊天中你说损坏是真的,但在你写的代码中我读到了

    // If the file starts with ID3 tag, but is shorter than 
    // expected for a valid ID3 tag, consider it corrupted
    if (strlen($fileContents) < 10) {
            return false;
    }

在 if 你写的之前的评论中,如果标签较短 文件已损坏,但返回 false。 => 看来 false 已损坏

函数的主体包含在单个 if 测试中: 如果文件不是以 ID3 开头,则返回 true => 看起来 true 已损坏

为了摆脱这种矛盾并使其在发生时脱颖而出,我更改了函数的名称并以更精简的方式重写了整个过程。

我懒得检查的复杂部分被从函数体中分解出来,以便进一步简化代码,并重新安排测试,以便在其中一个失败时尽早退出。

    /**
     * returns the header (first 100 bytes) of a file or false
     * @returns bool | string
     **/

    function mp3Header($filename) {
        // Open the file and read the first 100 bytes 
        // (usually enough to determine file type)
        $header = file_get_contents($filename, false, null, 0, 100);
        return $header;
    }

    /**
     * This function compute the mp3 tag size
     * I DID NOT TESTED IT -(Eineki)-
     * @return int
     */
    function computeTagSize($bytes) {
        $tagSize = (ord($bytes[0]) & 0x7F) << 21 |
                   (ord($bytes[1]) & 0x7F) << 14 |
                   (ord($bytes[2]) & 0x7F) << 7 |
                   (ord($bytes[3]) & 0x7F);
        return $tagSize;
    }

    /**
     * Check if an mp3 file is corrupted
     * The test is quite simple:
     * - file have to start with ID3 string
     * - version have to be 3
     * - header length have to be greater than 10
     *
     * @return boolean
     **/
    function isMp3Corrupted($filename) {
        $header= mp3Header($filename);
        // can't read or open the file => corrupted
        if ($header=== false) return true;

        // if the file do not start with ID3 it is corrupted
        if (substr($header, 0, 3) !== 'ID3') return true;

        // the file starts with ID3 tag, 
        // test for a valid ID3 header length
        // expected for a valid ID3 tag, consider it corrupted
       
        if (strlen($header) < 10) return true;
        
        // Check if the ID3 version is supported 
        // the version is contained into the fourth byte
        $version = ord($header[3]); // we need the numerical value
        $supportedVersions = [2, 3, 4];
        if (!in_array($version, $supportedVersions)) return true;

        $tagSize = computeTagSize(substr($header, 6, 4));
        // a tag size should not be less than 10
        if ($tagSize < 10) return true;

        // Passed All tests, the file is good (Not corrupted)
        return false;
    }

© www.soinside.com 2019 - 2024. All rights reserved.