一、百度搜索瀏覽器對於音頻文件的兼容,排在前面的文章大部分是複製粘貼好久之前的文章,容易誤導搜索資料的人,javascript
所以從新驗證整理下。css
以Firefox瀏覽器爲例,Firefox對於mp3格式音頻的支持在發佈版本21時就已經支持了(2013年)。html
下載Firefox各個版本,而後在audio標籤上引入mp3格式文件,在v20的Firefox不能播放,在V21上Firefox能夠播放。java
Firefox瀏覽器歷史版本下載地址:http://ftp.mozilla.org/pub/firefox/releases/,git
Firefox和mp3的一些討論資料地址:https://code.i-harness.com/zh-CN/q/4b1f00github
從新整理了一個表,羅列當前各主流瀏覽器對音/視頻的支持情況:web
1.二、測試代碼:windows
<html lang="cn"> <head> <meta charset="utf-8"/> <title>音頻/視頻</title> <style type="text/css"> body{ padding: 20px 50px; } table td, table th{ padding: 5px 10px; text-align: center; } table th { color: blue; font-size: 20px; } table tr td:nth-of-type(1) { color: purple; font-size: 20px; font-weight:bold;; } .first-th { font-size: 12px; color:black;position: relative; } .th-div1 { position: absolute; } .th-div2 { position: absolute; left: 50%; margin-left: -40px; width: 100%; line-height: 1px; border-top: 1px solid black; transform: rotate(30deg); } .th-div3 { position: absolute; top: 6px; right: 4px; } tbody tr:nth-of-type(1) { background-color: #ffffcc; } tbody tr:nth-of-type(2) { background-color: #ccffcc; } tbody tr:nth-of-type(3) { background-color: #ccccff; } tbody tr:nth-of-type(4) { background-color: #FFF0F5; } .testOne{ margin: 20px 5px; padding: 10px; border: 1px dashed black; } .playBtn { margin-left: 20px; width: 120px; height: 50px; background-color: #fff; border: 1px solid black; border-radius: 50px; line-heght: 50px; } </style> </head> <body> <table border="1" cellspacing="0"> <thead> <tr> <th class="first-th"> <div class="th-div1">格式</div> <div class="th-div2"></div> <div class="th-div3">瀏覽器</div> </th> <th>Chrome</th> <th>Firefox</th> <th>Opera</th> <th>Safari</th> <th>IE11</th> <th>Edge</th> </tr> </thead> <tbody> <tr> <td>OGG</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>NO</td> <td>NO</td> <td>YES</td> </tr> <tr> <td>MP3</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>YES</td> </tr> <tr> <td>WAV</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>NO</td> <td>YES</td> </tr> <tr> <td>MP4</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>YES</td> </tr> <tr> <td>WebM</td> <td>YES</td> <td>YES</td> <td>YES</td> <td>NO</td> <td>NO</td> <td>YES</td> </tr> </tbody> </table> <ul>注意: <li>Safari瀏覽器對於wav音頻格式和mp4視頻格式的支持,須要把頁面部署到web服務器裏面。<br/> 若是隻是單純的用Safari瀏覽器打開磁盤的一個靜態頁面,會發現不支持這兩種格式 </li> <li> 同上Opera瀏覽器對於ogg視頻格式的支持,也須要把頁面部署到web服務器上。 </li> </ul> <p></p> <div class="testOne"> <h3>一、測試一下,三個audio標籤,src依次爲ogg、mp三、wav格式文件</h3> <audio controls="controls" src="../img/file/Friendships.ogg">不支持該格式播放</audio> <audio controls="controls" src="../img/file/Friendships.mp3">不支持該格式播放</audio> <audio controls="controls" src="../img/file/Friendships.wav">不支持該格式播放</audio> </div> <div class="testOne"> <h3>二、audio標籤兼容寫法,一個audio標籤,裏面設置多個source標籤</h3> <audio controls="controls" id="audio"> <source src="../img/file/Friendships.ogg" /> <source src="../img/file/Friendships.mp3" /> <source src="../img/file/Friendships.wav" /> </audio> <button onclick="audioManage()" class="playBtn">播放/暫停</button> </div> <script type="text/javascript"> //手動控制音頻的播放 function audioManage(){ var obj = document.getElementById('audio'); if (obj.paused){ //表示是暫停,下一步播放 obj.play(); } else{ //播放中,下一步暫停 obj.pause(); } } </script> <br/> <div class="testOne"> <h3>三、測試一下,三個video標籤,src依次爲mp四、ogg、WebM格式文件</h3> <video controls="controls" src="../img/file/jia.mp4"></video> <video controls="controls" src="../img/file/jia.ogg"></video> <video controls="controls" src="../img/file/jia.WebM"></video> </div> <div class="testOne"> <h3>四、測試一下,兼容寫法, 一個video標籤,三個souce依次引用mp四、ogg、WebM</h3> <video controls="controls" id="video" width="300" height="150"> <source src="../img/file/jia.mp4"/> <source src="../img/file/jia.ogg"/> <source src="../img/file/jia.WebM"/> </video> <button onclick="videoManage()" class="playBtn">播放/暫停</button> <button onclick="changeBigVideo()" class="playBtn">寬高增大</button> <button onclick="changeSmallVideo()" class="playBtn">寬高減少</button> <script type="text/javascript"> var video = document.getElementById('video'); var videoBigInterval = null, videoSmallInterval = null; //視頻暫停和繼續播放 function videoManage(){ if (videoBigInterval){ clearInterval(videoBigInterval); } if (videoSmallInterval){ clearInterval(videoSmallInterval) } if (video.paused){ video.play(); } else{ video.pause(); } } //video標籤寬高變大 function changeBigVideo(){ console.log('...start....big....') if (videoBigInterval){ clearInterval(videoBigInterval); } if (videoSmallInterval){ clearInterval(videoSmallInterval) } videoBigInterval = setInterval(function(){ var oldW = video.width, oldH = video.height; console.log('big --> width: ', oldW, ', height: ', oldH) video.width = oldW*1.05; video.height = oldH*1.05; }, 1000) } //video標籤寬高變小 function changeSmallVideo(){ console.log(".....start ..small...") if (videoBigInterval){ clearInterval(videoBigInterval); } if (videoSmallInterval){ clearInterval(videoSmallInterval) } videoSmallInterval = setInterval(function(){ var oldW = video.width, oldH = video.height; console.log('small --> width: ', oldW, ', height: ', oldH) video.width = oldW*0.95; video.height = oldH*0.95; }, 1000) } </script> </div> </body> </html>
1.3.一、Chrome瀏覽器效果圖:瀏覽器
1.3.二、Safari瀏覽器效果圖:服務器
二、FFmpeg是一套能夠用來記錄、轉換數字音頻、視頻,並能將其轉化爲流的開源計算機程序,功能強大,應用普遍。
這裏主要介紹下它的轉換音頻格式和提取音頻命令, 以windows PC爲例:
2.一、下載安裝,設置全局環境變量
2.二、cmd打開終端,cd進入要轉化的音頻文件夾。
2.三、執行提取音頻命令或者音頻轉換命令,格式:
ffmpeg -i input.mp4 -f mp3 -ar 16k output.mp3
說明:
假如要從一個my.mp4視頻文件中提取音頻,獲得一個my.mp3音頻文件,則命令爲:
ffmpeg -i my.mp4 -f mp3 -ar 16k my.mp3
或者要從一個mp3音頻文件,轉換爲其餘音頻格式如wav
ffmpeg -i my.mp3 -f wav -ar 16k my.wav
其餘更多命令參考:ffmpeg參數中文詳細解釋 , FFmpeg官網文檔
縮放視頻格式命令:
ffmpeg -i input.mpg -s 1280x720 output.mp4
或
ffmpeg -i input.mpg -vf scale=1280:720 output.mp4
完整的測試頁面和音頻文件見:https://github.com/xiaotanit/Tan_HtmlDemo