2019-12-09

選擇mp4裡的幾段時間範圍,另存成一個mp4

最接近的KeyFrame時間

取得 1.mp4   1:23 (1分23秒)最接近的KeyFrame時間

ffprobe.exe   -v error  -select_streams v:0
    -show_entries "frame=best_effort_timestamp_time"
    -of  "csv=p=0"   -sexagesimal  
    -read_intervals   1:23%+#1    1.mp4

以上這四行是同一行,
要在cmd窗口執行。

如果要在bat執行,必須多加一個 %
改成 1:23%%+#1

選擇mp4裡的幾段時間範圍,另存成一個mp4

(以關鍵幀剪輯,所以時間不會很準確)

需要ffmpeg 和 ffprobe

執行方法:
    把mp4拖進bat圖示

    如果拖進之後沒反應,先點兩下打開bat,
    然後再把mp4拖進cmd窗口


檔名:
    支持unicode檔名(藉由8.3檔名來實現)

時間:
    從「起點」往前找「最近的KeyFrame」
    如果「起點」是0:0:0 ,可能會往後找

    「終點」必須大於「起點」

    如果終點沒有指定,就是指:起點開始的以後全部

會有錯誤訊息:
    Non-monotonous DTS in output stream 0:1; previous: 7850002, current: 7847497; 
    changing to 7850003. This may result in incorrect timestamps in the output file.
@echo off

set "aa=.;%path%"
for %%a in (ffmpeg.exe  ffprobe.exe) do (
    if "%%~$aa:a" == "" echo need %%a & pause & exit /b
)

::=====================
set mp4=

if /i "%~x1" == ".mp4"  set "mp4=%~1"
if "%mp4%" == "" echo drag mp4 to here & set /p mp4=

set "mp4=%mp4:"=%"

if not exist "%mp4%" echo "%mp4%" not exist & pause & exit /b

for %%a in ("%mp4%") do (
    set "mp4=%%~sa"
    set "outMp4=%%~na_out%%~xa"
)

if exist "%outMp4%"  echo "%outMp4%" exist & pause & exit /b

::=========================
set timeRange=
echo input  startTime endTime startTime endTime
echo for example:
echo 0:10  0:20   1:3:5  1:3:30
set /p timeRange=
if "%timeRange%" == "" echo no startTime exist & pause & exit /b

::===================
set txt=concat.txt

set ffCmd=ffprobe.exe -select_streams v:0
set ffCmd=%ffCmd%  -show_entries "frame=best_effort_timestamp_time"
set ffCmd=%ffCmd%  -of "csv=p=0" -sexagesimal  

cd.> %txt%
call :aa  %timeRange%

ffmpeg -n -safe 0 -f concat -i %txt%  -c copy "%outMp4%"
goto :eof

::=========================
:aa
if "%~1" == ""  goto :eof

set "mp4_txt=%mp4:\=/%"
set "mp4_txt=%mp4_txt:'='\''%"

for %%a in ("%mp4_txt%") do echo file  '%%~a'  >> %txt%

for /f %%a in ('%ffCmd%  -read_intervals %~1%%+#1   "%mp4%" ') do (
    echo inpoint  %%a  >> %txt%
)

if not "%~2" == "" echo outpoint  %~2  >> %txt%
shift & shift
goto aa

沒有留言:

張貼留言