简体   繁体   English

通过连接和标题('Content-Type:audio/mpeg')使用 php 合并 mp3 音频是否正确?

[英]Is it correct to merge mp3 audios using php by concatenation and header('Content-Type: audio/mpeg')?

I have built a simple CAPTCHA PHP script and now I am trying to create a script that generates a mp3 audio for people who can't see well or can't see at all.我已经构建了一个简单的 CAPTCHA PHP 脚本,现在我正在尝试创建一个脚本,为视力不好或根本看不见的人生成 mp3 音频。 My goal is only to challenge myself, so I prefer not to use a external library, but I would appreciate some help.我的目标只是挑战自己,所以我不想使用外部库,但我会很感激一些帮助。 I tried using file_put_contents() function to merge the audios (I have one mp3 audio for each letter) and for my surprise, it worked, but it seemed incorrect to me to just join audio files like I would join text files.我尝试使用 file_put_contents() function 来合并音频(每个字母都有一个 mp3 音频),令我惊讶的是,它起作用了,但对我来说,像加入文本文件一样加入音频文件似乎是不正确的。 So my second attempt is to do like so:所以我的第二次尝试是这样做:

<?php
    session_start();
    $letter_array = str_split($_SESSION["captcha"]); //this is an array with the letters of the captcha
    header('Content-Type: audio/mpeg');
    header('Cache-Control: no-cache');
    header('Pragma: no-cache');
    header('Expires: 0');
    $audio = '';
    foreach ($letter_array as $letter) {
        $audio .= file_get_contents("audios/".$letter.".mp3");
    }
    echo $audio;
?>

and then I can embed this file to an tag in HTML and it also works.然后我可以将这个文件嵌入到 HTML 中的一个标签中,它也可以工作。 I know that essentially both of the solutions are similar, but I find this second way more concise.我知道这两种解决方案本质上都是相似的,但我发现第二种方式更简洁。 My question is: although it works fine, is it OK to do such thing this manner, or is it wrong, or not recommended?我的问题是:虽然它工作正常,但以这种方式做这样的事情是否可以,或者是错误的,还是不推荐的? I don't know much encoding and about the structure of a mp3 file, so I dont' know if this solution would work for everyone.我不太了解编码和 mp3 文件的结构,所以我不知道这个解决方案是否适用于所有人。 I am also open to suggestions我也乐于接受建议
Thanks谢谢

Mostly, yes.大多数情况下,是的。

Regular MP3 files are just a series of MPEG frames.常规 MP3 文件只是一系列 MPEG 帧。 You can safely concatenate them as long as they have the same sample rate and channel count.只要它们具有相同的采样率和通道数,您就可以安全地连接它们。 You can mix and match bitrates.您可以混合和匹配比特率。

There are some caveats though.不过有一些注意事项。 Firstly, it's common for MP3 files to have ID3 tags attached to them.首先,MP3 文件通常带有 ID3 标签。 You're going to want to throw out those tags since they're expected to be in certain locations.你会想扔掉这些标签,因为它们应该位于某些位置。

Also, it's important to understand the bit reservoir.此外,了解位库也很重要。 This isn't an issue for you since you're concatenating fully encoded files, but if you ever have to split a stream, it's important to note that data from one from can be placed into other frames.这对您来说不是问题,因为您连接的是完全编码的文件,但如果您必须拆分stream,请务必注意来自一个帧的数据可以放入其他帧中。 This is to allow for using more bandwidth on more complicated frames, and less where it isn't needed.这是为了允许在更复杂的帧上使用更多的带宽,而在不需要的地方使用更少的带宽。

At the end of the day though, you really shouldn't build your own CAPTCHA.不过归根结底,您真的不应该构建自己的验证码。 It's trivial for someone to write maybe 5 lines of code to match your audio samples and pick out the numbers.对于某人来说,编写 5 行代码来匹配您的音频样本并挑选出数字是微不足道的。 Not to mention, you're potentially creating accessibility issues.更不用说,您可能会造成可访问性问题。 I highly recommend checking out Google reCAPTCHA.我强烈建议您查看 Google reCAPTCHA。 Current versions are non-intrusive to your users, and are potentially much more secure than what you're proposing.当前版本对您的用户没有干扰,并且可能比您提议的版本安全得多。 https://www.google.com/recaptcha/intro/v3.html https://www.google.com/recaptcha/intro/v3.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM