Application Scenarios
Online Course Editing:
- Legacy Courses: Batch update of intros and outros across varied formats (encoding, resolution, frame rate, audio format).
- New Course Recordings: Processing raw files, focusing on noise reduction, removal of blank segments, and adding standardized intros and outros.
- Rapid Slicing & Content Review: Based on scripts, for swift editing and review, crucial for content flagged for modification.
Requirements Breakdown
Legacy Courses:
- Objective: Replace (delete + add), standardize formats.
- Challenge: Inconsistent formats and intro/outro durations across batches, necessitating batch-by-batch processing.
New Courses:
- Objective: Audio enhancement, blank segment removal, intro/outro addition.
- Challenge: Automated detection of blank content.
Fast Slicing & Inspection:
- Objective: Swift inspection via speech-to-text + rapid editing.
Tool Selection
Intro/Outro Handling
Audio Batch Processing
空白内容处理
快速切片及检查
Speech-to-Text Solutions1:Capcut
Speech-to-Text Solutions2:FastWhisperGUI
Subtitle Guided Editing1:Autocut(命令行,不推荐)
Subtitle Guided Editing2:MediaToolkit(GUI,推荐)
FFmpeg
FFmpeg is an open-source audio and video processing toolkit that has been around for 22 years. It is capable of handling tasks such as encoding, decoding, format conversion, editing, merging, extracting, compressing, decompressing, filtering, and subtitle handling for multimedia files. Compatible with various platforms including Windows, Linux, and macOS.
FFmpeg GUI Applications
Fortunately, we’re not confined to solely using command lines with FFmpeg anymore, as there are now several open-source graphical user interface (GUI) alternatives. Here are some noteworthy ones:
LosslessCut
- Source Code: https://github.com/mifi/lossless-cut
- Specifically designed for video trimming without batch processing capabilities.
- Allows marking multiple segments within a video and exporting them losslessly and swiftly.
QuickCut
- Source Code: https://gitee.com/haujet/QuickCut
- Offers features like video compression, transcoding, reversing, concatenating clips, cutting based on subtitles, auto-subtitling, and automated editing.
- Not optimized for batch processing.
Format Factory
- Note: Not open-source, and it’s advised not to download the latest version due to unwanted background activities.
- However, older versions remain functional.
FFmpeg Batch AV Converter
- Source Code: https://github.com/eibol/ffmpeg_batch
- Enables batch trimming of video beginnings and endings based on duration, along with adding intro and outro sequences, although batch trimming might encounter issues.
Batch Processing of Intros and Outros
After testing numerous FFmpeg GUIs, many were found lacking in functionality, customization, or reliability, leaving no entirely satisfactory off-the-shelf software solution. Given FFmpeg’s command-line nature, however, batch processing can be achieved through scripting. Here are some pre-existing scripts and resources:
- [Bat File + FFmpeg] Batch Video Splitting, Format Conversion, and Merging
- Requires no additional runtime libraries.
- Link to the video tutorial.
- [Windows] Batch Removal of Video Intros and Outros (FFmpeg + Shell Script)
- Requires installing the Git Bash tool.
- Original Post.
- Implementing Large Video Segmentation, Watermarking, and Intro/Outro Merging Using FFMPEG + Python
- Requires installing Python.
- Article Link.
Ultimately, opting for the simplest approach led to choosing the Bat File + FFmpeg script solution. By referencing online resources and leveraging AI-generated code assistance, a tailored script was developed to fulfill the requirements.
Video Trimming Workflow
Challenges in video trimming stem from FFmpeg’s lack of reverse search capability, necessitating total video duration calculation for endpoint determination. Despite being straightforward, numerous GUIs falter when batch processing non-standard online course videos, potentially due to dynamic frame rates, audio inconsistencies, or GUI implementation issues, leading to errors or output videos with missing segments and audio-video sync problems. Ultimately, custom scripts proved most reliable for comprehensive video compatibility.
@echo off&title MP4 批量掐头去尾(运行版)
setlocal enabledelayedexpansion
echo 该脚本可以批量将目录内MP4文件,从头尾裁减固定时间。(如将多个视频,开头剪3s,结尾剪5s)
echo 如需批量指定视频的开始结束时间,请打开另一个脚本。(如指定多个视频,都从1分12秒开始,到5分27秒结束)
echo 如果您是从其他地方获取的该脚本,请先看一下b站视频,有详细介绍,谢谢。https://www.bilibili.com/video/BV1ic411N7ny/
echo 该脚本基于ffmpeg,请确保您已将ffmpeg添加到环境。
echo 时间作差部分依靠 @随风 @https://pokes.blog.csdn.net/
echo 获取视频文件时长借鉴 @亦良Cool @https://blog.csdn.net/annita2019/article/details/128747458
echo 作者虎啸ROAR b站:https://space.bilibili.com/19075528 个人网站:https://www.luotianyi.blue
rem 提示用户输入开头裁剪的时间,或者跳过
echo 若当前选项为0,可直接按回车
set /p sh=请输入开头切割的 小时部分(如开头无需切割,输入-1):
if "%sh%" == "-1" goto zs
if not "%sh%" == "-1" goto os
:zs
set st=00:00:00.00
echo 开头裁剪时长 %st%
goto end
exit
:os
rem 用户输入小时、分钟、秒和毫秒,设置开头裁剪时间
set sh=00
set /p sm=请输入开头切割的 分钟部分:
if "%sm%" == "" set sm=00
set /p ss=请输入开头切割的 秒部分:
if "%ss%" == "" set ss=00
set /p sms=请输入开头切割的 毫秒部分:
if "%sms%" == "" set sms=00
set st=%sh%:%sm%:%ss%.%sms%
echo 开头裁剪时长 %st%
goto end
exit
:end
rem 提示用户输入结尾裁剪的时间,或者跳过
set /p eh=请输入结尾切割的 小时部分(如结尾无需切割,输入-1):
if "%eh%" == "-1" ( goto ze) else ( goto oe)
exit
:ze
set et=00:00:00.00
echo 结尾裁剪时长 %et%
goto fin
exit
:oe
rem 用户输入小时、分钟、秒和毫秒,设置结尾裁剪时间
set eh=00
set /p em=请输入开头切割的 分钟部分:
if "%em%" == "" set em=00
set /p es=请输入开头切割的 秒部分:
if "%es%" == "" set es=00
set /p ems=请输入开头切割的 毫秒部分:
if "%ems%" == "" set ems=00
set et=%eh%:%em%:%es%.%ems%
echo 结尾裁剪时长 %et%
goto fin
exit
:fin
rem set st=0
rem set et=00:00:02.80
rem 创建结果目录
cd /d %~dp0
md result
rem 对每个MP4文件进行裁剪
for /f "delims=" %%i in ('dir /b /s /a-d *.mp4') do (
rem 获取视频持续时间
for /f "tokens=2 delims=, " %%a in ('ffmpeg -i "%%i" 2^>^&1 ^| find "Duration:"') do (
set str=%%a
)
rem 设置裁剪结束时间为输入的结束时间
set time=%et%:!str!
set t=!time!
call :time0 "!t!" "!time!" ok
echo 本视频开始时间为 %st% 结束时间为:!ok!
rem 执行裁剪操作
ffmpeg -ss %st% -to !ok! -accurate_seek -i "%%~si" -c:v copy -c:a copy "result\%%~ni.mp4"
)
pause
rem 将输入的时间转换为ffmpeg可识别的格式
:time0
@echo off&setlocal&set /a n=0
for /f "tokens=1-8 delims=.: " %%a in ("%~1:%~2") do (
set /a n+=10%%a%%100*360000+10%%b%%100*6000+10%%c%%100*100+10%%d%%100
set /a n-=10%%e%%100*360000+10%%f%%100*6000+10%%g%%100*100+10%%h%%100)
set /a s=n/360000,n=n%%360000,f=n/6000,n=n%%6000,m=n/100,n=n%%100
set "ok=%s%:%f%:%m%.%n%"
endlocal&set %~3=%ok:-=%
Video Concatenation Workflow
Video concatenation is easier theoretically, yet available references were scarce. As a programming novice, I utilized GPT-assisted learning to devise a failsafe concatenation strategy. FFmpeg offers two concatenation methods: direct concatenation for MPEG via concat protocol and universal concat demuxer, both supporting lossless and re-encoding. Testing revealed unpredictable outcomes when merging differently formatted videos.
The solution involved employing FFmpeg’s filter_complex for re-encoding (lossy), preceded by uniform encoding settings adjustment to ensure consistency. This, while time-consuming, guaranteed reliability. Uniformity across frame rate, resolution, aspect ratio (ensuring 1:1 to avoid mismatches), audio sample rate, and channel count was crucial.
rem 此为核心内容
for %a in ("*.mp4") do
ffmpeg -i ".\input\op.mp4" -i %a -i .\output\ed.mp4" -filter_complex "[0:v]fps=30,scale=1280:720,setsar=1[v0];[1:v]fps=30,scale=1280:720,setsar=1[v1];[2:v]fps=30,scale=1280:720,setsar=1[v2];[0:a]aformat=sample_rates=44100:channel_layouts=stereo[a0];[1:a]aformat=sample_rates=44100:channel_layouts=stereo[a1];[2:a]aformat=sample_rates=44100:channel_layouts=stereo[a2];[v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[vout][aout]" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 192k -ar 44100 -ac 2 "!output_file!"
rem 此为封装使用的文件源码
@echo off
setlocal enabledelayedexpansion
rem 设置输入文件夹和输出文件夹路径
set input_folder=input
set output_folder=output
rem 检查输出文件夹是否存在,如果不存在则创建
if not exist !output_folder! mkdir !output_folder!
rem 遍历当前目录的所有mp4文件
for %%a in (*.mp4) do (
set input_file=%%a
set output_file=!output_folder!\%%~na.mp4
rem 使用FFmpeg进行连接,调整编码参数
ffmpeg -i "!input_folder!\op.mp4" -i "!input_file!" -i "!input_folder!\ed.mp4" -filter_complex "[0:v]fps=30,scale=1280:720,setsar=1[v0];[1:v]fps=30,scale=1280:720,setsar=1[v1];[2:v]fps=30,scale=1280:720,setsar=1[v2];[0:a]aformat=sample_rates=44100:channel_layouts=stereo[a0];[1:a]aformat=sample_rates=44100:channel_layouts=stereo[a1];[2:a]aformat=sample_rates=44100:channel_layouts=stereo[a2];[v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[vout][aout]" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 192k -ar 44100 -ac 2 "!output_file!"
echo !output_file! created.
)
echo All files processed.
endlocal
rem 这个命令的解释如下:
rem 这个FFmpeg命令是用来合并视频文件的。具体来说,它首先会在当前目录下查找所有的mp4文件(使用通配符"*.mp4"来匹配),然后针对每一个文件执行以下操作:
rem 执行ffmpeg命令,将当前目录下的op.mp4(在input目录下)作为第一个输入文件,将当前遍历到的mp4文件作为第二个输入文件,将当前目录下的ed.mp4(在input目录下)作为第三个输入文件。
使用-filter_complex进行过滤操作。三个文件分别表示为[0:0], [0:1];[1:0], [1:1];[2:0], [2:1],然后使用concat过滤器将它们拼接起来。n=3表示有三个输入流,v=1表示合并视频流,a=1表示合并音频流,合并后的视频流和音频流分别为[v]和[a]。
rem 使用-map选项将拼接后的视频流和音频流分别映射到输出文件中。
rem 设置视频编码器为libx264,使用预设,设置视频码率为800000,帧率为30。
rem 设置音频编码器为aac,音频码率为192k,音频采样率为44100,声道数为2。
rem 将合并后的视频和频流输出至output目录下,输出的文件名与原文件相同但扩展名为.mp4。
Re-encoding parameters were tailored to specific needs.
-c:v h264_nvenc -preset fast -b:v 800000 -r 30
-c:v h264_nvenc -preset fast -crf 23 -c:a copy
-c:v h264_qsv -global_quality:v 22
-c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 256k
-c:v libx264 -preset veryfast -b:v 800000 -r 30 -c:a aac -b:a 256k
-c:v libx264 -preset veryfast -b:v 800000 -vf "scale=1280:-1" -c:a copy
Handling Silent Segments
Application Scenario: Removing pauses, waits, or stutters from long videos.
Implementation Principle: Audio amplitude detection identifies silent parts, with XML export for further review, restoration, or edits.
Solution 1: Automated Editing Tool A mature software product, iteratively refined for comprehensive functionality.
Solution 2: FFmpeg Silence Detection
ffmpeg -i input.mp4 -af silencedetect=noise=-30dB:d=0.5 -vn -sn -dn -f null /dev/null
# -af with silencedetect measures volume levels; compatible with specific audio formats.
# noise sets the quiet threshold (default -60dB); d defines silence duration (default 2 seconds).
# -vn, -sn, -dn ignore non-audio streams for efficiency.
# On Windows, replace /dev/null with NUL.
# For multi-channel audio, specify per-channel detection: ffmpeg -i input.mp3 -af "silencedetect=mono=1"
# For multi-channel audio, it is possible to specify detection for each channel individually.
ffmpeg -i input.mp3 -af "silencedetect=mono=1" -vn -sn -dn -f null /dev/null
Rapid Slicing & Inspection
Details are omitted, but key points include:
- Transcribed text serves primarily for timestamp reference.
- Subtitle-based video trimming yields mixed results.
- Preferred approach: use Losslesscut for precise, lossless cuts after subtitle-guided timing.