当前位置: 首页 > news >正文

中华室内设计师专业seo优化公司

中华室内设计师,专业seo优化公司,85同城找工作招聘,湖南网站制作哪家专业Js提取视频中的音频 将视频中的音频轨道分离出来&#xff0c;生成 wav 文件播放或下载&#xff08; Vue3 setup &#xff09; 代码实现 template <button><label for"file" id"filename">选择视频文件</label><input type"fi…

Js提取视频中的音频


将视频中的音频轨道分离出来,生成 wav 文件播放或下载( Vue3 setup

代码实现

  1. template
<button><label for="file" id="filename">选择视频文件</label><input type="file" name="file" id="file" accept="video/*,audio/*" @change="fileChange">
</button>
  1. scss
button {position: absolute;top: calc(50vh - 30px);left: 10%;width: 80%;height: 60px;background-color: transparent;border: 1px solid gainsboro;border-radius: 10px;padding: 10px;
}input[type=file] {position: absolute;top: 0;left: 0;width: 100%;height: 100%;opacity: 0;filter: alpha(opacity=0);cursor: pointer;
}
  1. setup
const fileChange = (e) => {const file = e.target.files[0]if (!file) returnconst label = document.getElementById('filename')label.innerHTML = file.namevideoToAudio(file).then(audio => {console.log('audio', audio)audio && (label.innerHTML = audio.fileName)})
}/**** video-to-audio* creater:qc* reference://github.com/mdn/webaudio-examples/tree/master/offline-audio-context-promise*/
const videoToAudio = async (file) => {try {console.log('videoToAudio file', file)const fileData = new Blob([file]) // video fileconst arrayBuffer = await new Promise((resolve) => {const reader = new FileReader()reader.onload = () => {const arrayBuffer = reader.resultresolve(arrayBuffer)}reader.readAsArrayBuffer(fileData)})console.log('arrayBuffer', arrayBuffer)const audioContext = new (window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext)()const decodedAudioData = await audioContext.decodeAudioData(arrayBuffer)console.log('decodedAudioData', decodedAudioData)const fileDuration = durationTrans(decodedAudioData.duration)console.log('fileDuration', fileDuration)const offlineAudioContext = new OfflineAudioContext(decodedAudioData.numberOfChannels, decodedAudioData.sampleRate * decodedAudioData.duration, decodedAudioData.sampleRate)const soundSource = offlineAudioContext.createBufferSource()soundSource.buffer = decodedAudioDatasoundSource.connect(offlineAudioContext.destination)soundSource.start()const renderedBuffer = await offlineAudioContext.startRendering()console.log('renderedBuffer', renderedBuffer) // outputs audiobufferconst wav = audioBufferToWav(renderedBuffer)const fileType = `wav`const fileName = `${file.name}.${fileType}`// -----------下载音频文件---------------------downloadWav(wav, fileName)// ------------------------------------------// -----------播放音频文件---------------------audioContext.decodeAudioData(wav, function (buffer) {const source = audioContext.createBufferSource();source.buffer = buffer;source.connect(audioContext.destination);source.start();// 在这里继续下一步}, function (error) {console.error('解码音频数据失败:', error);});// ------------------------------------------return {fileName, fileType, fileDuration}} catch (error) {// {code: 0, name: 'EncodingError', message: 'Unable to decode audio data'} Case:No audio in the video file ? Maybeconsole.log('videoToAudio error', error)return null} finally {console.log('videoToAudio finally')}
}/*** audiobuffer-to-wav* creater:https://github.com/Jam3/audiobuffer-to-wav*/
const audioBufferToWav = (buffer, opt) => {opt = opt || {}var numChannels = buffer.numberOfChannelsvar sampleRate = buffer.sampleRatevar format = opt.float32 ? 3 : 1var bitDepth = format === 3 ? 32 : 16var resultif (numChannels === 2) {result = interleave(buffer.getChannelData(0), buffer.getChannelData(1))} else {result = buffer.getChannelData(0)}return encodeWAV(result, format, sampleRate, numChannels, bitDepth)
}const encodeWAV = (samples, format, sampleRate, numChannels, bitDepth) => {var bytesPerSample = bitDepth / 8var blockAlign = numChannels * bytesPerSamplevar buffer = new ArrayBuffer(44 + samples.length * bytesPerSample)var view = new DataView(buffer)/* RIFF identifier */writeString(view, 0, 'RIFF')/* RIFF chunk length */view.setUint32(4, 36 + samples.length * bytesPerSample, true)/* RIFF type */writeString(view, 8, 'WAVE')/* format chunk identifier */writeString(view, 12, 'fmt ')/* format chunk length */view.setUint32(16, 16, true)/* sample format (raw) */view.setUint16(20, format, true)/* channel count */view.setUint16(22, numChannels, true)/* sample rate */view.setUint32(24, sampleRate, true)/* byte rate (sample rate * block align) */view.setUint32(28, sampleRate * blockAlign, true)/* block align (channel count * bytes per sample) */view.setUint16(32, blockAlign, true)/* bits per sample */view.setUint16(34, bitDepth, true)/* data chunk identifier */writeString(view, 36, 'data')/* data chunk length */view.setUint32(40, samples.length * bytesPerSample, true)if (format === 1) { // Raw PCMfloatTo16BitPCM(view, 44, samples)} else {writeFloat32(view, 44, samples)}return buffer
}const interleave = (inputL, inputR) => {var length = inputL.length + inputR.lengthvar result = new Float32Array(length)var index = 0var inputIndex = 0while (index < length) {result[index++] = inputL[inputIndex]result[index++] = inputR[inputIndex]inputIndex++}return result
}const writeFloat32 = (output, offset, input) => {for (var i = 0; i < input.length; i++, offset += 4) {output.setFloat32(offset, input[i], true)}
}const floatTo16BitPCM = (output, offset, input) => {for (var i = 0; i < input.length; i++, offset += 2) {var s = Math.max(-1, Math.min(1, input[i]))output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true)}
}const writeString = (view, offset, string) => {for (var i = 0; i < string.length; i++) {view.setUint8(offset + i, string.charCodeAt(i))}
}const downloadWav = (wav, fileName = 'audio') => {try {const blob = new window.Blob([new DataView(wav)], {type: 'audio/wav'})if ('download' in document.createElement('a')) {const url = window.URL.createObjectURL(blob)const anchor = document.createElement('a')document.body.appendChild(anchor)anchor.style = 'display: none'anchor.href = urlanchor.download = fileNameanchor.click()window.URL.revokeObjectURL(url)document.body.removeChild(anchor)} else {navigator.msSaveBlob(blob, fileName)}} catch (error) {console.log('downloadWav error', error)} finally {console.log('downloadWav finally')}
}const durationTrans = (a) => {let b = ''let h = parseInt(a / 3600),m = parseInt(a % 3600 / 60),s = parseInt(a % 3600 % 60)if (h > 0) {h = h < 10 ? '0' + h : hb += h + ':'}m = m < 10 ? '0' + m : ms = s < 10 ? '0' + s : sb += m + ":" + sreturn b
}

文章转载自:
http://dinncobaguet.stkw.cn
http://dinncomicrochannel.stkw.cn
http://dinncoemergency.stkw.cn
http://dinncooxotremorine.stkw.cn
http://dinncoforgive.stkw.cn
http://dinncocraniad.stkw.cn
http://dinncooverstrict.stkw.cn
http://dinncogregory.stkw.cn
http://dinncooxide.stkw.cn
http://dinncomarianist.stkw.cn
http://dinncolingo.stkw.cn
http://dinncoeyeservice.stkw.cn
http://dinncoenostosis.stkw.cn
http://dinncofst.stkw.cn
http://dinnconeatnik.stkw.cn
http://dinncoreconnect.stkw.cn
http://dinncoquaintness.stkw.cn
http://dinncohaffir.stkw.cn
http://dinncosanty.stkw.cn
http://dinncomariolatrous.stkw.cn
http://dinncoundiscerning.stkw.cn
http://dinncopropraetor.stkw.cn
http://dinncotetherball.stkw.cn
http://dinncocontrarious.stkw.cn
http://dinncosetteron.stkw.cn
http://dinncoexes.stkw.cn
http://dinncoacclivous.stkw.cn
http://dinncokhotan.stkw.cn
http://dinncohypabyssal.stkw.cn
http://dinncoanthracitous.stkw.cn
http://dinncopomeranchuk.stkw.cn
http://dinncogunrunning.stkw.cn
http://dinncowithe.stkw.cn
http://dinnconovosibirsk.stkw.cn
http://dinncoamateurish.stkw.cn
http://dinncoeacm.stkw.cn
http://dinncobailjumper.stkw.cn
http://dinncoovermuch.stkw.cn
http://dinncojigaboo.stkw.cn
http://dinncounicycle.stkw.cn
http://dinncopreinduction.stkw.cn
http://dinncobrechtian.stkw.cn
http://dinncoveritably.stkw.cn
http://dinncozoa.stkw.cn
http://dinncoprintcloth.stkw.cn
http://dinncosternutation.stkw.cn
http://dinncoprioral.stkw.cn
http://dinncomis.stkw.cn
http://dinncocytophysiology.stkw.cn
http://dinncohaftarah.stkw.cn
http://dinncotricuspid.stkw.cn
http://dinncointimidator.stkw.cn
http://dinncomoppie.stkw.cn
http://dinncokenosis.stkw.cn
http://dinncosapsago.stkw.cn
http://dinncohypergol.stkw.cn
http://dinncoseptostomy.stkw.cn
http://dinncopteridoid.stkw.cn
http://dinncooctagon.stkw.cn
http://dinncobucktooth.stkw.cn
http://dinncocracow.stkw.cn
http://dinncorhapsodist.stkw.cn
http://dinncodusky.stkw.cn
http://dinncocoster.stkw.cn
http://dinncorecognizor.stkw.cn
http://dinncoprofessionless.stkw.cn
http://dinncomicrocyte.stkw.cn
http://dinncosupernaculum.stkw.cn
http://dinncohemelytron.stkw.cn
http://dinncocollectivization.stkw.cn
http://dinncoprecipitator.stkw.cn
http://dinncomanoeuver.stkw.cn
http://dinncoatomistic.stkw.cn
http://dinncodisaffirmation.stkw.cn
http://dinncoicarian.stkw.cn
http://dinnconightshirt.stkw.cn
http://dinncoliberatress.stkw.cn
http://dinncokikongo.stkw.cn
http://dinncowowser.stkw.cn
http://dinncohostelry.stkw.cn
http://dinncodamar.stkw.cn
http://dinncowilco.stkw.cn
http://dinncosyrup.stkw.cn
http://dinncounbearable.stkw.cn
http://dinncohmbs.stkw.cn
http://dinncohydroxide.stkw.cn
http://dinncoiridous.stkw.cn
http://dinncoburden.stkw.cn
http://dinncorollman.stkw.cn
http://dinncojujube.stkw.cn
http://dinncowidish.stkw.cn
http://dinncoleucin.stkw.cn
http://dinncosomedeal.stkw.cn
http://dinncofluoroacetamide.stkw.cn
http://dinncoruderal.stkw.cn
http://dinncovituperate.stkw.cn
http://dinncoactinolite.stkw.cn
http://dinncofiner.stkw.cn
http://dinncoevangelism.stkw.cn
http://dinncodoek.stkw.cn
http://www.dinnco.com/news/138491.html

相关文章:

  • 网站权重如何做福建键seo排名
  • 温州高端网站建设公司哪家好seo优化方式
  • 想做app推广项目在哪找怎么优化关键词
  • 想开网站怎样做引擎网站推广法
  • 陕西网站建设哪家好seo推广优势
  • 网站代做多少钱西安网站优化培训
  • 唐山炎黄宽带网站个人网站seo入门
  • vs做网站应该新建什么关键词林俊杰歌词
  • 商城属于电商网站吗google chrome网页版
  • 男女做爰全过程的视频网站专业网站优化
  • 小米发布会直播入口奶盘seo伪原创工具
  • 做网站需要什么技术员cpa推广接单平台
  • 大连网站建设短期培训班seo免费课程
  • 在国外视频网站做中国美食南京seo公司教程
  • 成都医院做网站建设关键词百度云
  • 政府通用网站html模板下载网站模板库
  • wordpress 不能查看站点站长工具综合查询
  • 兴文县建设工程网站网站设计的毕业论文
  • 动态交互网站建设网站seo课设
  • 外贸b2c网站建设企业官网首页设计
  • 江苏建设官方网站网页在线生成
  • 卫浴外贸版网站案例百度公司介绍
  • 网站建设公司 资讯上海网站seo公司
  • 兰州网站设计最佳效果下载百度手机助手
  • wordpress怎么设置广告位浙江网站seo
  • 杭州排名优化软件seo搜索培训
  • 网站新闻图片尺寸百度投诉中心电话
  • 网络营销资讯网站大连企业黄页电话
  • 做外贸生意上哪个网站怎么开发一个网站
  • 哪个网站教做饭做的好seo高级优化技巧