ffmpeg - Bitare に基づく可変 CRF

ffmpeg - Bitare に基づく可変 CRF

私は、ffmpeg を使用して、さまざまなストリーミング サイトからダウンロードしたビデオを HEVC にエンコードします。Windows では、バッチ ファイルを使用して、ディレクトリ内のすべてのファイルを変換します。

ffmpeg -n -i input.mp4 -c:v libx265 -crf 22 -c:a libopus -b:a 48k -vbr on -compression_level 10 -frame_duration 60 -application audio output.mkv

これらのビデオの全体的なビットレートは、300 kb/s から 1500 kb/s の間で変化します。私のテストでは、これらすべてのビデオに同じ CRF を使用したところ、高ビットレートのビデオの場合は出力ファイルが大きくなり、低ビットレートの場合は低品質のビデオが生成されました。そのため、同様のビットレートのビデオの CRF を手動で変更しました。バッチ ファイルを使用してこれを実現するにはどうすればよいでしょうか。たとえば、全体のビットレートが 950 を超える場合 = crf 26、全体のビットレートが 750 から 949 の場合 = crf 24、全体のビットレートが 500 から 749 の場合 = crf 22、全体のビットレートが 499 未満の場合 = crf 18 などです。

答え1

@echo off & color 0A & cls 

cd /d "%~dp0" && setlocal enabledelayedexpansion
set "_ffmpeg=F:\2020-SU\Q1569837\ffmpeg\bin\ffmpeg.exe"
set "_ffprobe=F:\2020-SU\Q1569837\ffmpeg\bin\ffprobe.exe"
set "_range_crf=99999-950-26,949-750-24,749-500-22,499-0-18"

for %%# in (*.mp4)do echo; && set /a "_c+=1+0" && for /f tokens^=2^,6^delims^=^,^  %%i in ('
2^>^&1 "!_ffprobe!" -show_entries stream^=bit_rate "%%~f#"^|%__APPDIR__%findstr.exe /e [0-9].kb/s
')do echo\Check File: "%%~nx#" ^| Bite Rate: %%~j kb/s && call :^) "!_range_crf!" "%%~j" "%%~n#" "%%~f#" 

echo\ & <con: rem./ && echo\Total Files: 000!_c! && %__APPDIR__%timeout.exe /t -1 & endlocal && goto=:EOF

:^) 
echo\ && for %%i in (%~1)do for /f "tokens=1-3delims=-" %%a in ('echo\%%~i
')do if %~2 leq %%~a if %~2 geq %%~b title<nul & title .:^| File Name:"..\%~nx4" ^| Bite Rate: %~2 kb/s ^|:. && (
       "!_ffmpeg!" -y -i "%~4" -hide_banner -v error -stats -c:v libx265 -crf %%~c -c:a libopus -b:a 48k -vbr on ^
          -compression_level 10 -frame_duration 60 -application audio "%~3.mkv" && exit /b 0 )
  • 後ろの整備士forifs
       %2       %%a         %2      %%b              %%c         
:: if 951 leq 99999 and if 951 GEQ  950 = True  crf = 26 then Exit /loop :label
:: if 951 leq   949 and if 951 GEQ  750         crf = 24 then      /loop :label 
:: if 951 leq   749 and if 951 GEQ  500         crf = 22 then      /loop :label 
:: if 951 leq   499 and if 951 GEQ    0         crf = 18 then      /loop :label 
::
:: if 800 leq   999 and if 800 GEQ  950 = False crf = 26 then Keep /loop :label
:: if 800 leq   949 and if 800 GEQ  750 = True  crf = 24 then Exit /loop :label
:: if 800 leq   749 and if 800 GEQ  500         crf = 22 then      /loop :label 
:: if 800 leq   499 and if 500 GEQ    0         crf = 18 then      /loop :label 
::
:: if 600 leq   999 and if 600 GEQ  950 = False crf = 26 then Keep /loop :label
:: if 600 leq   949 and if 600 GEQ  750 = False crf = 24 then Keep /loop :label
:: if 600 leq   749 and if 600 GEQ  500 = True  crf = 22 then Exit /loop :label 
:: if 600 leq   499 and if 600 GEQ    0         crf = 18 then      /loop :label 
::
:: if 501 leq   999 and if 501 GEQ  950 = False crf = 26 then Keep /loop :label
:: if 501 leq   949 and if 501 GEQ  750 = False crf = 24 then Keep /loop :label
:: if 501 leq   749 and if 501 GEQ  500 = True  crf = 22 then Exit /loop :label
:: if 501 leq   499 and if 501 GEQ    0         crf = 18 then      /loop :label 
:: 
:: if 498 leq   999 and if 498 GEQ  950 = False crf = 26 then Keep /loop :label
:: if 498 leq   949 and if 498 GEQ  750 = False crf = 24 then Keep /loop :label
:: if 498 leq   749 and if 498 GEQ  500 = False crf = 22 then Keep /loop :label
:: if 498 leq   499 and if 498 GEQ    0 = True  crf = 18 then Exit /loop :label

観測: 1がある2つのスペース^=^,^⟵⟶%%iで: delims^=^,^spacespace%%i

for %%# in (*.mp4)do for /f tokens^=2^,6^delims^=^,^spacespace%%i in (...


1.- 宿題: 以下の変数をシナリオに合わせて置き換え、bat フォルダーにも移動します。

set "_ffmpeg=F:\2020-SU\Q1569837\ffmpeg\bin\ffmpeg.exe"
set "_ffprobe=F:\2020-SU\Q1569837\ffmpeg\bin\ffprobe.exe"

cd /d "%~dp0"

rem :: if your *.pm4 files are not in the same directory 
rem :: as your bat file, use the full path to drive/folder 
rem :: Example for drive D: folder/subfolder \Media\MP4\Convert 

cd /d "D:\Midia\MP4\Convet"

2.このバッチは複数の を使用していますfor loop。これを機能させるには、Deleyed Expansion実行時に変数が更新/拡張された値を受け取るように を有効にする必要があります。

Setlocal EnableDelayedExpansion

3.カンマで区切られた変数を定義し、区切り文字の間にループif内で使用する値を含めますfor。これにより、最大値、最小値、および互換性のある crf を操作できるようになります。

set "_range_crf= max-min-crf,max-min-24,max-500-22,499-0-18"
set "_range_crf=99999-950-26,949-750-24,749-500-22,499-0-18"

4.シンプルなforループとリストを作成するためのループを使用します.mp4ファイルとさらにカウンター(set /a "_c+=1+0"実行終了時に合計を取得するには:

for %%# in (*.mp4)do echo; && set /a "_c+=1+0"
  • 観測: 2echo;行を区切って実行時の視覚化を容易にするためにのみ使用され、ループ内に区切り線が作成されます。

5.mp4ファイルのフルパス/名前( )を取得したループ変数( )を追加で使用し、このループを既に定義している( で説明)への入力として渡します。for /f1st/for/var==%%#%%~f#ffprobe項目5。)、そのコマンドで取得されるトークンと区切り文字。

for /f tokens^=2^,6^delims^=^,^  %%i in (ffmprobe ...  %%~f# ... 

6.ffprobeループ内で使用されるコマンドは次のとおりです。for /f

..\ffprobe.exe -show_entries stream=bit_rate "Google Chrome - Now Everywhere.mp4"

7。スイッチを使用してフィルタリングする出力をStdErrリダイレクトすることから始めますStdOutffprobefindstr/End of a lineregex数字([0-9])を文字列に連結し.kb/s、適切なスケープ処理を使用してforループ:

2^>^&1 "!_ffprobe!" -show_entries stream^=bit_rate "%%~f#" ^| "%__APPDIR__%findstr.exe" /e [0-9].kb/s

8.上記の拡張コマンドをエスケープなしで実行すると、結果は次のようになります。

2>&1 ..\ffprobe.exe -show_entries stream=bit_rate "Google Chrome - Now Everywhere.mkv" | "%__APPDIR__%findstr.exe" /e [0-9].kb/s

9.フィルターによって処理された上記のコマンドの出力はfindstr次のようになります。

再生時間: 00:01:00.08、開始: -0.007000、ビットレート: 350 kb/s

10.フィルターによって処理された上記のコマンドの出力はfindstr次のようになります。

再生時間: 00:01:00.08、開始: -0.007000、ビットレート: 350 kb/s

11.複数の区切り文字を使用すると、%%iそして%%j出力は00:01:00.08そして350: は最後のコマンド出力で、00:01:00.08そして350:

... for /f tokens^=2^,6^delims^=^,^space %%i in (...
Duration: 00:01:00.08, start: -0.007000, bitrate: 350 kb/s

ここに画像の説明を入力してください

12.

限界値が350包括的) の場合は、if作業部分でいくつかのオプションを使用する必要があります。

 if %%~j > Bit_Rate      ∕∕ the same:  if %%~j > 349  (349 exclusive) 
 if %%~j  Bit_Rate      ∕∕ the same:  if %%~j  350  (350 inclusive) 

 set "_bit_rate=349"
 if %%~j > %_bit_rate%   ∕∕ the same:  if %%~j > 349  (349 exclusive) 

 set "_bit_rate=350"
 if %%~j  %_bit_rate%   ∕∕ the same:  if %%~j  350  (350 inclusive) 


 if LSS - Less Than                if [integer or int(var)]  < [integer or int(var)]
 if GTR - Greater Than             if [integer or int(var)]  > [integer or int(var)]
 if LEQ - Less Than or Equals      if [integer or int(var)]   [integer or int(var)]
 if GEQ - Greater Than or Equals   if [integer or int(var)]   [integer or int(var)]

13.結果としてiftrueまたはfalse、そしてケースに応じてアクションを実行します。教育目的のため、現在のファイルをtrue場合:

 if %%~j GTR 349 (
      case true
      ffmpeg transcode file mp4
    ) else (
      case false
      skip this file .mp4
      save the full path name 
    )
  •  if %%~j gtr 349 2>&1 ("!_ffmpeg!" -y -i "%%~f#" -hide_banner -v error -stats -c:v libx265 -crf 22 ^
       -c:a libopus -b:a 48k -vbr on -compression_level 10 -frame_duration 60 -application audio "%%~n#.mkv"
  • 観測:2キャラクター:space^は、行末の改行のすぐそばにあり、実行時にコマンド インタープリターはそれを 1 行として扱い、適用された改行をエスケープします。

// 申し訳ありませんが、説明は明日に続きます...

関連情報