h264格式视频转mp4

使用jave将h264格式视频转MP4

起因:最近做一个高空抛物的项目,其中需要将摄像机抓拍的h264格式视频文件转mp4,进行浏览器在线播放,经过多番资料查找,找到视频编解码工具ffmpeg,利用该工具,直接拼接命令也是可以的,但服务器端还需安装ffmpeg工具,另一种方法就是我要展示的,利用jave(暂且理解为java封装的ffmpeg工具)进行转换。
代码如下:
引入pom依赖:

		<dependency>
			<groupId>ws.schild</groupId>
			<artifactId>jave-core</artifactId>
			<version>2.4.6</version>
		</dependency>
		<!--linux下引入这个-->
		<dependency>
			<groupId>ws.schild</groupId>
			<artifactId>jave-native-linux64</artifactId>
			<version>2.4.6</version>
		</dependency>
		<!--windows下引入这个-->
		<!--<dependency>
			<groupId>ws.schild</groupId>
			<artifactId>jave-native-win64</artifactId>
			<version>2.4.6</version>
		</dependency>-->
	/**
     * 
     * @param filePath 源文件路径
     */
    public static void h264ToMp4(String filePath) {
        if (StringUtils.isEmpty(filePath) || !filePath.endsWith("h264")) {
            return;
        }
        try {
            File file = new File(filePath);
            String mp4FilePath = filePath.replace("h264", "mp4");
            File mpFile = new File(mp4FilePath);
            if (!mpFile.exists()) {
                mpFile.createNewFile();
            }
            //获取文件多媒体类
            MultimediaObject sourceFile = new MultimediaObject(file);

            VideoAttributes video = new VideoAttributes();
            AudioAttributes audio = new AudioAttributes();
            //音频编码器
            audio.setCodec("libmp3lame");
            //位速率又叫比特率,是指在单位时间内可以传输多少数据
            audio.setBitRate(new Integer(64000));
            //音频的通道数,一般来说 都是单通道和双通道(立体音)
            audio.setChannels(new Integer(1));
            //是指在数码音频和视频技术应用中,当进行模拟/数码转换时,每秒钟对模拟信号进行取样时的快慢次数
            audio.setSamplingRate(new Integer(22050));
            //视频编码器
            video.setCodec("libx264");
            //位速率又叫比特率,是指在单位时间内可以传输多少数据
            video.setBitRate(new Integer(800000));
            //画面桢速率
            video.setFrameRate(new Integer(20));
            video.setSize(new VideoSize(1920, 1080));
            EncodingAttributes attr = new EncodingAttributes();
            attr.setFormat("mp4");
            attr.setAudioAttributes(audio);
            attr.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            encoder.encode(sourceFile, mpFile, attr);
        } catch (Exception e) {
            log.error("h264视频转mp4异常:", e);
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
关于将h264格式文件换为mp4格式的命令,可以参考以下步骤: 1. 打开格式工厂软件,并选择MP4文件选项。 2. 点击添加文件按钮,选择录制好的h264码流文件,可以添加多个文件。 3. 添加完成后,点击确定按钮开始换,稍等片刻,等待换成功。 4. 点击输出路径按钮,可以查看文件输出路径,将换后的视频复制到需要的数据集目录下。 5. 可以按照规定的文件命名方式,如01-厨余垃圾-Watermelon rind,更加简单明了。 具体的命令可以根据格式工厂软件的操作界面来进行操作,这里没有提供具体的命令。但是,如果你想了解更多相关参数的理解,可以参考提供的链接,该链接中有关于相关参数的详细解释。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [使用格式工厂将H264文件mp4文件](https://blog.csdn.net/Wu_GuiMing/article/details/123717780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ffmpeg 将h264格式文件编码为MP4文件](https://blog.csdn.net/u012372584/article/details/125442188)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值