[TOOL] ubuntu 使用 ffmpeg 操作 gif、mp4

文章目录

  • 一、工具安装
  • [二、gif 转mp4](#二、gif 转mp4)
  • [三、mp4 两倍速](#三、mp4 两倍速)

一、工具安装

安装 ffmpeg 工具:

bash 复制代码
sudo apt install ffmpeg

二、gif 转mp4

1. 配置环境

核心指令:

bash 复制代码
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output_2x.mp4

编辑 gif2map4 文件,内容如下:

bash 复制代码
#!/bin/bash

# 检查是否输入参数
if [ $# -eq 0 ]; then
    echo "Usage: $0 <input.gif> [output.mp4]"
    echo "Example:"
    echo "  $0 mygif.gif              # 输出: mygif_YYYYMMDD_HHMMSS.mp4"
    echo "  $0 mygif.gif custom_video   # 输出: custom_video.mp4"
    exit 1
fi

# 输入文件
INPUT_FILE="$1"
if [ ! -f "$INPUT_FILE" ]; then
    echo "Error: Input file '$INPUT_FILE' not found!"
    exit 1
fi

# 输出文件名
if [ $# -ge 2 ]; then
    # 如果指定了第二个参数,直接使用
    OUTPUT_FILE="$2"
    # 确保输出文件有.mp4扩展名
    if [[ "$OUTPUT_FILE" != *.mp4 ]]; then
        OUTPUT_FILE="${OUTPUT_FILE}.mp4"
    fi
else
    # 否则自动生成带时间的文件名
    TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
    INPUT_BASENAME=$(basename "$INPUT_FILE" .gif)
    OUTPUT_FILE="${INPUT_BASENAME}_${TIMESTAMP}.mp4"
fi

# 运行转换命令
ffmpeg -i "$INPUT_FILE" -vf "fps=15,scale=640:-2:flags=lanczos" -c:v libx264 -preset slow -crf 23 -pix_fmt yuv420p "$OUTPUT_FILE"

# 检查是否成功生成文件
if [ -f "$OUTPUT_FILE" ]; then
    echo "Success! Output file:"
    echo "  - $OUTPUT_FILE"
else
    echo "Error: Failed to generate output file!"
    exit 1
fi

添加到终端指令

bash 复制代码
sudo cp gif2map4 /bin/

2. 命令使用

bash 复制代码
# 默认输出文件名
gif2map4 input.gif

# 指定输出文件名
gif2map4 input.gif output_20250711.mp4

三、mp4 两倍速

1. 配置环境

与步骤二类似

bash 复制代码
# 从第30秒开始,截取10秒的视频(保持原速度)
ffmpeg -i input.mp4 -ss 00:00:30 -t 10 -c:v copy -c:a copy output_cut.mp4

# 参数说明:
# -ss 开始时间(格式:HH:MM:SS或秒数)
# -t 持续时间
# -c:v copy 视频流直接复制(无损)
# -c:a copy 音频流直接复制

# 视频加速2倍(音频同步加速)
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output_2x.mp4
# setpts=0.5*PTS 视频速度2倍(值越小越快)
# atempo=2.0 音频速度2倍(范围0.5-2.0,超过需嵌套处理)

# 同时裁剪和加速
ffmpeg -i input.mp4 -ss 00:01:00 -t 20 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output_final.mp4
bash 复制代码
#!/bin/bash

# Check if input file is provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 <input.mp4> [output.mp4]"
    echo "Example:"
    echo "  $0 input.mp4              # Output: input_2x_YYYYMMDD_HHMMSS.mp4"
    echo "  $0 input.mp4 fast_video   # Output: fast_video.mp4"
    exit 1
fi

# Input file
INPUT_FILE="$1"
if [ ! -f "$INPUT_FILE" ]; then
    echo "Error: Input file '$INPUT_FILE' not found!"
    exit 1
fi

# Output filename
if [ $# -ge 2 ]; then
    # If second argument is provided, use it
    OUTPUT_FILE="$2"
    # Ensure output has .mp4 extension
    if [[ "$OUTPUT_FILE" != *.mp4 ]]; then
        OUTPUT_FILE="${OUTPUT_FILE}.mp4"
    fi
else
    # Otherwise generate timestamped filename
    TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
    INPUT_BASENAME=$(basename "$INPUT_FILE" .mp4)
    OUTPUT_FILE="${INPUT_BASENAME}_2x_${TIMESTAMP}.mp4"
fi

# Run the conversion command
ffmpeg -i "$INPUT_FILE" \
    -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" \
    -map "[v]" -map "[a]" \
    "$OUTPUT_FILE"

# Check if output was generated successfully
if [ -f "$OUTPUT_FILE" ]; then
    echo "Success! 2x speed video created:"
    echo "  - $OUTPUT_FILE"
else
    echo "Error: Failed to create output file!"
    exit 1
fi
相关推荐
聆风吟º5 小时前
无需 VNC / 公网 IP!用 Docker-Webtop+cpolar,在手机浏览器远程操控 Linux
linux·运维·docker
deng-c-f6 小时前
Linux C/C++ 学习日记(22):Reactor模式(二):实现简易的webserver(响应http请求)
linux·c语言·网络编程·reactor·http_server
BTU_YC6 小时前
CentOS 7 虚拟IP配置指南:使用传统network-scripts实现高可用
linux·tcp/ip·centos
陌路206 小时前
LINUX14 进程间的通信 - 管道
linux·网络
大聪明-PLUS6 小时前
从头开始为 ARM 创建 Ubuntu 映像
linux·嵌入式·arm·smarc
chenzhou__6 小时前
MYSQL学习笔记(个人)(第十五天)
linux·数据库·笔记·学习·mysql
序属秋秋秋7 小时前
《Linux系统编程之入门基础》【Linux基础 理论+命令】(上)
linux·运维·服务器·ubuntu·centos·命令模式
一张假钞12 小时前
Ubuntu SSH 免密码登陆
linux·ubuntu·ssh
Wang's Blog13 小时前
Linux小课堂: 文件操作警惕高危删除命令与深入文件链接机制
linux·运维·服务器
水月wwww14 小时前
操作系统——进程管理
linux·操作系统·vim·进程·进程调度