TNTwise / rife-ncnn-vulkan

RIFE, Real-Time Intermediate Flow Estimation for Video Frame Interpolation implemented with ncnn library
MIT License
34 stars 4 forks source link

Num-frame usage for fractional interpolation factors (ex: 24 to 64fps) #7

Closed bilodeausp closed 3 months ago

bilodeausp commented 3 months ago

Good day,

If I set num-frame with the desired target frame rate value (ex: 60fps), the output_frames folder will contain only 60 frames when I run these two lines:

$targetFps = 60 rife-ncnn-vulkan -i input_frames -o output_frames -g 0,1 -m rife-v4.16-lite -n $targetFps

Hence, I ffprobe'd the input file for the total frame count, then set num-frame by multiplying its value by (60 / 24) = 2.5, no luck.

Is it possible to use the num-frame parameter in order to interpolate from 24 to 60fps directly? If so, can you give a usage example?

Thank you kindly.

bilodeausp commented 3 months ago

Finally got it using the below

. "C:\Code\PS\Utilities\Get-TimeSpanPretty.ps1"

Clear-Host

$folderPath = "C:\Temp\" $extension = ".mp4" $targetFps = "60" $processStartTime = Get-Date -Format G

$files = Get-ChildItem $folderPath -Filter *.mp4

foreach ($file in $files) {

$fileName = $file.Basename
$source = $folderPath + $fileName + $extension

$f = ffprobe -v quiet -select_streams v:0 -count_packets -show_entries stream=r_frame_rate,nb_read_packets -of default=nw=1:nk=1 $source

$r = $f[0]
$pos = $r.IndexOf("/")
$frLeft = $r.Substring(0, $pos)
$frRight = $r.Substring($pos + 1)
$frameRate = [math]::Round([int]$frLeft / [int]$frRight)

$c = $f[1]
$factor = $targetFps / $frameRate
$numFrames = [math]::Round($factor * [int]$c)

$startTime = Get-Date -Format G 

Write-Host "`n---------------------------------------------------------------------------------------`n" 
Write-Host "`n[$startTime] Processing file $source" -ForegroundColor Green

Set-Location "C:\Folder\"

if (Test-Path input_frames) {
    Remove-Item input_frames -Recurse -Force -Confirm:$false
}
if (Test-Path output_frames) {
    Remove-Item output_frames -Recurse -Force -Confirm:$false
} 

mkdir input_frames
mkdir output_frames

$suffix = " (Rifed " + $targetFps + "fps)"
$destination = $folderPath + $fileName + $suffix + $extension

$currentTime = Get-Date -Format G    
Write-Host "[$currentTime] Frame rate is" $frameRate"fps, will convert to" $targetFps"fps" -ForegroundColor Yellow

ffmpeg -i $source -vn -acodec copy audio.m4a -y -v quiet
$currentTime = Get-Date -Format G    
Write-Host "[$currentTime] Audio extracted" -ForegroundColor Yellow

ffmpeg -i $source input_frames/frame_%08d.png -v quiet
$currentTime = Get-Date -Format G    
Write-Host "[$currentTime] Video extracted" -ForegroundColor Yellow

Write-Host "[$currentTime] Starting interpolation" -ForegroundColor Yellow

rife-ncnn-vulkan -i input_frames -o output_frames -g 0,1 -m rife-v4.16-lite -n $numFrames    
$currentTime = Get-Date -Format G    
Write-Host "[$currentTime] Interpolation completed, assembling output file" -ForegroundColor Yellow

ffmpeg -framerate $targetFps -i output_frames/%08d.png -i audio.m4a -c:a copy -crf 15 -c:v libx264 -pix_fmt yuv420p $destination -v quiet -stats -y
$endTime = Get-Date -Format G
$duration = New-TimeSpan -Start $startTime -End $endTime | Get-TimeSpanPretty

Write-Host "[$endTime] Output file ready [$destination]" -ForegroundColor Yellow
Write-Host "`nFile interpolation duration: $duration `n" -ForegroundColor Magenta
Write-Host "`n---------------------------------------------------------------------------------------`n" 

}

Write-Host "Script started at:" $processStartTime -ForegroundColor Green Write-Host "Script completion time:" $endTime -ForegroundColor Green $duration = New-TimeSpan -Start $processStartTime -End $endTime | Get-TimeSpanPretty Write-Host "Script duration:" $duration -ForegroundColor Green