递归处理视频文件以腾出空间

递归处理视频文件以腾出空间

我的存储空间已经用完了,我想腾出一些空间。

我考虑过将所有文件转换为 mkv,并将高分辨率文件缩小为较低分辨率。

因此,要将 mp4 转换为 mkv,需要

ffmpeg -i input.mp4 -vcodec copy -acodec copy output.mkv

缩小规模

ffmpeg -i input.mp4 -vf scale=$w:$h <encoding-parameters> output.mp4

但我需要转换所有文件,而不是单个 mp4 文件,处理完输入文件后,我需要删除它,而且我只需要对高于给定分辨率的文件执行此操作。此外,我想保留宽高比,但我不知道这是否能做到这一点。

有一种工具叫视频饮食这完成了部分工作。

这就是我想做的

Recursively find every file under the current directory
If the file resolution has a height equal or greater to a given height.
  Convert the file to mkv if it's in another format avi, mp4, flv, etc
  Downscale to a lower resolution height, keeping the aspect ratio.

也许我也应该降低帧速率到 24 fps?

如果有更好的方法,我很乐意听听。

答案1

这是帮助您入门的脚本。您可能希望修改它以满足您的需求,如果要更改帧速率,请向 ffmpeg 添加选项,等等。

请注意,其中有一个条件检查文件是否已经是 .mkv,但我实际上并没有对它们进行不同的处理,因此该条件毫无意义。它之所以存在只是因为你的问题听起来好像你想对它们进行不同的处理,尽管我不确定为什么。FFMPEG 可以同时进行缩放和转换。

但是如果您确实想做一些不同的事情,请将条件为真时的 ffmpeg 行更改为其他内容。

#!/bin/bash

# set the resolution above which you want to downscale
maxwidth=1920
maxheight=1080

# set the resolution to downscale to; the width will be
# calculated to preserve aspect ratio
targetwidth=1280

# a counter for errors
errors=0

# recursively find video files; add their names to
# an array to process
mapfile -t filenames < <(find $(pwd) -type f -iregex '.*avi$\|.*mp4$\|.*m4v$\|.*flv\|.*mkv\|.*ogv\|.*webm$\|.*mov')

# loop through the array
for filename in "${filenames[@]}" ; do
    # get the resolution with ffprobe
    res="$(ffprobe -hide_banner -v error -select_streams v:0 -show_entries stream=width,height -print_format csv "$filename")"
    # strip "stream" off "res"
    res="${res#stream,}"
    # parse into width and height
    width="${res%,*}"
    height="${res#*,}"
    # compare to maxwidth/height to see if it should be
    # processed
    if [[ "$width" -ge "$maxwidth" ]] || [[ "$height" -ge "$maxheight" ]] ; then
        # determine the output name
        outputname="${filename%.*}-downscaled.mkv"
        # check if output already exists; if so, skip it
        if [[ -e "$outputname" ]] ; then
            continue
        fi
        # calculate targetheight, divide by 2 multiply by 2
        # to ensure it's even
        targetheight="$(( ( ( (targetwidth * height) / width) / 2) * 2 ))"
        # get file extension
        ext="${filename##*.}"
        # do something different with mkvs?
        # not sure why you would though
        if [[ "$ext" =~ '[Mm]{Kk][Vv]' ]] ; then
            ffmpeg -i "$filename" -vf scale="$targetwidth:$targetheight" "$outputname"
            exitcode="$?"
        else
            ffmpeg -i "$filename" -vf scale="$targetwidth:$targetheight" "$outputname"
            exitcode="$?"
        fi
        # if conversion was a success, delete original file
        if [[ "$exitcode" == 0 ]] && [[ -e "$outputname" ]] ; then
            rm "$filename"
        else
            echo
            echo "Error processing $filename" >&2
            echo "Not deleted!"
            let errors++
            echo
        fi
    fi
done

# report number of errors if there were any
if [[ "$errors" == 0 ]] ; then
    exit 0
fi
echo "There were $errors errors!" >&2
exit 1

这可能有点过头了,也可能有方法可以简化它,但这就是我要做的。

相关内容