How to combine video and audio through API or JS? [closed]

不问归期 提交于 2020-12-25 02:08:21

问题


I am working to design a system which does the following:

  1. User uploads a video, JS code finds the length of the video.

  2. Performs HTTP calls to an already-existing service to retrieve an audio track of the same length.

  3. Synchronize and combine the audio and video (which are the exact same length). The best thing I could think to do is to play them both at the same time (entirely doable with HTML5), but I want to be able to have a user download a combined file, or be able to say... upload it to YouTube through their API.

I have been doing lots of googling, and so far have not found any services or code which might be able to do this.

I am familiar with JavaScript, Ruby on Rails, HTML, CSS, jQuery, and both AngularJS and Backbone.js. If a solution exists in one of those languages, or perhaps something I can access through HTTP, that would be wonderful.


回答1:


you could give videoconverter.js a try, I have not tried it before, but I think that's the only way to do it front-end for now... and the smallest minified version also takes 6.1 MB.

Another option is upload the video to your server, merge it using ffmpeg and give user the link to output file, either to download or stream.




回答2:


I am currently using FFmpeg.wasm. It is a great tool, but I have not found a way to do this with plain JavaScript because the audio and video have different codecs sometimes. I made a function to merge videos and return a Uint8Array, which can be used in a blob.

<script src='https://unpkg.com/@ffmpeg/ffmpeg@0.9.6/dist/ffmpeg.min.js'></script>
async function mergeVideo(video, audio) {
    let { createFFmpeg, fetchFile } = FFmpeg;
    let ffmpeg = createFFmpeg();
    await ffmpeg.load();
    ffmpeg.FS('writeFile', 'video.mp4', await fetchFile(video));
    ffmpeg.FS('writeFile', 'audio.mp4', await fetchFile(audio));
    await ffmpeg.run('-i', 'video.mp4', '-i', 'audio.mp4', '-c', 'copy', 'output.mp4');
    let data = await ffmpeg.FS('readFile', 'output.mp4');
    return new Uint8Array(data.buffer);
};



回答3:


Regarding the svc with the audio, consider youtube music / youtube api. It will provide all the tracks you will ever need. Just do not forget about copyright. However this is a problem you will run into wherever you go.



来源:https://stackoverflow.com/questions/28890275/how-to-combine-video-and-audio-through-api-or-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!