javacv icon indicating copy to clipboard operation
javacv copied to clipboard

Can I cut the audio?

Open Code-Lonely opened this issue 1 year ago • 10 comments

Can I cut the audio? For example, how can I cut a 10 minute audio file to 5 minutes

Code-Lonely avatar Sep 15 '22 15:09 Code-Lonely

That's most easily achieved with the ffmpeg program: http://bytedeco.org/javacpp-presets/ffmpeg/apidocs/org/bytedeco/ffmpeg/ffmpeg.html

saudet avatar Sep 15 '22 21:09 saudet

使用 ffmpeg 程序最容易做到这一点:http: //bytedeco.org/javacpp-presets/ffmpeg/apidocs/org/bytedeco/ffmpeg/ffmpeg.html

Can you give me an example

Code-Lonely avatar Sep 16 '22 01:09 Code-Lonely

There are a lot of examples everywhere, for example: https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg

saudet avatar Sep 16 '22 02:09 saudet

According to your statement, I successfully cropped the audio file on Windows, but the following errors will occur when running on Linux

Caused by: java.lang.UnsatisfiedLinkError: /root/.javacpp/cache/ffmpeg-5.0-1.5.7-linux-x86_64.jar/org/bytedeco/ffmpeg/linux-x86_64/libjniavdevice.so (libxcb.so.1: cannot open shared object file: No such file or directory)

Code-Lonely avatar Sep 16 '22 03:09 Code-Lonely

This is my code

        String ffmpeg = Loader.load(ffmpeg.class);
        String startTimeStr = String.valueOf(startTime);
        String endTimeStr = String.valueOf(endTime);
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpeg,
                "-ss", startTimeStr, "-to", endTimeStr, 
                "-i", inputFile.getAbsolutePath(),     
                "-acodec", "copy",                    
                outputFile.getAbsolutePath());        
        try {
            // 执行命令并等待
            processBuilder.inheritIO().start().waitFor();
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }

Can you tell me how to solve it

Code-Lonely avatar Sep 16 '22 03:09 Code-Lonely

FFmpeg depends on X11 by default, you'll need to install that.

saudet avatar Sep 16 '22 03:09 saudet

Need to introduce dependency? What should I do

Code-Lonely avatar Sep 16 '22 03:09 Code-Lonely

That depends on your Linux distribution.

saudet avatar Sep 16 '22 03:09 saudet

That depends on your Linux distribution.

I run the program in Docker

This is my system information

Linux huoting-server 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Code-Lonely avatar Sep 16 '22 03:09 Code-Lonely

In the case of Ubuntu, something like apt install libxcb should work.

saudet avatar Sep 18 '22 00:09 saudet

@saudet I have successfully solved the above problems by installing the missing dependencies. But now I find a more serious problem. If I use ffmpeg to cut audio, the memory will keep rising and will not be released. This is my code.

    /**
     * 音频文件裁剪
     * @param inputFile     要裁剪的文件路径
     * @param outputFile    裁剪完成输出的路径
     * @param startTime     开始时间(秒)
     * @param endTime       结束时间(秒)
     */
    public static void audioFileCut(File inputFile, File outputFile, long startTime, long endTime){
        // 加载ffmpeg可执行文件的路径
        String ffmpeg = Loader.load(ffmpeg.class);
        // 将输入转为字符串,用于后面拼接命令
        String startTimeStr = String.valueOf(startTime);
        String endTimeStr = String.valueOf(endTime);
        // 创建命令处理器
        ProcessBuilder processBuilder = new ProcessBuilder(ffmpeg,
                "-ss", startTimeStr,                   // 裁剪开始位置
                "-i", inputFile.getAbsolutePath(),     // 输入文件
                "-to", endTimeStr,                     // 裁剪结束位置
                "-acodec", "copy",                     // 只复制音频,不进行编解码操作
                outputFile.getAbsolutePath());         // 输出路径
        try {
            // 执行命令并等待
            processBuilder.inheritIO().start().waitFor();
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
    }

Do you know how to solve this problem

Code-Lonely avatar Sep 29 '22 06:09 Code-Lonely

This is the output log of ffmpeg

ffmpeg version 5.0 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 9 (GCC)
  configuration: --prefix=.. --disable-iconv --disable-opencl --disable-sdl2 --disable-bzlib --disable-lzma --disable-linux-perf --disable-xlib --enable-shared --enable-version3 --enable-runtime-cpudetect --enable-zlib --enable-libmp3lame --enable-libspeex --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-openssl --enable-libopenh264 --enable-libvpx --enable-libfreetype --enable-libopus --enable-libxml2 --enable-libsrt --enable-libwebp --enable-libmfx --enable-cuda --enable-cuvid --enable-nvenc --enable-pthreads --enable-libxcb --cc='gcc -m64' --extra-cflags='-I../include/ -I../include/libxml2' --extra-ldflags=-L../lib/ --extra-libs='-lstdc++ -lpthread -ldl -lz -lm -lva-drm -lva-x11 -lva'
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/xjz/accompaniment/temp/798a3a61-32b5-4996-a44b-5a743cf300b8/decrypt/UC10041_01_DR_DR.m4a':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: M4A mp42isom
    creation_time   : 2022-09-11T13:29:28.000000Z
    iTunSMPB        :  00000000 00000A40 000000D8 00000000029BB8E8 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    encoder         : Nero AAC codec / 1.5.4.0
  Duration: 00:16:32.35, start: 0.059501, bitrate: 93 kb/s
  Chapters:
    Chapter #0:0: start 0.059501, end 992.410701
      Metadata:
        title           : 
  Stream #0:0[0x1](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 91 kb/s (default)
    Metadata:
      creation_time   : 2022-09-11T13:29:28.000000Z
      handler_name    : Sound Media Handler
      vendor_id       : [0][0][0][0]
Output #0, ipod, to '/home/xjz/accompaniment/temp/798a3a61-32b5-4996-a44b-5a743cf300b8/music/DR.m4a':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: M4A mp42isom
    iTunSMPB        :  00000000 00000A40 000000D8 00000000029BB8E8 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    encoder         : Lavf59.16.100
  Chapters:
    Chapter #0:0: start 0.000000, end 693.000000
      Metadata:
        title           : 
  Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 91 kb/s (default)
    Metadata:
      creation_time   : 2022-09-11T13:29:28.000000Z
      handler_name    : Sound Media Handler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
size=   11108kB time=00:11:33.00 bitrate= 131.3kbits/s speed=7.17e+03x    
video:0kB audio:10989kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.074620%
ffmpeg version 5.0 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 9 (GCC)
  configuration: --prefix=.. --disable-iconv --disable-opencl --disable-sdl2 --disable-bzlib --disable-lzma --disable-linux-perf --disable-xlib --enable-shared --enable-version3 --enable-runtime-cpudetect --enable-zlib --enable-libmp3lame --enable-libspeex --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-openssl --enable-libopenh264 --enable-libvpx --enable-libfreetype --enable-libopus --enable-libxml2 --enable-libsrt --enable-libwebp --enable-libmfx --enable-cuda --enable-cuvid --enable-nvenc --enable-pthreads --enable-libxcb --cc='gcc -m64' --extra-cflags='-I../include/ -I../include/libxml2' --extra-ldflags=-L../lib/ --extra-libs='-lstdc++ -lpthread -ldl -lz -lm -lva-drm -lva-x11 -lva'
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/xjz/accompaniment/temp/798a3a61-32b5-4996-a44b-5a743cf300b8/decrypt/UC10041_02_BS_BS.m4a':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: M4A mp42isom
    creation_time   : 2022-09-11T13:29:09.000000Z
    iTunSMPB        :  00000000 00000A40 000000D8 00000000029BB8E8 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    encoder         : Nero AAC codec / 1.5.4.0
  Duration: 00:16:32.35, start: 0.059501, bitrate: 50 kb/s
  Chapters:
    Chapter #0:0: start 0.059501, end 992.410701
      Metadata:
        title           : 
  Stream #0:0[0x1](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 48 kb/s (default)
    Metadata:
      creation_time   : 2022-09-11T13:29:09.000000Z
      handler_name    : Sound Media Handler
      vendor_id       : [0][0][0][0]
Output #0, ipod, to '/home/xjz/accompaniment/temp/798a3a61-32b5-4996-a44b-5a743cf300b8/music/BS.m4a':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: M4A mp42isom
    iTunSMPB        :  00000000 00000A40 000000D8 00000000029BB8E8 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    encoder         : Lavf59.16.100
  Chapters:
    Chapter #0:0: start 0.000000, end 693.000000
      Metadata:
        title           : 
  Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 48 kb/s (default)
    Metadata:
      creation_time   : 2022-09-11T13:29:09.000000Z
      handler_name    : Sound Media Handler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
size=    5907kB time=00:11:33.00 bitrate=  69.8kbits/s speed=7.56e+03x    
video:0kB audio:5789kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.038484%

Is it related to my audio format?

Code-Lonely avatar Sep 29 '22 06:09 Code-Lonely

The project is spring boot deployed with Docker,The basic image uses ibmjava:8-sfj Dockerfile contents are as follows

# 基础镜像
FROM ibmjava:8-sfj
# author
MAINTAINER xjz
# Installing FFmpeg runtime dependencies
RUN apt-get update -y && apt-get install -y \
 libxcb1 \
 libxcb-shm0 \
 libxcb-shape0 \
 libxcb-xfixes0 \
 libasound2
# 挂载目录
VOLUME /home/xjz
# 创建目录
RUN mkdir -p /home/xjz
# 指定工作路径
WORKDIR /home/xjz
# 复制jar文件到路径,并重命名为app.jar
COPY ./target/*.jar /home/xjz/app.jar
# 启动服务
CMD ["java","-jar","app.jar"]

I wonder what the problem is?

Code-Lonely avatar Sep 29 '22 08:09 Code-Lonely

That depends on your Linux distribution.

saudet avatar Oct 11 '22 06:10 saudet