经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JSJS库框架 » JavaScript » 查看文章
浏览器音频兼容和ffmpeg的音频转码使用
来源:cnblogs  作者:TDX  时间:2018/10/8 9:07:23  对本文有异议

 

1、百度搜索浏览器对于音频文件的兼容,排在前面的文章大部分是复制粘贴很久以前的文章,容易误导搜索资料的人,

因此重新验证整理下。

  以Firefox浏览器为例,Firefox对于mp3格式音频的支持在发布版本21时就已经支持了(2013年)。

下载Firefox各个版本,然后在audio标签上引入mp3格式文件,在v20的Firefox不能播放,在V21上Firefox可以播放。

Firefox浏览器历史版本下载地址:http://ftp.mozilla.org/pub/firefox/releases/

Firefox和mp3的一些讨论资料地址:https://code.i-harness.com/zh-CN/q/4b1f00

   重新整理了一个表,罗列当前各主流浏览器对音/视频的支持状况:

    注意:
  • Safari浏览器对于wav音频格式和mp4视频格式的支持,需要把页面部署到web服务器里面。
    如果只是单纯的用Safari浏览器打开磁盘的一个静态页面,会发现不支持这两种格式
  • 同上Opera浏览器对于ogg视频格式的支持,也需要把页面部署到web服务器上。

 

 1.2、测试代码:

  1. <html lang="cn">
  2. <head>
  3. <meta charset="utf-8"/>
  4. <title>音频/视频</title>
  5. <style type="text/css">
  6. body{ padding: 20px 50px; }
  7. table td, table th{
  8. padding: 5px 10px; text-align: center;
  9. }
  10. table th { color: blue; font-size: 20px; }
  11. table tr td:nth-of-type(1) { color: purple; font-size: 20px; font-weight:bold;; }
  12. .first-th { font-size: 12px; color:black;position: relative; }
  13. .th-div1 {
  14. position: absolute;
  15. }
  16. .th-div2 {
  17. position: absolute; left: 50%; margin-left: -40px; width: 100%;
  18. line-height: 1px; border-top: 1px solid black;
  19. transform: rotate(30deg);
  20.  
  21. }
  22. .th-div3 {
  23. position: absolute; top: 6px; right: 4px;
  24. }
  25. tbody tr:nth-of-type(1) { background-color: #ffffcc; }
  26. tbody tr:nth-of-type(2) { background-color: #ccffcc; }
  27. tbody tr:nth-of-type(3) { background-color: #ccccff; }
  28. tbody tr:nth-of-type(4) { background-color: #FFF0F5; }
  29. .testOne{
  30. margin: 20px 5px; padding: 10px;
  31. border: 1px dashed black;
  32. }
  33. .playBtn {
  34. margin-left: 20px;
  35. width: 120px; height: 50px; background-color: #fff;
  36. border: 1px solid black; border-radius: 50px; line-heght: 50px;
  37. }
  38. </style>
  39. </head>
  40.  
  41. <body>
  42. <table border="1" cellspacing="0">
  43. <thead>
  44. <tr>
  45. <th class="first-th">
  46. <div class="th-div1">格式</div>
  47. <div class="th-div2"></div>
  48. <div class="th-div3">浏览器</div>
  49. </th>
  50. <th>Chrome</th>
  51. <th>Firefox</th>
  52. <th>Opera</th>
  53. <th>Safari</th>
  54. <th>IE11</th>
  55. <th>Edge</th>
  56. </tr>
  57. </thead>
  58. <tbody>
  59. <tr>
  60. <td>OGG</td>
  61. <td>YES</td>
  62. <td>YES</td>
  63. <td>YES</td>
  64. <td>NO</td>
  65. <td>NO</td>
  66. <td>YES</td>
  67. </tr>
  68. <tr>
  69. <td>MP3</td>
  70. <td>YES</td>
  71. <td>YES</td>
  72. <td>YES</td>
  73. <td>YES</td>
  74. <td>YES</td>
  75. <td>YES</td>
  76. </tr>
  77. <tr>
  78. <td>WAV</td>
  79. <td>YES</td>
  80. <td>YES</td>
  81. <td>YES</td>
  82. <td>YES</td>
  83. <td>NO</td>
  84. <td>YES</td>
  85. </tr>
  86. <tr>
  87. <td>MP4</td>
  88. <td>YES</td>
  89. <td>YES</td>
  90. <td>YES</td>
  91. <td>YES</td>
  92. <td>YES</td>
  93. <td>YES</td>
  94. </tr>
  95. <tr>
  96. <td>WebM</td>
  97. <td>YES</td>
  98. <td>YES</td>
  99. <td>YES</td>
  100. <td>NO</td>
  101. <td>NO</td>
  102. <td>YES</td>
  103. </tr>
  104. </tbody>
  105. </table>
  106. <ul>注意:
  107. <li>Safari浏览器对于wav音频格式和mp4视频格式的支持,需要把页面部署到web服务器里面。<br/>
  108. 如果只是单纯的用Safari浏览器打开磁盘的一个静态页面,会发现不支持这两种格式
  109. </li>
  110. <li>
  111. 同上Opera浏览器对于ogg视频格式的支持,也需要把页面部署到web服务器上。
  112. </li>
  113. </ul>
  114. <p></p>
  115.  
  116. <div class="testOne">
  117. <h3>1、测试一下,三个audio标签,src依次为ogg、mp3、wav格式文件</h3>
  118. <audio controls="controls" src="../img/file/Friendships.ogg">不支持该格式播放</audio>
  119. <audio controls="controls" src="../img/file/Friendships.mp3">不支持该格式播放</audio>
  120. <audio controls="controls" src="../img/file/Friendships.wav">不支持该格式播放</audio>
  121. </div>
  122.  
  123. <div class="testOne">
  124. <h3>2、audio标签兼容写法,一个audio标签,里面设置多个source标签</h3>
  125. <audio controls="controls" id="audio">
  126. <source src="../img/file/Friendships.ogg" />
  127. <source src="../img/file/Friendships.mp3" />
  128. <source src="../img/file/Friendships.wav" />
  129. </audio>
  130. <button onclick="audioManage()" class="playBtn">播放/暂停</button>
  131. </div>
  132. <script type="text/javascript">
  133.  
  134. //手动控制音频的播放
  135. function audioManage(){
  136. var obj = document.getElementById('audio');
  137. if (obj.paused){
  138. //表示是暂停,下一步播放
  139. obj.play();
  140. }
  141. else{
  142. //播放中,下一步暂停
  143. obj.pause();
  144. }
  145. }
  146. </script>
  147.  
  148. <br/>
  149. <div class="testOne">
  150. <h3>3、测试一下,三个video标签,src依次为mp4、ogg、WebM格式文件</h3>
  151. <video controls="controls" src="../img/file/jia.mp4"></video>
  152. <video controls="controls" src="../img/file/jia.ogg"></video>
  153. <video controls="controls" src="../img/file/jia.WebM"></video>
  154. </div>
  155. <div class="testOne">
  156. <h3>4、测试一下,兼容写法, 一个video标签,三个souce依次引用mp4、ogg、WebM</h3>
  157. <video controls="controls" id="video" width="300" height="150">
  158. <source src="../img/file/jia.mp4"/>
  159. <source src="../img/file/jia.ogg"/>
  160. <source src="../img/file/jia.WebM"/>
  161. </video>
  162. <button onclick="videoManage()" class="playBtn">播放/暂停</button>
  163. <button onclick="changeBigVideo()" class="playBtn">宽高增大</button>
  164. <button onclick="changeSmallVideo()" class="playBtn">宽高减小</button>
  165. <script type="text/javascript">
  166. var video = document.getElementById('video');
  167. var videoBigInterval = null, videoSmallInterval = null;
  168. //视频暂停和继续播放
  169. function videoManage(){
  170. if (videoBigInterval){
  171. clearInterval(videoBigInterval);
  172. }
  173. if (videoSmallInterval){
  174. clearInterval(videoSmallInterval)
  175. }
  176. if (video.paused){
  177. video.play();
  178. }
  179. else{
  180. video.pause();
  181. }
  182. }
  183. //video标签宽高变大
  184. function changeBigVideo(){
  185. console.log('...start....big....')
  186. if (videoBigInterval){
  187. clearInterval(videoBigInterval);
  188. }
  189. if (videoSmallInterval){
  190. clearInterval(videoSmallInterval)
  191. }
  192. videoBigInterval = setInterval(function(){
  193. var oldW = video.width, oldH = video.height;
  194. console.log('big --> width: ', oldW, ', height: ', oldH)
  195. video.width = oldW*1.05;
  196. video.height = oldH*1.05;
  197. }, 1000)
  198. }
  199. //video标签宽高变小
  200. function changeSmallVideo(){
  201. console.log(".....start ..small...")
  202. if (videoBigInterval){
  203. clearInterval(videoBigInterval);
  204. }
  205. if (videoSmallInterval){
  206. clearInterval(videoSmallInterval)
  207. }
  208. videoSmallInterval = setInterval(function(){
  209. var oldW = video.width, oldH = video.height;
  210. console.log('small --> width: ', oldW, ', height: ', oldH)
  211. video.width = oldW*0.95;
  212. video.height = oldH*0.95;
  213. }, 1000)
  214. }
  215. </script>
  216. </div>
  217.  
  218. </body>
  219. </html>
View Code

 

1.3.1、Chrome浏览器效果图:

 

1.3.2、Safari浏览器效果图:

 

 

2、FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序,功能强大,应用广泛。

这里主要介绍下它的转换音频格式和提取音频命令, 以windows PC为例:

2.1、下载安装,设置全局环境变量

2.2、cmd打开终端,cd进入要转化的音频文件夹。

2.3、执行提取音频命令或者音频转换命令,格式:

  1. ffmpeg -i input.mp4 -f mp3 -ar 16k output.mp3

说明:

  • 源视频或音频:input.mp4
  • 输出格式:-f mp3  (这里设置输出mp3格式)
  • 音频采样率:-ar 16k   (这里设置了16k)
  • 输出文件名:output.mp3

假如要从一个my.mp4视频文件中提取音频,得到一个my.mp3音频文件,则命令为:

  1. ffmpeg -i my.mp4 -f mp3 -ar 16k my.mp3

或者要从一个mp3音频文件,转换为其他音频格式如wav

  1. ffmpeg -i my.mp3 -f wav -ar 16k my.wav

其他更多命令参考:ffmpeg参数中文详细解释  ,   FFmpeg官网文档

 

 

完整的测试页面和音频文件见:https://github.com/xiaotanit/Tan_HtmlDemo

 

版权声明


作者:TDX

出处:博客园TDX的技术博客--http://www.cnblogs.com/tandaxia

您的支持是对博主最大的鼓励,感谢您的认真阅读。

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号