Skip to content

Instantly share code, notes, and snippets.

@LB--
Last active March 31, 2018 04:09
Show Gist options
  • Save LB--/389a4e51f4e1b3ca15f9f5f17f0e2aeb to your computer and use it in GitHub Desktop.
Save LB--/389a4e51f4e1b3ca15f9f5f17f0e2aeb to your computer and use it in GitHub Desktop.
A script I use to format my OBS Studio video recordings for upload to Twitch and YouTube. Requires ffmpeg.
cmake_policy(VERSION 3.10)
if(NOT DEFINED SOURCE)
message(FATAL_ERROR "-DSOURCE=")
endif()
if(NOT DEFINED DEST)
set(DEST ".")
endif()
if(NOT DEFINED ENCODER)
if(NOT DEFINED PRESET)
set(PRESET veryfast)
endif()
if(NOT DEFINED CRF)
set(CRF 20)
endif()
set(_tune)
if(DEFINED TUNE)
set(_tune -tune "${TUNE}")
endif()
set(ENCODER libx264 -preset "${PRESET}" -crf "${CRF}" ${_tune})
endif()
if(NOT DEFINED MAPS)
set(MAPS 0:0 0:1 0:2 0:3)
endif()
set(_maps)
foreach(_map ${MAPS})
set(_maps ${_maps} -map "${_map}")
endforeach()
set(_fps)
if(DEFINED FPS)
set(_fps -r "${FPS}")
endif()
function(_handle_directory _path _output)
# string(REPLACE "\\" "/" _path "${_path}")
# string(FIND "${_path}" "/" _last_slash REVERSE)
# math(EXPR _dir_start "(0${_last_slash})+1")
# string(SUBSTRING "${_path}" "${_dir_start}" "-1" _dir)
get_filename_component(_dir "${_path}" NAME)
file(MAKE_DIRECTORY "${_output}/")
set(_outputs "")
set(_output_files "")
if(NOT EXISTS "${_output}/${_dir}.mkv" AND NOT NOMKV)
list(APPEND _outputs "${_output}/${_dir}.mkv")
list(APPEND _output_files "${_output}/${_dir}.mkv")
endif()
if(NOT EXISTS "${_output}/${_dir}.mp4" AND NOT NOMP4)
list(APPEND _outputs "[movflags=+faststart]${_output}/${_dir}.mp4")
list(APPEND _output_files "${_output}/${_dir}.mp4")
endif()
if("${_dir}" MATCHES "duskers-daily-([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])" AND NOT EXISTS "${_output}/${_dir}.png")
set(_date "${CMAKE_MATCH_1}")
set(_base "C:/Users/LB/Pictures/sorted/duskers/Duskers_Launch_SS_01.png")
set(_fontfile "C:/Users/LB/OneDrive/GitHub/ChessPlusPlus/src/client/data/font/OpenSans/OpenSans-ExtraBold.ttf")
set(_font "Open Sans")
set(_fontcolor "white")
set(_fontsize "135")
set(_drawtext "")
set(_drawtext "${_drawtext}drawtext=font='${_font}':fontfile='${_fontfile}':fontcolor='${_fontcolor}':fontsize='${_fontsize}':x=(w-text_w)/2:y=text_h/5:text='Daily',")
set(_drawtext "${_drawtext}drawtext=font='${_font}':fontfile='${_fontfile}':fontcolor='${_fontcolor}':fontsize='${_fontsize}':x=(w-text_w)/2:y=(h/2)-(text_h/3):text='${_date}'")
execute_process(
COMMAND
ffmpeg -y
-i "${_base}"
-vf "${_drawtext}" "${_output}/${_dir}.png"
-vf "${_drawtext}" -q:v 1 "${_output}/${_dir}.jpg"
WORKING_DIRECTORY "${_output}"
)
endif()
list(LENGTH _outputs _num_missing_formats)
if("${_num_missing_formats}" GREATER 0)
string(REPLACE ";" "|" _outputs_piped "${_outputs}")
string(REPLACE "'" "\\'" _outputs_piped "${_outputs_piped}")
set(_ffmpeg_command
ffmpeg -y
-f concat -safe 0 -i concat.txt ${_fps}
${_maps}
-c:v ${ENCODER}
-c:a copy
-f tee -flags +global_header
"${_outputs_piped}"
)
file(WRITE "${_path}/concat.txt" "#\n")
file(GLOB _videos
LIST_DIRECTORIES false
"${_path}/*.mp4"
"${_path}/*.mkv"
)
list(SORT _videos)
foreach(_v ${_videos})
get_filename_component(_v "${_v}" NAME)
file(APPEND "${_path}/concat.txt" "file '${_v}'\n")
endforeach()
if(_videos)
message("\n\n${_ffmpeg_command}")
execute_process(
COMMAND ${_ffmpeg_command}
WORKING_DIRECTORY "${_path}"
RESULT_VARIABLE _ffmpeg_result
)
if(NOT "${_ffmpeg_result}" EQUAL 0)
foreach(_o ${_output_files})
if(EXISTS "${_o}")
file(REMOVE "${_o}")
endif()
endforeach()
endif()
endif()
list(LENGTH _videos _num_videos)
if("${_num_videos}" LESS 2)
file(REMOVE "${_path}/concat.txt")
endif()
else()
message(STATUS "Already processed: ${_output}/${_dir}")
endif()
file(GLOB _dirs
LIST_DIRECTORIES true
"${_path}/*"
)
foreach(_d ${_dirs})
if(NOT IS_DIRECTORY "${_d}")
continue()
endif()
get_filename_component(_dir "${_path}" NAME)
_handle_directory("${_d}" "${_output}/${_dir}")
endforeach()
endfunction()
get_filename_component(_source "${SOURCE}" ABSOLUTE)
get_filename_component(_dest "${DEST}" ABSOLUTE)
_handle_directory("${_source}" "${_dest}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment