Skip to content

Instantly share code, notes, and snippets.

@codepo8
Last active August 10, 2024 11:16
Show Gist options
  • Save codepo8/ff453b203ebc4ef78840161aa1a55684 to your computer and use it in GitHub Desktop.
Save codepo8/ff453b203ebc4ef78840161aa1a55684 to your computer and use it in GitHub Desktop.
<?php
function convertfilename($filename) {
$filename = str_replace([ "?", "[", "]", "/", "\\", "=", "<", ">",
":", ";", ",", "'", '"', "&", "$", "#",
"*", "(", ")", "|", "~" ], "", $filename);
$filename = preg_replace("/[\s-]+/", "-", $filename);
$filename = trim($filename, ".-_");
return ucwords($filename);
}
function convert($id,$start,$end,$uid){
$id = preg_replace("/\?share.*/","",$id);
$uid = str_replace('==','',$uid);
$tool = 'ffmpeg -hide_banner -loglevel error ';
$yt = "https://www.youtube.com/watch?v=";
$cmd = "yt-dlp --print filename '$yt$id'";
$name = exec($cmd);
$name = preg_replace("/\..*$/",".mp4",$name);
$dirname = convertfilename(str_replace('.mp4','',$name)).'---'.$uid;
$name = convertfilename($name);
mkdir($dirname);
chdir($dirname);
echo "\n\n*** Getting ".$name." *** \n\n";
$cmd = "yt-dlp --output '$name' --merge-output-format mp4 '$yt$id'";
$x = exec($cmd);
$cmd = "ffprobe -v error -select_streams v -show_entries stream=width,height -of csv=p=0:s=x '$name'";
$size = exec($cmd);
echo '*'.$size.'*'."\n";
$small = strstr($size, '1280') ? '-small' : '';
$intro = "../Intro$small.mp4";
$outro = "../Outro$small.mp4";
echo "\n\n*** Getting clip from $start to $end *** \n\n";
$chunkname = preg_replace("/\.mp4$/","-clip.mp4",$name);
$cmd = $tool."-ss $start -to $end -i '$name' -c copy '$chunkname'";
exec($cmd);
echo "\n\n*** Adding intro and outro *** \n\n";
$introname = str_replace('-clip.mp4','---'.$uid.'.mp4',$chunkname);
$cmd = $tool.'-i "'.$intro.'" -i "'.$chunkname.'" -i "'.$outro.'" -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" "'.$introname.'"';
exec($cmd);
unlink($chunkname);
echo "*** Extracting audio from ".$name." *** \n";
$cmd = $tool."-i '$introname' -vn -acodec copy '".str_replace(".mp4",".m4a",$introname)."'";
exec($cmd);
chdir('..');
}
$timings = file_get_contents('timings.tsv');
$lines = explode("\n",$timings);
// $lines = array_slice($lines,16,1);
foreach($lines as $i=>$l){
echo ($i+1)." of ".sizeof($lines)."\n";
$chunks = explode("\t",$l);
$id = $chunks[6];
$start = '0'.$chunks[1];
$end = '0'.$chunks[2];
$uid = $chunks[7];
echo $chunks[3]."\n";
echo ($id.'-'.$start.'-'.$end.'-'.$uid)."\n";
convert($id,$start,$end,$uid);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment