首页 > 其他分享 >FFmpeg服务器适配问题

FFmpeg服务器适配问题

时间:2023-06-13 18:14:15浏览次数:37  
标签:FFmpeg 适配 video attrs File 服务器 new import audio

用org.bytedeco   javacv /  ffmpeg-platform   / javacpp  实现的ffmpeg视频抽帧截取图片 在cenos正常但是在arm64服务器有适配的问题。

解决方案 换另外的实现:

 <groupId>ws.schild</groupId>
            <artifactId>jave-all-deps</artifactId>
            <version>3.3.1</version>
 </dependency>

 

package com.maike.common.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.maike.codegenerate.common.entity.VideoItem;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.info.VideoInfo;
 
/**
 *
 * @ClassName VideoFormatUtil
 * @Description 视频格式转换工具
 * @Author geekcjj
 * @Date 2020年12月13日 上午10:52:28
 */
public class VideoFormatUtil {
 
    public static Logger Log = LoggerFactory.getLogger(VideoFormatUtil.class);
 
    /**
     * 视频文件转音频文件
     * @param videoPath
     * @param audioPath
     * @return
     */
    public static boolean videoToAudio(String videoPath, String audioPath) {
        File fileMp4=new File(videoPath);
        File fileMp3=new File(audioPath);
 
        //Audio Attributes
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
        audio.setBitRate(128000);
        audio.setChannels(2);
        audio.setSamplingRate(44100);
 
        //Encoding attributes
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setOutputFormat("mp3");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        MultimediaObject mediaObject=new MultimediaObject(fileMp4);
        try {
            encoder.encode(mediaObject, fileMp3, attrs);
            Log.info("File MP4 convertito in MP3");
            return true;
        } catch (Exception e) {
            Log.error("File non convertito");
            Log.error(e.getMessage());
            return false;
        }
    }
 
    /**
     * 获取视频的基本信息,视频长宽高,视频的大小等
     * @param fileSource
     * @return
     */
    public static VideoItem getVideoInfo(String fileSource) {
        // String filePath =
        // Utils.class.getClassLoader().getResource(fileSource).getPath();
        File source = new File(fileSource);
        //Encoder encoder = new Encoder();
        FileInputStream fis = null;
        FileChannel fc = null;
        VideoItem videoInfo = null;
        try {
            MultimediaObject MultimediaObject=new MultimediaObject(source);
            MultimediaInfo m = MultimediaObject.getInfo();
            fis = new FileInputStream(source);
            fc = fis.getChannel();
            videoInfo = new VideoItem(m.getVideo().getSize().getWidth(), m.getVideo().getSize().getHeight(), fc.size(),
                    m.getDuration(), m.getFormat());
            System.out.println(videoInfo);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fc) {
                try {
                    fc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return videoInfo;
    }
 
    /**
     * 截取视频中某一帧作为图片
     * @param videoPath
     * @param imagePath
     * @return
     */
    public static boolean getVideoProcessImage(String videoPath,String imagePath){
        long times = System.currentTimeMillis();
        File videoSource = new File(videoPath);
        File imageTarget = new File(imagePath);
        MultimediaObject object = new MultimediaObject(videoSource);
        try {
            MultimediaInfo multimediaInfo = object.getInfo();
            VideoInfo videoInfo=multimediaInfo.getVideo();
            VideoAttributes video = new VideoAttributes();
            video.setCodec("png");
            video.setSize(videoInfo.getSize());
            EncodingAttributes attrs = new EncodingAttributes();
            //VideoAttributes attrs = ecodeAttrs.getVideoAttributes().get();
            attrs.setOutputFormat("image2");
            attrs.setOffset(11f);//设置偏移位置,即开始转码位置(11秒)
            attrs.setDuration(0.01f);//设置转码持续时间(1秒)
            attrs.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            encoder.encode(object,imageTarget,attrs);
            return true;
        } catch (EncoderException e) {
            e.printStackTrace();
            return false;
        }
    }
 
    /**
     * m4r音频格式转换为mp3,audioPath可更换为要转换的音频格式
     * @param audioPath
     * @param mp3Path
     */
    public static void m4rToMp3(String audioPath,String mp3Path){
        File source = new File(audioPath);
        File target = new File(mp3Path);
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");
        audio.setBitRate(new Integer(128000));
        audio.setChannels(new Integer(2));
        audio.setSamplingRate(new Integer(44100));
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setOutputFormat("mp3");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        try {
            encoder.encode(new MultimediaObject(source), target, attrs);
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 从和视频中提取音频wav
     * @param aviPath
     * @param targetWavPath
     */
    public static void videoExtractAudio(String aviPath,String targetWavPath){
        File source = new File(aviPath);
        File target = new File(targetWavPath);
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("pcm_s16le");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setOutputFormat("wav");
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        try {
            encoder.encode(new MultimediaObject(source), target, attrs);
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 视频转换为手机可播放的格式
     * @param sourceVideo sourceVideo.avi
     * @param targetVideo targetVideo.3gp
     */
    public static void videoToMobileVideo(String sourceVideo, String targetVideo){
        File source = new File("source.avi");
        File target = new File("target.3gp");
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libfaac");
        audio.setBitRate(new Integer(128000));
        audio.setSamplingRate(new Integer(44100));
        audio.setChannels(new Integer(2));
        VideoAttributes video = new VideoAttributes();
        video.setCodec("mpeg4");
        video.setBitRate(new Integer(160000));
        video.setFrameRate(new Integer(15));
        video.setSize(new VideoSize(176, 144));
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setOutputFormat("3gp");
        attrs.setAudioAttributes(audio);
        attrs.setVideoAttributes(video);
        Encoder encoder = new Encoder();
        try {
            encoder.encode(new MultimediaObject(source), target, attrs);
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        //System.out.println("数据 = [" + getVideoProcessImage("E:/MyFile/mylove/video/dPQRVRFuZHJ8.mp4","E:/MyFile/sfaafasddxg.jpg") + "]");
        //System.out.println("数据 = [" + getVideoInfo("E:/MyFile/mylove/video/dPQRVRFuZHJ8.mp4") + "]");
        //System.out.println("数据 = [" + videoToAudio("E:/MyFile/mylove/video/dPQRVRFuZHJ8.mp4","E:/MyFile/sfaafasddxgd.mp3") + "]");
    }
 
}

 

标签:FFmpeg,适配,video,attrs,File,服务器,new,import,audio
From: https://www.cnblogs.com/thinkthree/p/17478373.html

相关文章

  • 使用nginx搭建https服务器
    最近在研究nginx,整好遇到一个需求就是希望服务器与客户端之间传输内容是加密的,防止中间监听泄露信息,但是去证书服务商那边申请证书又不合算,因为访问服务器的都是内部人士,所以自己给自己颁发证书,忽略掉浏览器的不信任警报即可。下面是颁发证书和配置过程。首先确保机器上安装了op......
  • 通过堡塔部署API到云服务器
     首先登录堡塔的官网:https://www.bt.cn/new/index.html,下载终端  下载完后安装终端,路径不用修改,安装到默认盘。安装完成后打开堡塔终端,图片如下  点击+号,添加服务器   我使用的是阿里云,服务器地址如下  登录云服务器的密码如下,  点击远程连接......
  • 服务器巡检常用命令
    Linux系统需要定期巡检,以检查服务器软硬件使用情况,确保可以及时发现问题、解决问题,降低损失。常用的巡检命令如下:free-m查看内存使用量和交换区使用量df-h查看各分区使用情况du-sh<目录名>查看指定目录的大小uptime查看系统运行时间、用户数、负载w查看活动用户l......
  • IBM Cloud:裸金属服务器+多云策略助力音视频解决方案成功出海
    到底什么是公有云、私有云和混合云?疫情给云服务厂商带来了哪些挑战?IBM是如何助力音视频解决方案成功出海的?“后疫情”时代音视频的下一个风口在哪里?对此,LiveVideoStack很荣幸地采访到了来自IBM云平台事业部,资深云计算架构师——胡磊,聊一聊他对这些问题的看法和观点。胡磊技术访谈#0......
  • 一文简述FFmpeg
    Easy-Tech#017#——FFmpegFFmpeg是一款开源软件,用于生成处理多媒体数据的各类库和程序。FFmpeg可以转码、处理视频和图片(调整视频、图片大小,去噪等)、打包、传输及播放视频。作为最受欢迎的视频和图像处理软件,它被来自各行各业的不同公司所广泛使用。审校者注:FFmpeg项目由Fabrice......
  • 浪潮信息整机柜服务器三大优势让数据中心建设“更快、更好、更省”
    数据中心到底怎么建才“快、好、省”?在数据应用越来越复杂的背景下,数字产业化,产业数字化快速推进,业务普遍开始走向“云端”,原本作为辅助支撑的IT设备,转变为核心生产工具。同时随着“计算”向“智算”的转变,整个社会的计算需求呈现指数型增长,数据中心正在向着集约化、规模化不断发展......
  • 如何实现不同服务器之间 大体量的数据自动同步?
    随着企业结构分散化的不断扩大,企业的数据中心、服务器节点、异地分支机构之间,会存在多种文件交换场景。传统的FTP、rsync、网盘等传输方式在数据体量较小、时效性要求不高的情况下,基本也可以满足需求。但随着数量爆发式增长,需要及时分析使用数据的情况下,就不太够用了,弊端也随之体......
  • linux服务器CPU飙高排查
    文章目录前言一、第一步top二、根据pid查找具体线程2.根据pid找到16进制3.根据进程和线程查找原因总结前言系统cpu飙高,尤其对于后端人员来说,其实应该学会排查,这样也算是综合能力的体现;那么当出现了cpu严重飙高的时候怎么排查呢?一、第一步top直接在问题服务器输入命令:to......
  • DDOS和CC打击的区别,哪种打击对服务器伤害更大
    近几年,网络恶意打击逐渐增多,很多网站饱受困扰,而其中最为常见的恶意打击就是CC以及DDoS打击,对于一些防御能力较弱的网站来说,一旦遭遇这些打击,轻则网站瘫痪,重则直接影响流量导致无法生存,那么DDoS打击和CC打击区别在哪里?哪一个对服务器伤害比较大?下面来简单分析一下DDoS打击DDoS打击(分......
  • U-Mail企业邮件服务器搭建方案
    数字化办公的日渐推行,使企业对邮箱的依赖与日俱增,正式工作报告,部门之间的事物往来、通知等等都需要使用到企业邮箱。随着企业对数字化建设的不断深入,企业对企业邮箱的要求也越来越高,比如对第三方应用集成及协同办公要求高,需要同时支持多种业务的开展,对企业邮箱的稳定及高效性要求高......