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秒的时长,他们的体积大小吻合时间比例。

原文地址:http://blog.51cto.com/13591594/2084826

时间: 2024-10-07 11:18:35

Android 音视频深入 二十一 FFmpeg视频剪切的相关文章

Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment

你可以把fragment看成是activity的模块化部分,它拥有自己的生命周期,接受它自己的输入事件,你可以在activity运行时添加或者删除它(有点像是一个"子activity",你可以在不同的activity中重用它).本课将向你展示如何使用Support Libaray继承 Fragment 类来让你的应用能够兼容正在运行Android 1.6的设备. 提示: 如果你决定你的应用需求的最低API级别是11或者更高,那么你不需要使用Support Library,你可以直接使用

android产品研发(二十一)-->UI优化

转载请标明出处:一片枫叶的专栏 上一篇文章中我们讲解了android产品研发过程中的代码Review.通过代码Review能够提高产品质量,增强团队成员之间的沟通,提高开发效率,所以良好的产品开发迭代过程中,代码Review是一个必不可少的步骤.那么如何进行代码Review呢?我们主要讲解了团队成员之间的代码Review,代码lint检查,开发规范等方面的知识点,更多关于代码Review相关的知识可参考我的:android产品研发(二十)–>代码Review 本文我们将讲解一下android U

Android学习笔记(二十一)——实战:程序数据共享

//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 我们继续在Database项目的基础上继续开发,通过内容提供器来给它加入外部访问接口.首先将 MyDatabaseHelper 中使用 Toast弹出创建数据库成功的提示去除掉,因为跨程序访问时我们不能直接使用 Toast. 一.添加一个 DatabaseProvider 类: 1 public class DatabaseProvider extends ContentProvider { 2 public sta

Android开发系列(二十一):Spinner的功能和用法以及实现列表选择框

Spinner是一个列表选择框,相当于弹出一个菜单供用户进行选择. Spinner继承AdapterView Spinnet支持的XML的属性: android:entries:使用数组资源设置该下拉列表框的列表项目 android:popupBackground:设置下拉列表框的背景色 (一般这两个比较常用一点) 用Spinner用两种方法: 第一.已经确定下拉列表里的列表项,只要为Spinner指定android:entries属性就可以实现Spinner. 第二.如果程序需要在运行时动态地

Android开发系列(二十一):Spinner的功能和使用方法以及实现列表选择框

Spinner是一个列表选择框.相当于弹出一个菜单供用户进行选择. Spinner继承AdapterView Spinnet支持的XML的属性: android:entries:使用数组资源设置该下拉列表框的列表项目 android:popupBackground:设置下拉列表框的背景色 (一般这两个比較经常使用一点) 用Spinner用两种方法: 第一.已经确定下拉列表里的列表项,仅仅要为Spinner指定android:entries属性就能够实现Spinner. 第二.假设程序须要在执行时

Android学习路线(二十四)ActionBar Fragment运用最佳实践

通过前面的几篇博客,大家看到了Google是如何解释action bar和fragment以及推荐的用法.俗话说没有demo的博客不是好博客,下面我会介绍一下action bar和fragment在实战中的应用,以及相关demo源码,希望和大家相互交流. 了解过fragment的同学们应该都知道,fragment是android 3.0版本才出现的的,因此如果要在支持android 3.0一下版本的工程中使用fragment的话是需要添加Support Library的.具体如何添加我就不再赘述

android产品研发(二十二)--&gt;android实用调试技巧

转载请标明出处:一片枫叶的专栏 上一篇文章中我们讲解了android UI优化方面的知识.我们讲解了android中的include.marge.ViewStub标签,在使用这些标签时可以简化我们的布局文件,优化组件绘制流程:讲解了android中的过度绘制相关知识点,通过优化我们的App过度绘制可以提高App的UI绘制流程与性能:我们还讲解了App中一些UI优化的小tips.更多关于android UI优化方面的知识可以参考我的:android产品研发(二十一)–>android中的UI优化

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

项目地址,求starhttps://github.com/979451341/AudioVideoStudyCodeTwo/tree/master/FFmpeg%E6%92%AD%E6%94%BE%E9%9F%B3%E4%B9%90%EF%BC%88%E4%BF%9D%E7%A8%8B%E5%BA%8F%E4%B8%8D%E6%AD%BB%EF%BC%89 这个是FFmpeg解码出音频,给AudioTrack播放,这回才算是java与c语言之间合作 这回我们将会从c++里调用java函数,下面就

Android 音视频深入 十八 FFmpeg播放视频,有声音(附源码下载)

项目地址https://github.com/979451341/AudioVideoStudyCodeTwo/tree/master/FFmpegv%E6%92%AD%E6%94%BE%E8%A7%86%E9%A2%91%E6%9C%89%E5%A3%B0%E9%9F%B3%EF%BC%8C%E6%9A%82%E5%81%9C%EF%BC%8C%E9%87%8A%E6%94%BE%E3%80%81%E5%BF%AB%E8%BF%9B%E3%80%81%E9%80%80%E5%90%8E 这个项