Android 音视频深入 二十一 FFmpeg视频剪切

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 音视频深入 二十一 FFmpeg视频剪切相关的知识,希望对你有一定的参考价值。

视频剪切我意外的发现上一次的视频压缩的代码能够运行FFmpeg视频剪切的命令,但是不能做视频合并的命令,因为不能读取记录了几个视频的路径的txt文件。

这里我就说直说视频剪切的过程,不说代码,只说log,毕竟我也不清楚代码往哪运行了
?上一次的项目地址
https://github.com/979451341/FFmpegCompress

首先我们需要将项目代码里改一下,将runCommand函数传入的参数改成下面这样

                int ret = FFmpegNativeBridge.runCommand(new String[]{"ffmpeg",
                        "-ss", "00:00:00",
                        "-t","00:00:10",
                        "-i", et_input.getText().toString(),
                        "-vcodec", "copy",
                        "-acodec", "copy",
                        et_output.getText().toString()});

开始就是解析命令

读取开始时间,就是-ss后面的"00:00:00",你也可以写成0

03-10 14:19:55.430 21706-22256/org.voiddog.ffmpeg D/TAG: Splitting the commandline.
03-10 14:19:55.430 21706-22256/org.voiddog.ffmpeg D/TAG: Reading option ‘-ss‘ ...
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: matched as option ‘ss‘ (set the start time offset) with argument ’00:00:00‘.

读取结束时间,就是-t后面的时间,你也可以写成10

03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Reading option ‘-t‘ ...
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: matched as option ‘t‘ (record or transcode "duration" seconds of audio/video) with argument ’00:00:10‘.

设置输入视频文件,就是切割这个视频文件的前10秒形成新的视频

03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Reading option ‘-i‘ ...
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: matched as input url with argument ‘/storage/emulated/0/pauseRecordDemo/video/video.mp4‘.

复制视频流

03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Reading option ‘-vcodec‘ ...
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: matched as option ‘vcodec‘ (force video codec (‘copy‘ to copy stream)) with argument ‘copy‘.

复制音频流

03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Reading option ‘-acodec‘ ...
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: matched as option ‘acodec‘ (force audio codec (‘copy‘ to copy stream)) with argument ‘copy‘.

最后就是选择输出视频文件路径

03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Reading option ‘/storage/emulated/0/pauseRecordDemo/video/compress6.mp4‘ ...
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: matched as output url.
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Finished splitting the commandline.
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Parsing a group of options: global .
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Successfully parsed a group of options.

开始创建并配置输出视频文件

03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Parsing a group of options: input url /storage/emulated/0/pauseRecordDemo/video/video.mp4.
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Applying option ss (set the start time offset) with argument 00:00:00.
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Applying option t (record or transcode "duration" seconds of audio/video) with argument 00:00:10.
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Successfully parsed a group of options.

然后打开它

03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Opening an input file: /storage/emulated/0/pauseRecordDemo/video/video.mp4.
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Opening ‘/storage/emulated/0/pauseRecordDemo/video/video.mp4‘ for reading
03-10 14:19:55.431 21706-22256/org.voiddog.ffmpeg D/TAG: Setting default whitelist ‘file,crypto‘
03-10 14:19:55.434 21706-22256/org.voiddog.ffmpeg D/TAG: Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100
03-10 14:19:55.434 21706-22256/org.voiddog.ffmpeg D/TAG: ISO: File Type Major Brand: mp42

解析输出视频文件的头部,不知道啥用

03-10 14:19:55.441 21706-22256/org.voiddog.ffmpeg D/TAG: stts: 0 ctts: 7507, ctts_index: 0, ctts_count: 699
03-10 14:19:55.441 21706-22256/org.voiddog.ffmpeg D/TAG: stts: 3753 ctts: 7507, ctts_index: 0, ctts_count: 699
03-10 14:19:55.441 21706-22256/org.voiddog.ffmpeg D/TAG: stts: 7507 ctts: 7507, ctts_index: 0, ctts_count: 699
03-10 14:19:55.441 21706-22256/org.voiddog.ffmpeg D/TAG: stts: 11260 ctts: 15015, ctts_index: 1, ctts_count: 699
03-10 14:19:55.441 21706-22256/org.voiddog.ffmpeg D/TAG: stts: 15014 ctts: 7508, ctts_index: 2, ctts_count: 699

获取编码器,和获取输入视频文件的媒体格式信息

03-10 14:19:55.600 21706-22256/org.voiddog.ffmpeg D/TAG: Parsing MediaFormat {image-data=java.nio.HeapByteBuffer[pos=0 lim=104 cap=104], mime=video/raw, crop-top=0, crop-right=1279, slice-height=720, color-format=21, height=720, width=1280, crop-bottom=719, crop-left=0, stride=1280}
03-10 14:19:55.601 21706-22256/org.voiddog.ffmpeg I/TAG: Output crop parameters top=0 bottom=719 left=0 right=1279, resulting dimensions width=1280 height=720
03-10 14:19:55.601 21706-22256/org.voiddog.ffmpeg D/TAG: MediaCodec 0xe4998b40 started successfully
03-10 14:19:55.601 21706-22256/org.voiddog.ffmpeg I/TAG: MediaCodec started successfully, ret = 0

获取输入视频文件的相关数据

03-10 14:19:55.643 21706-22256/org.voiddog.ffmpeg D/TAG: After avformat_find_stream_info() pos: 1343 bytes read:93574 seeks:2 frames:4
03-10 14:19:55.643 21706-22256/org.voiddog.ffmpeg I/TAG: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘/storage/emulated/0/pauseRecordDemo/video/video.mp4‘:
03-10 14:19:55.643 21706-22256/org.voiddog.ffmpeg I/TAG: Metadata:
03-10 14:19:55.643 21706-22256/org.voiddog.ffmpeg I/TAG: major_brand :
03-10 14:19:55.643 21706-22256/org.voiddog.ffmpeg I/TAG: mp42
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: minor_version :
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: 512
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: compatible_brands:
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: isomiso2avc1mp41
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: encoder :
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: HandBrake 0.10.0 2014112200
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: Duration:
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: 00:00:31.81
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: , start:
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: 0.083000
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: , bitrate:
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: 1878 kb/s
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: Stream #0:0
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: (und)
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg D/TAG: , 3, 1/90000
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: : Video: h264 (avc1 / 0x31637661), nv12, 1280x720, 1728 kb/s
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: ,
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: 23.98 fps,
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: 23.98 tbr,
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: 90k tbn,
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: 90k tbc
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: (default)
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: Metadata:
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: handler_name :
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: VideoHandler
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: Stream #0:1
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: (und)
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg D/TAG: , 1, 1/48000
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: : Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 146 kb/s
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: (default)
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: Metadata:
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: handler_name :
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg I/TAG: Stereo
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg D/TAG: Successfully opened the file.

配置输出视频文件的相关数据

03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg D/TAG: Parsing a group of options: output url /storage/emulated/0/pauseRecordDemo/video/compress6.mp4.
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg D/TAG: Applying option vcodec (force video codec (‘copy‘ to copy stream)) with argument copy.
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg D/TAG: Applying option acodec (force audio codec (‘copy‘ to copy stream)) with argument copy.
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg D/TAG: Successfully parsed a group of options.
03-10 14:19:55.644 21706-22256/org.voiddog.ffmpeg D/TAG: Opening an output file: /storage/emulated/0/pauseRecordDemo/video/compress6.mp4.
03-10 14:19:55.648 21706-22256/org.voiddog.ffmpeg D/TAG: Setting default whitelist ‘file,crypto‘
03-10 14:19:55.649 21706-22256/org.voiddog.ffmpeg D/TAG: Successfully opened the file.
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Output #0, mp4, to ‘/storage/emulated/0/pauseRecordDemo/video/compress6.mp4‘:
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Metadata:
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: major_brand :
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: mp42
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: minor_version :
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: 512
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: compatible_brands:
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: isomiso2avc1mp41
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: encoder :
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Lavf57.73.100
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Stream #0:0
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: (und)
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg D/TAG: , 0, 1/90000
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: : Video: h264 ([33][0][0][0] / 0x0021), nv12, 1280x720, q=2-31, 1728 kb/s
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: ,
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: 23.98 fps,
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: 23.98 tbr,
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: 90k tbn,
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: 90k tbc
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: (default)
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Metadata:
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: handler_name :
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: VideoHandler
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Stream #0:1
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: (und)
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg D/TAG: , 0, 1/48000
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: : Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 146 kb/s
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: (default)
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Metadata:
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: handler_name :
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Stereo

将输入视频文件的音视频流的前10秒数据复制到输出视频文件
明明是最重要的部分,却只有这点log

03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Stream mapping:
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Stream #0:0 -> #0:0
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: (copy)
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Stream #0:1 -> #0:1
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: (copy)
03-10 14:19:55.650 21706-22256/org.voiddog.ffmpeg I/TAG: Press [q] to stop, [?] for help

统计出之前复制的数据的包

03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg I/TAG: frame= 244 fps=0.0 q=-1.0 Lsize= 2522kB time=00:00:10.06 bitrate=2052.0kbits/s speed= 136x
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg I/TAG: video:2280kB audio:233kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg I/TAG: 0.355927%
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: Input file #0 (/storage/emulated/0/pauseRecordDemo/video/video.mp4):
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: Input stream #0:0 (video):
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: 245 packets read (2343932 bytes);
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: Input stream #0:1 (audio):
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: 474 packets read (238991 bytes);
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: Total: 719 packets (2582923 bytes) demuxed
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: Output file #0 (/storage/emulated/0/pauseRecordDemo/video/compress6.mp4):
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: Output stream #0:0 (video):
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: 244 packets muxed (2335158 bytes);
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: Output stream #0:1 (audio):
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: 473 packets muxed (238407 bytes);
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg V/TAG: Total: 717 packets (2573565 bytes) muxed
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg D/TAG: 0 frames successfully decoded, 0 decoding errors
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg D/TAG: Statistics: 60 seeks, 764 writeouts
03-10 14:19:55.725 21706-22256/org.voiddog.ffmpeg D/TAG: Statistics: 2662503 bytes read, 2 seeks

效果

这个compress6.mp4就是从video.mp4切割出来的,video.mp4是31秒,compress6.mp4是10秒的时长,他们的体积大小吻合时间比例。

以上是关于Android 音视频深入 二十一 FFmpeg视频剪切的主要内容,如果未能解决你的问题,请参考以下文章

Android 音视频深入 十一 FFmpeg和AudioTrack播放声音(附源码下载)

Python基础(二十一):面向对象深入了解

Python基础(二十一):面向对象深入了解

二十一CANdelaStudio深入-SPRMIB的配置

Android开发实战(二十一):浅谈android:clipChildren属性

Android开发笔记(二十一)横幅轮播页Banner