mp4格式的视频,编码方式mpeg4,转化为h264

时间:2023-01-21 19:58:13

知识点:在使用vcastr3.swf播放器播放flv视频,(同时在html5页面,使用《video》标签时),发现某些MP4格式的代码不能播放

原因:vcastr3.swf和video,不支持mpeg4编码格式的MP4视频播放

参考:http://www.runoob.com/html/html5-video.html  (video标签)

          https://www.cnblogs.com/frost-yen/p/5848781.html (ffmpeg常用命令)

          https://blog.csdn.net/daidaineteasy/article/details/53861429  (html使用vcastr3.swf播放器播放flv视频)

          https://www.cnblogs.com/ghlin/articles/8202842.html  (linux下java使用ffmpeg将MP4视频转为H264编码)

解决方案:1:后台获取编码格式给用户,不允许上传

                  2:利用ffmpeg工具,将mpeg4转化h264格式的视频文件

 

方案2代码:

        配置ffmpeg环境变量以后,可使用命令直接转换编码方式

        视频编码格式转换

          比如一个视频的编码是MPEG4,想用H264编码,咋办?
          ffmpeg -i input.mp4 -vcodec h264 output.mp4  //input.mp4是指要转换视频的地址;output.mp4是转化后视频的存放路径
         相反也一样
         ffmpeg -i input.mp4 -vcodec mpeg4 output.mp4

 
  java代码实现视频编码格式转换

          private void transfer(String infile,String outfile) {
           
                    String videoCommend = "ffmpeg -i " + infile + " -vcodec libx264 -r 29.97 -b 768k -ar 24000 -ab 64k -s 1280x720 "
                                    + outfile;

                   try {
                        Runtime rt = Runtime.getRuntime();
                       Process proc = rt.exec(videoCommend);
                       InputStream stderr = proc.getErrorStream();
                      InputStreamReader isr = new InputStreamReader(stderr);
                      BufferedReader br = new BufferedReader(isr);
                     String line = null;

                     while ( (line = br.readLine()) != null)
                           System.out.println(line);

                     int exitVal = proc.waitFor();
                       System.out.println("Process exitValue: " + exitVal);
                } catch (Throwable t) {
                         t.printStackTrace();
                }
             }