我怎样才能将 .mp4 分成两个?

我怎样才能将 .mp4 分成两个?

我有一堆 .mp4 文件(无 DRM)。每个文件包含一个儿童电视节目的两集。我想简单地将文件分成两部分,而无需重新编码。最好的方法是什么?最好使用 GUI(因为我需要跳到每个文件的正确部分来找到两集之间的分隔符)。

谢谢,

答案1

我建议在媒体播放器中打开视频,找到要分割的时间。然后,您可以将 ffmpeg 与以下脚本一起使用。它不会重新编码视频。

#!/bin/bash

# Split Video Script
# Usage: script_name file_name split-point
# Example: split_video_script bugs_bunny.mp4 31:23
# Instructions:     
# 1. Type the name of your script (if it is already added to ~/bin and marked as executable). 
# 2. Type the file name including path to it if necessary. 
# 3. Type the time where you want to split the video. It goes in minutes:seconds

# Get length in seconds
length=$(echo "$2" | awk -F: '{print ($1 * 60) + $2}')

# Get filename without extension
fname="${1%.*}"

# First half
ffmpeg -i "${fname}.mp4" -c copy -t "$length" "${fname}1.mp4"

# Second half
ffmpeg -i "${fname}.mp4" -c copy -ss "$length" "${fname}2.mp4"

更新:我最近需要更新此脚本,因为后半部分存在问题。所以,现在我必须处理它的后半部分。您将添加特定于原始视频的参数。您可以使用mediainfoffprobeffmpeg -i来查找有关原始视频的所需信息。

#!/bin/bash

if [ -z "$1" ]; then
    echo "Usage: $0 file-name"
    exit 1
fi

read -p "Enter time to split video (hh:mm:ss.mmm) " split_time

ext="${1##*.}"
fname="${1%.*}"

# First half
ffmpeg -i "$1" -c copy -t "$split_time" -c:s copy "${fname}1.${ext}"

# Second half
ffmpeg -ss "$split_time" -i "$1" -c:v libx264 -crf 17 -preset veryfast -r 30 -s 1920x1080 -c:a aac -ac 2 -b:a 256k -ar 44100 -pix_fmt yuv420p -movflags faststart -c:s copy "${fname}2.${ext}"

答案2

我需要使用您的脚本将视频分割成三部分,而不是将 mp4 分割成两部分。这是我的脚本版本:

#!/bin/bash

NUM_OF_SPLITS=3

main() {
    # Requires: $ `brew install ffmpeg`

    # total length in seconds 
    total_length=$(ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$1")

    # Get filename without extension
    fname="${1%.*}"
    split_time=$(echo "$total_length"/"$NUM_OF_SPLITS" | bc -l)

    declare -a start_times
    declare -a end_times

    for((i=0; i < $num_of_splits; i++)); do
        local output_file="${fname}_"part$i".mp4"
        if [[ "$i" -eq 0 ]]; then
            local start_time=0
            local end_time="$split_time"

            ffmpeg -i "${fname}.mp4" -c copy -t "$end_time" "$output_file"
        else
            local start_time="${end_times[$i-1]}"
            local end_time=$(echo "$start_time "*" 2" | bc -l)

            # echo "Start time: $start_time"
            # echo "End time: $end_time"

            ffmpeg -ss "$start_time" -to "$end_time"  -i "${fname}.mp4" -c copy "$output_file"
        fi

        start_times+=($start_time)
        end_times+=("$end_time")
    done

}

# Split Video Script
# Example: split_video_script bugs_bunny.mp4
# Instructions:     
# 1. Type the name of your script (if it is already added to ~/bin and marked as executable). 
# 2. Type the file name including path to it if necessary. 

main "$1"

相关内容