Advertisement
Guest User

FF Fader

a guest
Dec 18th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 10.60 KB | None | 0 0
  1. @ECHO OFF
  2. REM FF Command Line Editor 1.1
  3. REM Open a command prompt to run ffmpeg/ffplay/ffprobe
  4. REM Copyright (C) 2020 Chaddo
  5.  
  6. TITLE FF Command Line Editor
  7.  
  8. cls
  9. setlocal enabledelayedexpansion
  10.  
  11. REM user input to get video file
  12. ECHO.
  13. ECHO Type in video file location
  14. set /p video=""
  15.  
  16.  
  17. REM === start the processing ====
  18.  
  19. REM les use ffprobe to get video length
  20. for /F "delims=" %%I in ('bin\ffprobe -v error -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 "%video%" 2^>^&1') do SET "vid=%%I"
  21.  
  22. REM lets strip anything that has a decimal
  23. FOR %%i IN ("%vid%") DO (
  24.     set /A duration=%%~ni
  25.     GOTO check_duration
  26. )
  27.  
  28. :check_duration
  29. IF %duration% lss 32 (
  30.     ECHO.
  31.     echo Video duration is: %duration%s
  32.     echo.
  33.     GOTO less_than_30  
  34. ) ELSE (
  35.     ECHO.
  36.     echo Video duration is: %duration%s
  37.     echo.
  38.     GOTO greater_than_30
  39. )
  40.  
  41. :less_than_30
  42. echo Segments must be between 4 and 10 seconds
  43. ECHO How many seconds per segment do you want to cut up this video?
  44. set /p segment_cut_time=""
  45. SET /A segment_cut_time=%segment_cut_time%
  46.  
  47. IF %segment_cut_time% lss 4 (
  48.     echo you have to select a number between 4 and 10
  49.     ECHO.
  50.     Goto :less_than_30
  51.    
  52. ) ELSE (
  53.  
  54.     IF %segment_cut_time% gtr 10 (
  55.         echo you have to select a number between 4 and 10
  56.         ECHO.
  57.         Goto :less_than_30
  58.     ) ELSE (
  59.         Goto :cut_times
  60.     )
  61. )
  62.  
  63. :greater_than_30
  64. ECHO How many seconds per segment do you want to cut up this video?
  65. set /p segment_cut_time=""
  66. SET /A segment_cut_time=%segment_cut_time%
  67. GOTO cut_times             
  68.  
  69. :cut_times
  70. ECHO.
  71. ECHO Video will be cut into %segment_cut_time% second segments
  72.  
  73. REM ===== need to set this frames per second====
  74.  
  75. SET /A fps=30
  76.  
  77. REM ====== Video Fade ===========
  78. SET /A segment_fade_duration=1
  79. SET /A segment_time=%segment_cut_time%+%segment_fade_duration%
  80. SET /A segment_fade_In=0
  81. SET /A segment_fade_out=%segment_cut_time%
  82.  
  83.  
  84. REM will start overall timer
  85. set start=%time%
  86.  
  87. REM ====  Editing starts here ============
  88.  
  89. echo.
  90. echo We will start by cutting the video into %segment_cut_time% second segments
  91. REM == FFMpeg ==>
  92. bin\ffmpeg -hide_banner -loglevel 0 -i "%video%" -c copy -map 0 -f segment -segment_time %segment_time% -reset_timestamps 1 -segment_format_options movflags=+faststart out%%03d.mp4
  93. echo.
  94. echo Cutting the video has finished...
  95.  
  96.  
  97. :: ==================  cut the the fade in - segement one =========
  98. echo.
  99. echo Lets cut the the first 2 seconds off each segment to create the fade in
  100. :: lets cut the 1st 2 seconds to create the fade in
  101. for %%a in (out*.mp4) DO bin\ffmpeg -hide_banner -loglevel 0 -t 2 -i "%%a" -c copy a_%%~na.mp4
  102.  
  103.  
  104. echo.
  105. echo Now lets create the fade in for those segments
  106. :: now lets add the fade in for audio and video
  107. for %%a in (a_out*.mp4) DO bin\ffmpeg -hide_banner -loglevel 0 -i "%%a" -vf "fade=t=in:st=0:d=1" -af "afade=t=in:st=0:d=1" -async 1 b_%%~na.mp4
  108.  
  109. :: creates b_a_out
  110. :: delete the pre fades
  111. del a_out*.mp4
  112.  
  113. :: ==================  Create the fade out - segement one =========
  114.  
  115. echo.
  116. echo Now lets create 2 seconds off each end of the file
  117.  
  118. :: middle section
  119. for %%a in (out*.mp4) DO bin\ffmpeg -hide_banner -loglevel 0 -ss 2 -i "%%a" -ss 4 -i "%%a" -c copy -map 1:0 -map 0 -shortest -f nut - | bin\ffmpeg -hide_banner -loglevel 0 -f nut -i - -map 0 -map -0:0 -c copy 2b_%%~na.mp4
  120. :: creates 2b_out
  121.  
  122.  
  123. :: ==================    Create the main content bewtween fades =========
  124.  
  125. echo.
  126. echo OK, now lets cut the the last 2 seconds off the end of each segment to create the fade out
  127.  
  128. :: lets cut the last 2 seconds to create the fade out
  129. for %%a in (out*.mp4) DO bin\ffmpeg -hide_banner -loglevel 0 -sseof -2 -i "%%a" -c copy c_%%~na.mp4
  130.  
  131.  
  132. echo.
  133. echo Now lets create the fade out for those segments
  134. :: now lets add the fade in for audio and video
  135. for %%a in (c_out*.mp4) DO bin\ffmpeg -hide_banner -loglevel 0 -i "%%a" -vf "fade=t=out:st=1:d=1" -af "afade=t=out:st=1:d=1" -async 1 2b_%%~na.mp4
  136.  
  137. :: creates 2b_c_out
  138. :: delete the pre fades
  139. del c_out*.mp4
  140.  
  141. :: ==================  Combine all the segments  =========
  142. :: create text file with fade ins =====
  143. for %%c in (b_a_out*mp4) do (
  144.     echo %%c >> list.tmp
  145. )
  146.  
  147. >fade_in.tmp (
  148. for /F "delims=;" %%F in (list.tmp) do (
  149.     for %%F in (^"%%F^") do (
  150.         for /f "tokens=2-5 delims=:., " %%a in (
  151.             'bin\ffmpeg -i "%%F" 2^>^&1 ^| find "Duration:"'
  152.         ) do (
  153.             set /a full=%%a * 60 * 60 + %%b * 60 + %%c
  154.         )      
  155.         echo %%~nxF^|!full!
  156.     )
  157. )
  158. )
  159. del list.tmp
  160.  
  161. :: =====  create text file with fade ins =====
  162. for %%c in (2b_out*mp4) do (
  163.     echo %%c >> list.tmp
  164. )
  165.  
  166. >middle.tmp (
  167. for /F "delims=;" %%F in (list.tmp) do (
  168.     for %%F in (^"%%F^") do (
  169.         for /f "tokens=2-5 delims=:., " %%a in (
  170.             'bin\ffmpeg -i "%%F" 2^>^&1 ^| find "Duration:"'
  171.         ) do (
  172.             set /a full=%%a * 60 * 60 + %%b * 60 + %%c
  173.         )
  174.         echo %%~nxF^|!full!
  175.     )
  176. )
  177. )
  178. del list.tmp
  179.  
  180.  
  181. >fades.tmp (
  182. 3< middle.tmp (for /F "delims=" %%a in (fade_in.tmp) do (
  183.    set /P line2=<&3
  184.    echo %%a^|!line2!
  185. ))
  186. )
  187. del middle.tmp
  188. del fade_in.tmp
  189.  
  190. ::create text file with fade outs
  191. for %%c in (2b_c_out*mp4) do (
  192.     echo %%c >> list.tmp
  193. )
  194.  
  195. >fade_out.tmp (
  196.  for /F "delims=;" %%F in (list.tmp) do (
  197.     for %%F in (^"%%F^") do (
  198.         for /f "tokens=2-5 delims=:., " %%a in (
  199.             'bin\ffmpeg -i "%%F" 2^>^&1 ^| find "Duration:"'
  200.         ) do (
  201.             set /a full=%%a * 60 * 60 + %%b * 60 + %%c
  202.         )  
  203.         echo %%~nxF^|!full!
  204.     )
  205. )
  206. )
  207. del list.tmp
  208.  
  209. :: combine the segments and fade in tmp files
  210. >concat.tmp (
  211. 3< fade_out.tmp (for /F "delims=" %%a in (fades.tmp) do (
  212.    set /P line2=<&3
  213.    echo %%a^|!line2!
  214. ))
  215. )
  216.  
  217. :: =====  read the fades file to make main content splits =====
  218. for /f "tokens=1-6 delims=|" %%a in (concat.tmp) do (
  219.  
  220.     IF %%d lss 2 (
  221.         bin\ffmpeg -hide_banner -loglevel 0 -i %%e -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1_%%~na.ts
  222.         bin\ffmpeg -hide_banner -loglevel 0 -i intermediate1_%%~na.ts -c copy -bsf:a aac_adtstoasc mid_2_q%%~na.mp4
  223.     ) ELSE (   
  224.         bin\ffmpeg -hide_banner -loglevel 0 -i %%a -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1_%%~na.ts
  225.         bin\ffmpeg -hide_banner -loglevel 0 -i %%c -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2_%%~na.ts
  226.         bin\ffmpeg -hide_banner -loglevel 0 -i %%e -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate3_%%~na.ts
  227.         bin\ffmpeg -hide_banner -loglevel 0 -i "concat:intermediate1_%%~na.ts|intermediate2_%%~na.ts|intermediate3_%%~na.ts" -c copy -bsf:a aac_adtstoasc mid_2_q%%~na.mp4
  228.     )
  229. )
  230.  
  231. ::Rename the files
  232. for /F "eol=| delims=" %%I in ('dir /A-D /B mid_2_qb_a_*mp4') do (
  233.     for /F "eol=| tokens=2 delims=t " %%J in ("%%~nI") do ren "%%I" "final_%%J%%~xI"
  234. )
  235.  
  236. ::  clean up all the temp files
  237. del b_a_out*.mp4
  238. del 2b_out*.mp4
  239. del 2b_c_out*.mp4
  240. del concat.tmp
  241. del fades.tmp
  242. del fade_out.tmp
  243. del *.ts
  244. del out*.mp4
  245.  
  246. REM == FFMpeg ==>
  247. for %%c in (final_*.mp4) do (
  248.     echo file %%c >> list.txt
  249. )
  250. bin\ffmpeg -hide_banner -loglevel 0 -f concat -safe 0 -i list.txt -c copy combined.mp4
  251.  
  252.  
  253. REM ========  End the ffmpeg timer ===============
  254. :ffmpeg_finished
  255. set end=%time%
  256.  
  257. set options="tokens=1-4 delims=:.,"
  258. for /f %options% %%a in ("%start%") do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
  259. for /f %options% %%a in ("%end%") do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 100
  260.  
  261. set /a hours=%end_h%-%start_h%
  262. set /a mins=%end_m%-%start_m%
  263. set /a secs=%end_s%-%start_s%
  264. set /a ms=%end_ms%-%start_ms%
  265. if %ms% lss 0 set /a secs = %secs% - 1 & set /a ms = 100%ms%
  266. if %secs% lss 0 set /a mins = %mins% - 1 & set /a secs = 60%secs%
  267. if %mins% lss 0 set /a hours = %hours% - 1 & set /a mins = 60%mins%
  268. if %hours% lss 0 set /a hours = 24%hours%
  269. if 1%ms% lss 100 set ms=0%ms%
  270.  
  271. :: Mission accomplished
  272. set /a totalsecs = %hours%*3600 + %mins%*60 + %secs%
  273. ECHO.
  274. ECHO The whole editing process took :
  275. ECHO %hours%:%mins%:%secs%.%ms% (%totalsecs%.%ms%s total)  
  276. ECHO.
  277. ECHO Now lets Preview it...
  278.  
  279. bin\ffplay -autoexit -hide_banner -loglevel 0 -i "combined.mp4" -x 640 -y 480  
  280. del list.txt
  281.  
  282. :delete_preview
  283. ECHO.
  284. SET choice=
  285. ECHO Do you want to delete the combined video you just previewed before exiting?
  286. SET /p choice=PLEASE NOTE:  This can't be redone  [Y/N]:  
  287. IF NOT '%choice%'=='' SET choice=%choice:~0,1%
  288. IF '%choice%'=='Y' GOTO do_delete_preview
  289. IF '%choice%'=='y' GOTO do_delete_preview
  290. IF '%choice%'=='N' GOTO change_vid_name
  291. IF '%choice%'=='n' GOTO change_vid_name
  292. IF '%choice%'=='' GOTO delete_preview_must_make_choice
  293. ECHO "%choice%" is not valid
  294.  
  295. :delete_preview_must_make_choice
  296. ECHO.
  297. ECHO You have to type Y or N to delete the video segments or not
  298. GOTO delete_preview
  299. ECHO.
  300.  
  301. GOTO delete_preview
  302.  
  303. :do_preview_segments
  304. REM delete the preview we made
  305. if NOT EXIST list.txt goto :out
  306. del list.txt
  307.  
  308. :do_delete_preview
  309. if NOT EXIST combined*.mp4 GOTO :change_vid_name
  310. del combined*.mp4 GOTO :delete_segments
  311.  
  312.  
  313. :change_vid_name
  314. ECHO.
  315. ECHO The current name of the video you previwed is : combined.mp4
  316. SET choice=
  317. ECHO Do you want to change the name from 'combined.mp4' before exiting?
  318. SET /p choice=PLEASE NOTE:  This can't be redone  [Y/N]:  
  319. IF NOT '%choice%'=='' SET choice=%choice:~0,1%
  320. IF '%choice%'=='Y' GOTO do_change
  321. IF '%choice%'=='y' GOTO do_change
  322. IF '%choice%'=='N' GOTO delete_segments
  323. IF '%choice%'=='n' GOTO delete_segments
  324. IF '%choice%'=='' GOTO change_must_make_choice
  325. ECHO "%choice%" is not valid
  326.  
  327. :change_must_make_choice
  328. ECHO.
  329. ECHO You have to type Y or N to change the name of the video or not
  330. GOTO change_vid_name
  331. ECHO.
  332.  
  333. GOTO change_vid_name
  334.  
  335. REM user input to get video file
  336. :do_change
  337. ECHO.
  338. ECHO Please type in new video name only, do not add extension.
  339. set /p new_name=""
  340.  
  341. bin\ffmpeg -hide_banner -loglevel 0 -i "combined.mp4" -c copy "%new_name%".mp4
  342. del "combined.mp4"
  343.  
  344. :delete_segments
  345. ECHO.
  346. SET choice=
  347. ECHO Do you want to delete segments in the preview video before exiting?
  348. SET /p choice=PLEASE NOTE:  This can't be redone  [Y/N]:
  349. IF NOT '%choice%'=='' SET choice=%choice:~0,1%
  350. IF '%choice%'=='Y' GOTO do_delete_segments
  351. IF '%choice%'=='y' GOTO do_delete_segments
  352. IF '%choice%'=='N' GOTO no
  353. IF '%choice%'=='n' GOTO no
  354. IF '%choice%'=='' GOTO delete_segments_must_make_choice
  355. ECHO "%choice%" is not valid
  356.  
  357. :delete_segments_must_make_choice
  358. ECHO.
  359. ECHO You have to type Y or N to delete the video segments or not
  360. GOTO delete_segments
  361. ECHO.
  362.  
  363. GOTO delete_segments
  364.  
  365. :do_delete_segments
  366. REM delete the last of the segments we made
  367. if NOT EXIST list.txt goto :out
  368. del list.txt
  369.  
  370. :out
  371. if NOT EXIST final_*.mp4 goto :cleanedup
  372. del final_*.mp4
  373. echo.
  374. echo cleaning up is now complete...
  375.  
  376. :no
  377. echo.
  378. echo Good bye now go and enjoy your edited video!!!
  379.  
  380. pause
  381. EXIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement