如何混用MKV和MKA文件并使其在浏览器中播放? [英] How can I mux a MKV and MKA file and get it to play in a browser?
问题描述
我正在使用ffmpeg将.mkv和.mka文件合并为.mp4文件.我当前的命令如下所示:
I'm using ffmpeg to merge .mkv and .mka files into .mp4 files. My current command looks like this:
ffmpeg -i video.mkv -i audio.mka output_path.mp4
音频和视频文件是Amazon S3的预签名URL.即使在具有足够资源的服务器上,此过程也会非常缓慢.我研究了可以告诉ffmpeg跳过每个帧重新编码的情况,但是我认为在我的情况下,实际上确实需要重新编码每个帧.
The audio and video files are pre-signed urls from Amazon S3. Even on a server with sufficient resources, this process is going very slowly. I've researched situations where you can tell ffmpeg to skip re-encoding each frame, but I think that in my situation it actually does need to re-encode each frame.
我已经将2个示例文件下载到了我的macbook pro,并通过自制软件在本地安装了ffmpeg.当我运行命令
I've downloaded 2 sample files to my macbook pro and have installed ffmpeg locally via homebrew. When I run the command
ffmpeg -i video.mkv -i audio.mka -c copy output.mp4
我得到以下输出:
ffmpeg version 3.3.2 Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 8.1.0 (clang-802.0.42)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
Input #0, matroska,webm, from '319_audio_1498590673766.mka':
Metadata:
encoder : GStreamer matroskamux version 1.8.1.1
creation_time : 2017-06-27T19:10:58.000000Z
Duration: 00:00:03.53, start: 2.831000, bitrate: 50 kb/s
Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
Metadata:
title : Audio
Input #1, matroska,webm, from '319_video_1498590673766.mkv':
Metadata:
encoder : GStreamer matroskamux version 1.8.1.1
creation_time : 2017-06-27T19:10:58.000000Z
Duration: 00:00:03.97, start: 2.851000, bitrate: 224 kb/s
Stream #1:0(eng): Video: vp8, yuv420p(progressive), 640x480, SAR 1:1 DAR 4:3, 30 tbr, 1k tbn, 1k tbc (default)
Metadata:
title : Video
[mp4 @ 0x7fa4f0806800] Could not find tag for codec vp8 in stream #0, codec not currently supported in container
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Stream mapping:
Stream #1:0 -> #0:0 (copy)
Stream #0:0 -> #0:1 (copy)
Last message repeated 1 times
因此,看来我正在使用的特定编码是vp8视频和opus音频文件,我认为它们与.mp4输出容器不兼容.我希望能得到涵盖将vp8和opus最佳地合并到.mp4输出中的方式的答案,或者使我朝着与vp8& amp;兼容的输出媒体格式的方向指出的答案.作品,并且可以在网络和移动设备上播放,因此我可以完全绕过重新编码步骤.
So it appears that the specific encodings I'm working with are vp8 videos and opus audio files, which I believe are incompatible with the .mp4 output container. I would appreciate answers that cover ways of optimally merging vp8 and opus into .mp4 output or answers that point me in the direction of output media formats that are both compatible with vp8 & opus and are playable on web and mobile devices so that I can bypass the re-encoding step altogether.
仅在遵循LordNeckbeard的建议后提供基准:
Just wanted to provide a benchmark after following LordNeckbeard's advice:
4 min 41 second video transcoded locally on my mac
LordNeckbeard’s approach : 15 mins 55 seconds (955 seconds)
Current approach : 18 mins 49 seconds (1129 seconds)
18% speed increase
推荐答案
您可以使用ffmpeg
将MKV和MKA多路复用和/或重新编码为与Web浏览器兼容的格式,例如Webm或MP4.
You can use ffmpeg
to mux and/or re-encode MKV and MKA into web browser compatible formats such as Webm or MP4.
如果您输入的是VP8或VP9视频以及Vorbis或Opus音频,例如问题中的输入,则可以将其混入Webm.这应该很快,因为它不会重新编码:
You can just mux into Webm if your inputs are VP8 or VP9 video and Vorbis or Opus audio, such as the inputs in your question. This should be fast because it will not re-encode:
ffmpeg -i video.mkv -i audio.mka -c copy output.webm
默认流选择行为是为每种流类型选择一个流,因此使用-map
您可以告诉它选择哪些流以防止错误.例如,如果两个输入都包含多个流,但是您只想来自video.mkv
的第一个视频流和来自audio.mka
的第一个音频流:
Default stream selection behavior is to select one stream per stream type, so with -map
you can tell it which streams to choose to prevent mistakes. For example, if both inputs contain multiple streams, but you only want to first video stream from video.mkv
and the first audio stream from audio.mka
:
ffmpeg -i video.mkv -i audio.mka -map 0:v:0 -map 1:a:0 -c copy -movflags +faststart output.webm
MP4多路复用器:如果输入格式是H.264/H.265视频和AAC音频
ffmpeg -i video.mkv -i audio.mka -c copy -movflags +faststart output.mp4
-
添加了
-
-movflags +faststart
是因为您提到了Web回放.这将使视频在客户端完全下载之前就可以开始播放. -movflags +faststart
was added because you mentioned web playback. This will allow the video to begin playback before it is completely downloaded by the client.
您需要重新编码:
ffmpeg -i video.mkv -i audio.mka -c:v libvpx-vp9 -crf 33 -b:v 0 -c:a libopus output.webm
-
VP9真的很慢.如果要VP8,请使用
-c:v libvpx
.有关更多信息,请参见 FFmpeg Wiki:VP8 和VP9 is really slow. If you want VP8 instead use
-c:v libvpx
. For more info see FFmpeg Wiki: VP8 and FFmpeg Wiki: VP9.如果没有libopus支持,请改用libvorbis.
If you don't have libopus support use libvorbis instead.
ffmpeg -i video.mkv -i audio.mka -c:v libx264 -crf 23 -preset medium -c:a aac -movflags +faststart output.mp4
-
对于视频,请使用
-crf
控制质量,并使用-preset
控制编码速度.参见 FFmpeg Wiki:H.264 和For video, control quality with
-crf
and encoding speed with-preset
. See FFmpeg Wiki: H.264 and FFmpeg Wiki: AAC for more info.如果目标设备受支持的H.264配置文件限制,则可以添加
-profile:v main
或-profile:v baseline
.If your target devices are limited in the H.264 profiles they support you can add
-profile:v main
or-profile:v baseline
.您可以创建一个脚本来自动执行此操作.
ffprobe
可用于确定格式:You can make a script to automate this.
ffprobe
can be used to determine the formats:$ ffprobe -loglevel error -select_streams v:0 -show_entries stream=codec_name -of csv=p=0 video.mkv h264 $ ffprobe -loglevel error -select_streams a:0 -show_entries stream=codec_name -of csv=p=0 audio.mka aac
ffprobe
的输出可用作if/then语句中的变量.The
ffprobe
outputs can be used as variables in an if/then statement.这篇关于如何混用MKV和MKA文件并使其在浏览器中播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-