JavaCV流到流轉化的一個小應用

1、應用場景

在實際開發過程中,推流和拉流不想生成本地文件,以節省服務器對磁盤的要求。網上大量文檔都是生成本地文件的形式來做操作。因此,在查看源碼的基礎之上,參考網上文檔,碼了一個工具類。

2、具體應用類

本類只是流到流不產生具體的本地文件,如有需要可以網上搜索相關文檔,很多。

代碼:

import org.bytedeco.javacv.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * @author hilbert.xu
 */
public class Convert {

    private static final Logger log = LoggerFactory.getLogger(Convert.class);

    private static ByteArrayOutputStream convert2OutputStream(InputStream inputStream) {
        Frame audioSamples = null;
        ByteArrayOutputStream swapStream = null;
        // 音頻錄製(輸出地址,音頻通道)
        FFmpegFrameRecorder recorder = null;
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputStream);
        // 開啓抓取器
        if (start(grabber)) {
            swapStream = new ByteArrayOutputStream();
            recorder = new FFmpegFrameRecorder(swapStream, 1);
            recorder.setAudioOption("crf", "0");
            recorder.setAudioBitrate(16);
            recorder.setAudioChannels(1);
            recorder.setSampleRate(16000);
            recorder.setAudioQuality(0);
            recorder.setFormat("wav"); // 具體轉化音視頻類型
            recorder.setAudioOption("aq", "10");
            // 開啓錄製器
            if (start(recorder)) {
                try {
                    // 抓取音頻
                    while ((audioSamples = grabber.grab()) != null) {
                        recorder.setTimestamp(grabber.getTimestamp());
                        recorder.record(audioSamples);
                    }
                } catch (org.bytedeco.javacv.FrameGrabber.Exception e1) {
                    log.error("抓取失敗");
                } catch (Exception e) {
                    log.error("錄製失敗");
                }
                stop(grabber);
                stop(recorder);
            }
        }
        return swapStream;
    }

    public static String convert2String(InputStream inputStream) {
        ByteArrayOutputStream outputStream = convert2OutputStream(inputStream);
        return outputStream.toString();
    }

    public static byte[] convert2ByteArray(InputStream inputStream) {
        ByteArrayOutputStream outputStream = convert2OutputStream(inputStream);
        return outputStream.toByteArray();
    }

    private static boolean start(FrameGrabber grabber) {
        try {
            grabber.start();
            return true;
        } catch (org.bytedeco.javacv.FrameGrabber.Exception e2) {
            try {
                log.error("首次打開抓取器失敗,準備重啓抓取器...");
                grabber.restart();
                return true;
            } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
                try {
                    log.error("重啓抓取器失敗,正在關閉抓取器...");
                    grabber.stop();
                } catch (org.bytedeco.javacv.FrameGrabber.Exception e1) {
                    log.error("停止抓取器失敗!");
                }
            }

        }
        return false;
    }

    private static boolean start(FrameRecorder recorder) {
        try {
            recorder.start();
            return true;
        } catch (Exception e2) {
            try {
                log.error("首次打開錄製器失敗!準備重啓錄製器...");
                recorder.stop();
                recorder.start();
                return true;
            } catch (Exception e) {
                try {
                    log.error("重啓錄製器失敗!正在停止錄製器...");
                    recorder.stop();
                } catch (Exception e1) {
                    log.error("關閉錄製器失敗!");
                }
            }
        }
        return false;
    }

    private static boolean stop(FrameGrabber grabber) {
        try {
            grabber.flush();
            grabber.stop();
            return true;
        } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
            return false;
        } finally {
            try {
                grabber.stop();
            } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
                log.error("關閉抓取器失敗");
            }
        }
    }

    private static boolean stop(FrameRecorder recorder) {
        try {
            recorder.stop();
            recorder.release();
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            try {
                recorder.stop();
            } catch (Exception e) {

            }
        }
    }
}

3、 總結

博主查了大量文檔也未解決自己的需求,因此自己碼了此類,希望對有需要的童鞋有所幫助。歡迎大家學習討論。

另:以示記錄,同技術死磕到底。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章