ffmpeg的各种黑科技

获取音频的时长

/**
     * 获取视频文件的时长
     * @param ffmpegPath 是ffmpeg软件存放的目录,sourceFile是目标文件
     * @return
     */
    public String duration(String ffmpegPath,String sourceFile){
        List<String> duration = new ArrayList<String>();
        String ffmpegroot = ffmpegPath+"/ffmpeg";
        duration.add(ffmpegroot);
        duration.add("-i");
        duration.add(sourceFile);
        ProcessBuilder pb = new ProcessBuilder();
        pb.command(duration);
        pb.redirectErrorStream(true);
        InputStream is = null;
        BufferedReader br = null;
        try {
            Process p = pb.start();
            is = p.getInputStream();
            StringBuffer outS = new StringBuffer("");
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = br.readLine();
            while(line!=null){
                outS.append(line);
                line = br.readLine();
            }
            String out = outS.toString();
            int index = out.indexOf("Duration:");
            int end = out.indexOf(",", index);
            if(index>=0){
                String result = out.substring(index+10, end);
                return result;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                br.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


音频的原时长:00:00:25.60

获取音频的多少秒(和上面那个方法一块使用)

 //参数格式:"00:00:10.68"  
    public float getTimelen(String timelen){ 
        float min=0.0f;  
        String strs[] = timelen.split(":");  
        if (strs[0].compareTo("0") > 0) {  
            min+=Integer.valueOf(strs[0])*60*60;//
        }  
        if(strs[1].compareTo("0")>0){  
            min+=Integer.valueOf(strs[1])*60;  
        }  
        if(strs[2].compareTo("0")>0){  
            min+=Float.valueOf(strs[2]);  
        }  
        return min;  
    } 

截取音频的指定时长

/**
     * 截取音频的  从front-duration秒
     * @param ffmpegPath ffmpeg程序的路径
     * @param sourcePath 源文件
     * @param targetPath 需要生成的文件
     * @param front      从多少秒开始截取
     * @param duration   一共截取多长时间
     * @throws Exception
     */
    public static void wavCut(String ffmpegPath,String sourcePath, String targetPath,double front,double duration) throws Exception {  
        List<String> wavToPcm = new ArrayList<String>();
        wavToPcm.add(ffmpegPath+"/ffmpeg");
        wavToPcm.add("-i");
        wavToPcm.add(sourcePath);
        wavToPcm.add("-ss");
        wavToPcm.add("00:00:0"+front);
        wavToPcm.add("-t");
        wavToPcm.add(duration+"");
        wavToPcm.add("-y");
        wavToPcm.add(targetPath);
        ProcessBuilder builder = new ProcessBuilder();
        builder.command(wavToPcm);
        for(String str:wavToPcm){
            System.out.print(str+" ");
        }
        System.out.println();
        builder.redirectErrorStream(true);
        try {
            Process process=builder.start();
            int a=process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch(InterruptedException ie){
            ie.printStackTrace();
        }
    }

tip注意

ffmpeg截取一段视频中一段视频

ffmpeg  -i ./plutopr.mp4 -vcodec copy -acodec copy -ss 00:00:10 -to 00:00:15 ./cutout1.mp4 -y

 

-ss time_off        set the start time offset 设置从视频的哪个时间点开始截取,上文从视频的第10s开始截取
-to 截到视频的哪个时间点结束。上文到视频的第15s结束。截出的视频共5s.
如果用-t 表示截取多长的时间如 上文-to 换位-t则是截取从视频的第10s开始,截取15s时长的视频。即截出来的视频共15s.
 
注意的地方是:
 如果将-ss放在-i ./plutopr.mp4后面则-to的作用就没了,跟-t一样的效果了,变成了截取多长视频。一定要注意-ss的位置。
 
参数解析
-vcodec copy表示使用跟原视频一样的视频编解码器。
-acodec copy表示使用跟原视频一样的音频编解码器。
 
-i 表示源视频文件
-y 表示如果输出文件已存在则覆盖。
----------------

音频格式转换 ---转8位

public static void changeWavToRightWav(String ffmpegPath,String sourcePath, String targetPath) throws Exception {  
            List<String> wavToWav = new ArrayList<String>();
            wavToWav.add(ffmpegPath+"/ffmpeg");
            wavToWav.add("-i");
            wavToWav.add(sourcePath);
            
            wavToWav.add("-ar");
            wavToWav.add("8000");
            
            wavToWav.add("-ac");
            wavToWav.add("1");
            
            wavToWav.add("-acodec");
            wavToWav.add("pcm_alaw");
            wavToWav.add("-y");
            wavToWav.add(targetPath);
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(wavToWav);
            builder.redirectErrorStream(true);
            final Process process=builder.start();
            //处理InputStream的线程
            new Thread()
            {
                @Override
                public void run()
                {
                    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
                    String line = null;
                    try 
                    {
                        while((line = in.readLine()) != null)
                        {
                            System.out.println("output: " + line);
                        }
                    } 
                    catch (IOException e) 
                    {                        
                        e.printStackTrace();
                    }
                    finally
                    {
                        try 
                        {
                            in.close();
                        } 
                        catch (IOException e) 
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            new Thread()
            {
                @Override
                public void run()
                {
                    BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
                    String line = null;
                    
                    try 
                    {
                        while((line = err.readLine()) != null)
                        {
                            System.out.println("err: " + line);
                        }
                    } 
                    catch (IOException e) 
                    {                        
                        e.printStackTrace();
                    }
                    finally
                    {
                        try 
                        {
                            err.close();
                        } 
                        catch (IOException e) 
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            int a=process.waitFor();
        }

arm转wav

public static void changeAmrToWav(String ffmpegPath,String sourcePath, String targetPath) throws Exception {  
        List<String> wavToPcm = new ArrayList<String>();
        wavToPcm.add(ffmpegPath+"/ffmpeg");
        wavToPcm.add("-y");
        wavToPcm.add("-i");
        wavToPcm.add(sourcePath);
        wavToPcm.add("-acodec");
        wavToPcm.add("pcm_alaw");
        wavToPcm.add(targetPath);
        ProcessBuilder builder = new ProcessBuilder();
        builder.command(wavToPcm);
        builder.redirectErrorStream(true);
        final Process process=builder.start();
        //处理InputStream的线程
        new Thread()
        {
            @Override
            public void run()
            {
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
                String line = null;
                try 
                {
                    while((line = in.readLine()) != null)
                    {
                        System.out.println("output: " + line);
                    }
                } 
                catch (IOException e) 
                {                        
                    e.printStackTrace();
                }
                finally
                {
                    try 
                    {
                        in.close();
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread()
        {
            @Override
            public void run()
            {
                BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
                String line = null;
                
                try 
                {
                    while((line = err.readLine()) != null)
                    {
                        System.out.println("err: " + line);
                    }
                } 
                catch (IOException e) 
                {                        
                    e.printStackTrace();
                }
                finally
                {
                    try 
                    {
                        err.close();
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        int a=process.waitFor();
    }

得到amr的时长

/**
     * 得到amr的时长
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static long getAmrDuration(File file) throws IOException {
        long duration = -1;
        int[] packedSize = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            long length = file.length();//文件的长度
            int pos = 6;//设置初始位置
            int frameCount = 0;//初始帧数
            int packedPos = -1;
            /////////////////////////////////////////////////////
            byte[] datas = new byte[1];//初始数据值
            while (pos <= length) {
                randomAccessFile.seek(pos);
                if (randomAccessFile.read(datas, 0, 1) != 1) {
                    duration = length > 0 ? ((length - 6) / 650) : 0;
                    break;
                }
                packedPos = (datas[0] >> 3) & 0x0F;
                pos += packedSize[packedPos] + 1;
                frameCount++;
            }
            /////////////////////////////////////////////////////
            duration += frameCount * 20;//帧数*20
        } finally {
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
        }
        return duration;

    }

获取音频速率

/**
     * 功能:获取音频速率
     * @param file
     * @return
     * @throws Exception
     */
    public static Integer getWavRate(File file) throws Exception{
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream buffer = new BufferedInputStream(fis);
        AudioInputStream ain = AudioSystem.getAudioInputStream(buffer);
        AudioFormat format=ain.getFormat();
        Float frameRate = format.getFrameRate();
        return frameRate!=null?frameRate.intValue():0;
    }

 wav转pcm

public static void changeWavToPcm(String ffmpegPath,String sourcePath, String targetPath) throws Exception {  
    
        List<String> wavToPcm = new ArrayList<String>();
        wavToPcm.add(ffmpegPath+"/ffmpeg");
        wavToPcm.add("-y");
        wavToPcm.add("-i");
        wavToPcm.add(sourcePath);
        wavToPcm.add("-acodec");
        wavToPcm.add("pcm_s16le");
        wavToPcm.add("-f");
        wavToPcm.add("s16le"); 
        wavToPcm.add("-ac"); 
        wavToPcm.add("1"); 
        wavToPcm.add("-ar"); 
        wavToPcm.add("8000"); 
        wavToPcm.add(targetPath);
        ProcessBuilder builder = new ProcessBuilder();
        builder.command(wavToPcm);
        builder.redirectErrorStream(true);
        final Process process=builder.start();
        //处理InputStream的线程
        new Thread()
        {
            @Override
            public void run()
            {
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
                String line = null;
                try 
                {
                    while((line = in.readLine()) != null)
                    {
                        System.out.println("output: " + line);
                    }
                } 
                catch (IOException e) 
                {                        
                    e.printStackTrace();
                }
                finally
                {
                    try 
                    {
                        in.close();
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        new Thread()
        {
            @Override
            public void run()
            {
                BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
                String line = null;
                try 
                {
                    while((line = err.readLine()) != null)
                    {
                        System.out.println("err: " + line);
                    }
                } 
                catch (IOException e) 
                {                        
                    e.printStackTrace();
                }
                finally
                {
                    try 
                    {
                        err.close();
                    } 
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        process.waitFor();
    
    }

猜你喜欢

转载自www.cnblogs.com/coder-lzh/p/9550263.html