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

发布于:2025-07-15 ⋅ 阅读:(18) ⋅ 点赞:(0)

一、工具安装

安装 ffmpeg 工具:

sudo apt install ffmpeg

二、gif 转mp4

1. 配置环境
核心指令:

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 文件,内容如下:


#!/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

添加到终端指令

sudo cp gif2map4 /bin/

2. 命令使用

# 默认输出文件名
gif2map4 input.gif

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

三、mp4 两倍速

1. 配置环境
与步骤二类似

# 从第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
#!/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

网站公告

今日签到

点亮在社区的每一天
去签到