撸一个批处理优化视频网页传输

2021-10-07 – 3:13 下午 --- 36,486 次阅读

哦,原来视频流式传输需要这么优化:

Optimizing MP4 Video for Fast Streaming
Tools to fix MP4 videos so players can start playback instantly (without downloading the whole file)
Understanding the M.P.E.G-4 moov atom: Pseudo-Streaming in Mp4

检测视频文件是否已经为流式传输优化:

mp4box -info A.mp4 2>&1 1>NUL | find "File suitable for progressive download (moov before mdat)"
#或者
AtomicParsley input.mp4 -T | sed -n 2p | grep -q "moov" && echo "yup" || echo "nope"

那么就撸个批处理干,支持 mp4/mkv/mov/m4v 格式的优化。

#!/bin/bash

[ -n "$1" ] && {
    src_folder="$1"
} || {
    echo "input you source folder location."
    exit
}

src_empty=$(ls -A "$src_folder")

[ -n "$src_empty" ] && {
    for file_a in ${src_folder}/*
    do
        in_full_filename=$(basename "$file_a")
        in_filename="${filename%.*}"
        in_extension="${file_a##*.}"
        out_full_filename="${in_filename}_.${in_extension}"

        echo "src=$in_full_filename ndst=$out_full_filename"

        case $in_extension in
            mp4|mov|m4v)
                ffmpeg -i "${src_folder}/${in_full_filename}" -movflags faststart -acodec copy -vcodec copy -copyts "${src_folder}/$out_full_filename"
                ;;
            mkv)
                ffmpeg -i "${src_folder}/${in_full_filename}" -c copy -reserve_index_space 100k -copyts "${src_folder}/$out_full_filename"
                ;;
            *)
                echo "${in_extension} file not support faster start."
                ;;
        esac

        [ $? -eq 0 ] && {
            rm -f "${src_folder}/${in_full_filename}"
            mv -f "${src_folder}/$out_full_filename" "${src_folder}/${in_full_filename}"
            echo "-----------${in_filename} now faster web loading------------"
        } || {
            echo "-----------${in_filename} faster process failed------------"
        }
    done
} || {
    echo "Source folder is empty."
}

Windows 版本,丢到要处理的视频目录下:

for /r %%F in (*.mp4) do (
    ffmpeg.exe  -i "%%F" -movflags faststart -acodec copy -vcodec copy -copyts "%%~dpnF"_.mp4
    if not errorlevel 1 if exist "%%~dpnF.mp4" (
		del /q "%%F"
		move "%%~dpnF"_.mp4 "%%F"
	)
)

for /r %%F in (*.mov) do (
    ffmpeg.exe  -i "%%F" -movflags faststart -acodec copy -vcodec copy -copyts "%%~dpnF"_.mov
    if not errorlevel 1 if exist "%%~dpnF.mov" (
		del /q "%%F"
		move "%%~dpnF"_.mov "%%F"
	)
)

for /r %%F in (*.mkv) do (
    ffmpeg.exe  -i "%%F" -c copy -reserve_index_space 100k -copyts "%%~dpnF"_.mkv
    if not errorlevel 1 if exist "%%~dpnF.mkv" (
		del /q "%%F"
		move "%%~dpnF"_.mkv "%%F"
	)
)

for /r %%F in (*.wmv) do (
    ffmpeg.exe  -i "%%F" -movflags faststart -copyts "%%~dpnF".mp4
    if not errorlevel 1 if exist "%%~dpnF.wmv" (
		del /q "%%F"
	)
)

for /r %%F in (*.mpg) do (
    ffmpeg.exe  -i "%%F" -movflags faststart -copyts "%%~dpnF".mp4
    if not errorlevel 1 if exist "%%~dpnF.mpg" (
		del /q "%%F"
	)
)

for /r %%F in (*.flv) do (
    ffmpeg.exe  -i "%%F" -movflags faststart -copyts "%%~dpnF".mp4
    if not errorlevel 1 if exist "%%~dpnF.flv" (
		del /q "%%F"
	)
)

for /r %%F in (*.rmvb) do (
    ffmpeg.exe  -i "%%F" -movflags faststart -copyts "%%~dpnF".mp4
    if not errorlevel 1 if exist "%%~dpnF.rmvb" (
		del /q "%%F"
	)
)

for /r %%F in (*.rm) do (
    ffmpeg.exe  -i "%%F" -movflags faststart -copyts "%%~dpnF".mp4
    if not errorlevel 1 if exist "%%~dpnF.rm" (
		del /q "%%F"
	)
)
点击显示引用框
引用本文,复制粘贴...

点击可把本文加入多个网络分享站点

您必须 登录 才能发表评论.