Skip to content

Instantly share code, notes, and snippets.

@ciencia
Created February 23, 2025 15:45
Show Gist options
  • Save ciencia/1e9398b580ec16906f9f10b32682bb3d to your computer and use it in GitHub Desktop.
Save ciencia/1e9398b580ec16906f9f10b32682bb3d to your computer and use it in GitHub Desktop.
patch MediaWiki extension TimedMediaHandler enable alpha transcodes REL1_43
diff --git a/includes/WebVideoTranscode/WebVideoTranscodeJob.php b/includes/WebVideoTranscode/WebVideoTranscodeJob.php
index 162be036..466f502b 100644
--- a/includes/WebVideoTranscode/WebVideoTranscodeJob.php
+++ b/includes/WebVideoTranscode/WebVideoTranscodeJob.php
@@ -19,6 +19,7 @@ use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\Shell\CommandFactory;
use MediaWiki\Shell\Shell;
+use MediaWiki\TimedMediaHandler\Handlers\WebMHandler\WebMHandler;
use MediaWiki\TimedMediaHandler\HLS\Segmenter;
use MediaWiki\TimedMediaHandler\TimedMediaHandler;
use MediaWiki\Title\Title;
@@ -560,6 +561,24 @@ class WebVideoTranscodeJob extends Job {
->params( $this->config->get( MainConfigNames::ShellboxShell ), 'scripts/' . $script );
}
+ /**
+ * Adds input codec information for more accurate input codec selection
+ */
+ private function getInputCodec() {
+ $file = $this->getFile();
+ $handler = $file->getHandler();
+ if ( $handler instanceof WebMHandler ) {
+ $streams = $handler->getStreamTypes( $file );
+ if ( in_array( 'VP9', $streams ) ) {
+ // In order to carry out vp9 alpha channel to transcodes,
+ // it's necessary to specify the input codec as vp9,
+ // otherwise the default vp8 gets selected
+ return ' -vcodec libvpx-vp9';
+ }
+ }
+ return '';
+ }
+
/**
* Utility helper for ffmpeg mapping
* @param array $options
@@ -585,6 +604,7 @@ class WebVideoTranscodeJob extends Job {
$extension = substr( $transcodeKey, strrpos( $transcodeKey, '.' ) + 1 );
// Set up all the video-related options
+ $optsEnv['TMH_INPUTCODEC'] = $this->getInputCodec();
if ( isset( $options['novideo'] ) ) {
$optsEnv['TMH_OPTS_VIDEO'] = '-vn';
} else {
@@ -987,7 +1007,8 @@ class WebVideoTranscodeJob extends Job {
// Force to 4:2:0 chroma subsampling. Others are supported in Theora
// and in VP9 profile 1, but Chrome and Edge don't grok them.
- $cmd .= ' -pix_fmt yuv420p';
+ // yuva420p allows alpha channel
+ $cmd .= ' -pix_fmt yuva420p';
// libvpx-specific constant quality or constrained quality
// note the range is different between VP8 and VP9
diff --git a/scripts/ffmpeg-encode.sh b/scripts/ffmpeg-encode.sh
index 94e95193..fbe53868 100644
--- a/scripts/ffmpeg-encode.sh
+++ b/scripts/ffmpeg-encode.sh
@@ -7,7 +7,7 @@ export TMH_FFMPEG_THREADS="${TMH_FFMPEG_THREADS:-2}"
# Safe options with no user input or validated user input
# These can be used unquoted.
# Video related
-export TMH_FFMPEG2_OPTS TMH_MOVFLAGS TMH_OPTS_VIDEO TMH_REMUX TMH_OPT_SPEED TMH_OPT_VIDEOCODEC
+export TMH_FFMPEG2_OPTS TMH_MOVFLAGS TMH_OPTS_VIDEO TMH_REMUX TMH_OPT_SPEED TMH_OPT_VIDEOCODEC TMH_INPUTCODEC
# Audio-related
export TMH_OPTS_AUDIO TMH_OPT_NOAUDIO
export TMH_OUTPUT_FILE
@@ -63,7 +63,9 @@ doFfmpegEncode() {
# Please note, the unquoted entries are unquoted on purpose here.
- "$TMH_FFMPEG_PATH" -nostdin -nostats -y -i original.video \
+ "$TMH_FFMPEG_PATH" -nostdin -nostats -y \
+ $TMH_INPUTCODEC \
+ -i original.video \
$TMH_OPTS_VIDEO \
$FFMPEG2_OPTS \
$AUDIO_OPTS \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment