iam building a little youtube downloader by using youtube-dl and php in the backend. When user submits a youtube video url, php is executing youtube-dl.exe and passing the video data directly to the client because i avoided it to download the video in the backend and than again to download it to the frontend.
Here the php code where iam passing the stream directly to the client:
header("Content-Disposition: attachment; filename=\"...\"" );
header("Content-Type: application/octet-stream");
passthru("$pathToYouTubedlExe -o - $youtube_video_url");
The video data which arrives at the client looks like that and its a string:
ftypmp42 isommp42 �moov lmvhd � �B� �B X � @ iods O��)� �trak \tkhd � �B 2 @ � h $edts elst 2 �mdia mdhd � �B < �U� -hdlr vide VideoHandler �minf vmhd $dinf dref url dstbl �stsd �avc1 �h H H �� .avcCB��� gB����� ��b� h�<� btrt + ]X *� stts C stss = @stsc stsz C + 7 / � � � 2 : � � � N � t � J � � � F � 0 # ? Q �
$ � � � � � � y � � 3 � � Y � � � 0 > ^ � � [ X � � � 3 ! % � y 6 3 $stco � z� �p kx �k �trak \tkhd � �B� �B � @ =mdia mdhd � �B� �B �D � U� Lhdlr soun IsoMedia File Produced by Google, 5-11-2011 �minf smhd $dinf dref url �stbl istsd Ymp4a �D 5esds ' @ Q � �� stts f 4stsc �stsz f + % # & # ! " ! 8 ! & 4 - # ! =
3 6 Q O � � " " % # " ! % 5 Q F F F ? 8 6 $stco b� � T6 �1 �B �mdat +e����ϔP �Ў�j�� �Dٸ���a &X���m� ����~�J�R��jf�NMB�T�&h�Yd!���䲭�Ԙ +c�Q�HOj�̑Rv��U�,���L��=~��xwU�1��Ć́\��H���k�Ы ���.�(�ثg�b��������?�����>��A�=� )freeIsoMedia File Produced By Google
Thats the data which arrives at the client. My question is how can i create a video file, like mp4 or webm or any other available extension for that particular video?
i tried it with a blob:
let response = await axios.get("http://localhost/youtube-dl/index.php",{
params: {
download_video:JSON.stringify(this.videoInfo)
}
},{ responseType:"application/octet-stream" });
const props = {type: "octet/stream"};
const props2 = {type: "video/mp4"}; / <<---- not working too
let file;
try {
file = new File([response.data],"test.mp4", props);
} catch (e) {
file = new Blob([response.data], props);
}
var fileURL = window.URL.createObjectURL(file);
let fileLink = document.createElement("a");
fileLink.href = fileURL;
fileLink.setAttribute("download","test.mp4")
document.body.appendChild(fileLink);
fileLink.click();
I tried it to create a json object with that video data in hope that maybe there is an object json structure with JSON.parse(response.data) but it does not work.