
我已經用盡了儲存空間,我想騰出一些空間。
我曾想過將所有檔案轉換為 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
這可能有點矯枉過正,可能有一些方法可以簡化它,但這就是我要做的。