
Recherche avancée
Médias (1)
-
The Great Big Beautiful Tomorrow
28 octobre 2011, par
Mis à jour : Octobre 2011
Langue : English
Type : Texte
Autres articles (30)
-
Personnaliser en ajoutant son logo, sa bannière ou son image de fond
5 septembre 2013, parCertains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;
-
Publier sur MédiaSpip
13 juin 2013Puis-je poster des contenus à partir d’une tablette Ipad ?
Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir -
Submit bugs and patches
13 avril 2011Unfortunately a software is never perfect.
If you think you have found a bug, report it using our ticket system. Please to help us to fix it by providing the following information : the browser you are using, including the exact version as precise an explanation as possible of the problem if possible, the steps taken resulting in the problem a link to the site / page in question
If you think you have solved the bug, fill in a ticket and attach to it a corrective patch.
You may also (...)
Sur d’autres sites (7497)
-
Use FFMPEG to convert MP4/AAC to MP3 without audio time getting shortened
10 janvier 2023, par Esenkay07I've been struggling with extracting the audio from a couple of mp4 files I have. I have tried using all of the different answers posted previously on Stack Overflow, but each of them results in an mp3 file that's shorter than the original mp4.


If anyone has any insights as to what I'm doing wrong, that'd be greatly appreciated, or at the very least why it's not this simple to convert from mp4 to mp3.


I have tried copying the codec and using variable bitrate. The input video is 1:24:56, but the output audio is 1:24:46.


Command :
ffmpeg -i sample.mp4 -c:v copy -q:a 0 sample.mp3


Output :


$ffmpeg -i sample.mp4 -c:v copy -q:a 0 sample.mp3

ffmpeg version 2023-01-09-git-13d04e30d7-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
 built with gcc 12.1.0 (Rev2, Built by MSYS2 project)
 configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
 libavutil 57. 43.100 / 57. 43.100
 libavcodec 59. 56.100 / 59. 56.100
 libavformat 59. 34.102 / 59. 34.102
 libavdevice 59. 8.101 / 59. 8.101
 libavfilter 8. 53.100 / 8. 53.100
 libswscale 6. 8.112 / 6. 8.112
 libswresample 4. 9.100 / 4. 9.100
 libpostproc 56. 7.100 / 56. 7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'sample.mp4':
 Metadata:
 major_brand : isom
 minor_version : 512
 compatible_brands: isomiso2avc1mp41
 encoder : Lavf57.83.100
 Duration: 01:24:56.57, start: 0.000000, bitrate: 612 kb/s
 Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, smpte170m, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 480 kb/s, 29.94 fps, 30 tbr, 90k tbn (default)
 Metadata:
 handler_name : VideoHandler
 vendor_id : [0][0][0][0]
 Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
 Metadata:
 handler_name : SoundHandler
 vendor_id : [0][0][0][0]
Stream mapping:
 Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, mp3, to 'sample.mp3':
 Metadata:
 major_brand : isom
 minor_version : 512
 compatible_brands: isomiso2avc1mp41
 TSSE : Lavf59.34.102
 Stream #0:0(und): Audio: mp3, 44100 Hz, stereo, fltp (default)
 Metadata:
 handler_name : SoundHandler
 vendor_id : [0][0][0][0]
 encoder : Lavc59.56.100 libmp3lame
size= 88336kB time=01:24:56.54 bitrate= 142.0kbits/s speed= 172x
video:0kB audio:88336kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000394%



-
Using node-media-server and FFMPEG, Transmuxing ends when rtmp is publishing with no errors
8 juillet 2023, par Sina KHI've configured and used node-media-server library on 2 of my test servers and it works great, but when I'm trying to make it work on production, it creates directories for different qualities, but does not generate .ts and .m3u8 for them. It only creates original hls outputs.
My logs show that instantly after
[rtmp publish] Handle video.
and[rtmp publish] Handle audio.
files, it receives[rtmp publish] Close stream. id=Y8KK9U3D streamPath=/live/15_1280 streamId=1
and[rtmp play] Close stream. id=7MC9Q65N streamPath=/live/15_1280 streamId=1

Please note that everything is the same on my test and prod servers, and with no known cause, it fails on some of my servers and works on some other ones. I've tried both deploying dockerized and not dockerized versions, with different node versions.
FFMPEG version is also the same on all systems.

My configs :


const config = {
 rtmp: {
 port: parseInt(process.env.STREAM_RTMP_PORT || '8082'),
 chunk_size: parseInt(process.env.STREAM_CHUNK_SIZE || '60000'),
 gop_cache: true,
 ping: 60,
 ping_timeout: 30
 },
 http: {
 mediaroot: process.env.FILE_PATH + '/media',
 port: parseInt(process.env.STREAM_HTTP_PORT || '8081'),
 allow_origin: '*'
 },
 auth: process.env.STREAM_SECRET?.length ? {
 api: true,
 play: false,
 publish: true,
 secret: process.env.STREAM_SECRET,
 api_user: process.env.STREAM_API_AUTH_USER,
 api_pass: process.env.STREAM_API_AUTH_PASS,
 } : undefined,
 trans: {
 ffmpeg: process.env.FFMPEG_PATH || '',
 tasks: [
 {
 app: 'live',

 hls: true,
 hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]',
 hlsKeep: true, // to prevent hls file delete after end the stream

 // dash: true,
 // dashFlags: '[f=dash:window_size=3:extra_window_size=5]',
 // dashKeep: true, // to prevent dash file delete after end the stream

 mp4: true,
 mp4Flags: '[movflags=frag_keyframe+empty_moov]',
 }
 ]
 },
 fission: {
 ffmpeg: process.env.FFMPEG_PATH || '',
 tasks: [
 {
 rule: "live/*",
 model: [
 {
 ab: "128k",
 vb: "1500k",
 vs: "720x1280",
 vf: "30",
 },
 {
 ab: "64k",
 vb: "1000k",
 vs: "480x854",
 vf: "24",
 },
 {
 ab: "32k",
 vb: "600k",
 vs: "360x640",
 vf: "20",
 },
 ]
 },
 ]
 }
}



and logs :


[NodeEvent on preConnect] id=OKRRCRT1 args={"app":"live","type":"nonprivate","supportsGoAway":true,"flashVer":"FMLE/3.0 (compatible; FMSc/1.0)","swfUrl":"rtmp://IP_PORT_HERE/live","tcUrl":"rtmp://IP_PORT_HERE/live"}
7/5/2023 16:13:28 963 [INFO] [rtmp connect] id=OKRRCRT1 ip=MY_IP_HERE app=live args={"app":"live","type":"nonprivate","supportsGoAway":true,"flashVer":"FMLE/3.0 (compatible; FMSc/1.0)","swfUrl":"rtmp://IP_PORT_HERE/live","tcUrl":"rtmp://IP_PORT_HERE/live"}
[NodeEvent on postConnect] id=OKRRCRT1 args={"app":"live","type":"nonprivate","supportsGoAway":true,"flashVer":"FMLE/3.0 (compatible; FMSc/1.0)","swfUrl":"rtmp://IP_PORT_HERE/live","tcUrl":"rtmp://IP_PORT_HERE/live"}
[NodeEvent on prePublish] id=OKRRCRT1 StreamPath=/live/15 args={"sign":"CORRECT_SIGN_HERE__REMOVED_TO_SHARE_IT_WITH_YOU"}
7/5/2023 16:13:28 963 [INFO] [rtmp publish] New stream. id=OKRRCRT1 streamPath=/live/15 streamId=1
[NodeEvent on postPublish] id=OKRRCRT1 StreamPath=/live/15 args={"sign":"CORRECT_SIGN_HERE__REMOVED_TO_SHARE_IT_WITH_YOU"}
7/5/2023 16:13:28 963 [INFO] [Transmuxing MP4] /live/15 to /home/data/stream_files/media/live/15/2023-07-05-16-13-28.mp4
7/5/2023 16:13:28 963 [INFO] [Transmuxing HLS] /live/15 to /home/data/stream_files/media/live/15/index.m3u8
[NodeEvent on preConnect] id=CPRQV6U6 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:28 963 [INFO] [rtmp connect] id=CPRQV6U6 ip=::ffff:127.0.0.1 app=live args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on postConnect] id=CPRQV6U6 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on preConnect] id=FTAZ3SW8 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:28 963 [INFO] [rtmp connect] id=FTAZ3SW8 ip=::ffff:127.0.0.1 app=live args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on postConnect] id=FTAZ3SW8 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on prePlay] id=CPRQV6U6 StreamPath=/live/15 args={}
[NodeEvent on postPlay] id=CPRQV6U6 StreamPath=/live/15 args={}
7/5/2023 16:13:28 963 [INFO] [rtmp play] Join stream. id=CPRQV6U6 streamPath=/live/15 streamId=1 
[NodeEvent on prePlay] id=FTAZ3SW8 StreamPath=/live/15 args={}
[NodeEvent on postPlay] id=FTAZ3SW8 StreamPath=/live/15 args={}
7/5/2023 16:13:28 963 [INFO] [rtmp play] Join stream. id=FTAZ3SW8 streamPath=/live/15 streamId=1 
7/5/2023 16:13:29 963 [INFO] [rtmp publish] Handle audio. id=OKRRCRT1 streamPath=/live/15 sound_format=10 sound_type=2 sound_size=1 sound_rate=3 codec_name=AAC 48000 2ch
7/5/2023 16:13:29 963 [INFO] [rtmp publish] Handle video. id=OKRRCRT1 streamPath=/live/15 frame_type=1 codec_id=7 codec_name=H264 1920x1080
[NodeEvent on preConnect] id=ISVLTK71 args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
7/5/2023 16:13:30 963 [INFO] [rtmp connect] id=ISVLTK71 ip=::ffff:127.0.0.1 app=live args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
[NodeEvent on postConnect] id=ISVLTK71 args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
[NodeEvent on prePublish] id=ISVLTK71 StreamPath=/live/15_1280 args={}
7/5/2023 16:13:30 963 [INFO] [rtmp publish] New stream. id=ISVLTK71 streamPath=/live/15_1280 streamId=1
[NodeEvent on postPublish] id=ISVLTK71 StreamPath=/live/15_1280 args={}
7/5/2023 16:13:30 963 [INFO] [Transmuxing MP4] /live/15_1280 to /home/data/stream_files/media/live/15_1280/2023-07-05-16-13-30.mp4
7/5/2023 16:13:30 963 [INFO] [Transmuxing HLS] /live/15_1280 to /home/data/stream_files/media/live/15_1280/index.m3u8
[NodeEvent on preConnect] id=YNOFQB7P args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
7/5/2023 16:13:31 963 [INFO] [rtmp connect] id=YNOFQB7P ip=::ffff:127.0.0.1 app=live args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
[NodeEvent on postConnect] id=YNOFQB7P args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
[NodeEvent on preConnect] id=OT8T2OPP args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:31 963 [INFO] [rtmp connect] id=OT8T2OPP ip=::ffff:127.0.0.1 app=live args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on postConnect] id=OT8T2OPP args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on prePublish] id=YNOFQB7P StreamPath=/live/15_854 args={}
7/5/2023 16:13:31 963 [INFO] [rtmp publish] New stream. id=YNOFQB7P streamPath=/live/15_854 streamId=1
[NodeEvent on postPublish] id=YNOFQB7P StreamPath=/live/15_854 args={}
7/5/2023 16:13:31 963 [INFO] [Transmuxing MP4] /live/15_854 to /home/data/stream_files/media/live/15_854/2023-07-05-16-13-31.mp4
7/5/2023 16:13:31 963 [INFO] [Transmuxing HLS] /live/15_854 to /home/data/stream_files/media/live/15_854/index.m3u8
[NodeEvent on prePlay] id=OT8T2OPP StreamPath=/live/15_1280 args={}
[NodeEvent on postPlay] id=OT8T2OPP StreamPath=/live/15_1280 args={}
7/5/2023 16:13:31 963 [INFO] [rtmp play] Join stream. id=OT8T2OPP streamPath=/live/15_1280 streamId=1 
[NodeEvent on preConnect] id=62KCI105 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:31 963 [INFO] [rtmp connect] id=62KCI105 ip=::ffff:127.0.0.1 app=live args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on postConnect] id=62KCI105 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on preConnect] id=9QAHATOC args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
7/5/2023 16:13:31 963 [INFO] [rtmp connect] id=9QAHATOC ip=::ffff:127.0.0.1 app=live args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
[NodeEvent on postConnect] id=9QAHATOC args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
[NodeEvent on prePlay] id=62KCI105 StreamPath=/live/15_854 args={}
[NodeEvent on postPlay] id=62KCI105 StreamPath=/live/15_854 args={}
7/5/2023 16:13:31 963 [INFO] [rtmp play] Join stream. id=62KCI105 streamPath=/live/15_854 streamId=1 
[NodeEvent on prePublish] id=9QAHATOC StreamPath=/live/15_640 args={}
7/5/2023 16:13:31 963 [INFO] [rtmp publish] New stream. id=9QAHATOC streamPath=/live/15_640 streamId=1
[NodeEvent on postPublish] id=9QAHATOC StreamPath=/live/15_640 args={}
7/5/2023 16:13:31 963 [INFO] [Transmuxing MP4] /live/15_640 to /home/data/stream_files/media/live/15_640/2023-07-05-16-13-31.mp4
7/5/2023 16:13:31 963 [INFO] [Transmuxing HLS] /live/15_640 to /home/data/stream_files/media/live/15_640/index.m3u8
[NodeEvent on preConnect] id=V308RJRW args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:31 963 [INFO] [rtmp connect] id=V308RJRW ip=::ffff:127.0.0.1 app=live args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on postConnect] id=V308RJRW args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on prePlay] id=V308RJRW StreamPath=/live/15_640 args={}
[NodeEvent on postPlay] id=V308RJRW StreamPath=/live/15_640 args={}
7/5/2023 16:13:31 963 [INFO] [rtmp play] Join stream. id=V308RJRW streamPath=/live/15_640 streamId=1 
7/5/2023 16:13:32 963 [INFO] [rtmp publish] Handle video. id=ISVLTK71 streamPath=/live/15_1280 frame_type=1 codec_id=7 codec_name=H264 720x1280
7/5/2023 16:13:32 963 [INFO] [rtmp publish] Handle audio. id=ISVLTK71 streamPath=/live/15_1280 sound_format=10 sound_type=2 sound_size=1 sound_rate=3 codec_name=AAC 48000 2ch
7/5/2023 16:13:32 963 [INFO] [rtmp publish] Close stream. id=ISVLTK71 streamPath=/live/15_1280 streamId=1
[NodeEvent on donePublish] id=ISVLTK71 StreamPath=/live/15_1280 args={}
7/5/2023 16:13:32 963 [INFO] [rtmp play] Close stream. id=OT8T2OPP streamPath=/live/15_1280 streamId=1
7/5/2023 16:13:32 963 [INFO] [rtmp disconnect] id=OT8T2OPP
[NodeEvent on doneConnect] id=OT8T2OPP args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:32 963 [INFO] [rtmp publish] Handle video. id=YNOFQB7P streamPath=/live/15_854 frame_type=1 codec_id=7 codec_name=H264 480x854
7/5/2023 16:13:32 963 [INFO] [rtmp publish] Handle audio. id=YNOFQB7P streamPath=/live/15_854 sound_format=10 sound_type=2 sound_size=1 sound_rate=3 codec_name=AAC 48000 2ch
7/5/2023 16:13:32 963 [INFO] [rtmp publish] Close stream. id=YNOFQB7P streamPath=/live/15_854 streamId=1
[NodeEvent on donePublish] id=YNOFQB7P StreamPath=/live/15_854 args={}
7/5/2023 16:13:32 963 [INFO] [rtmp play] Close stream. id=62KCI105 streamPath=/live/15_854 streamId=1
7/5/2023 16:13:32 963 [INFO] [rtmp disconnect] id=62KCI105
[NodeEvent on doneConnect] id=62KCI105 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:32 963 [INFO] [rtmp publish] Close stream. id=9QAHATOC streamPath=/live/15_640 streamId=1
[NodeEvent on donePublish] id=9QAHATOC StreamPath=/live/15_640 args={}
7/5/2023 16:13:32 963 [INFO] [rtmp play] Close stream. id=V308RJRW streamPath=/live/15_640 streamId=1
7/5/2023 16:13:32 963 [INFO] [rtmp disconnect] id=V308RJRW
[NodeEvent on doneConnect] id=V308RJRW args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
[NodeEvent on donePlay] id=CPRQV6U6 StreamPath=/live/15 args={}
7/5/2023 16:13:32 963 [INFO] [rtmp play] Close stream. id=CPRQV6U6 streamPath=/live/15 streamId=1
7/5/2023 16:13:32 963 [INFO] [rtmp disconnect] id=CPRQV6U6
[NodeEvent on doneConnect] id=CPRQV6U6 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:32 963 [INFO] [Transmuxing end] /live/15_1280
7/5/2023 16:13:32 963 [INFO] [rtmp disconnect] id=ISVLTK71
[NodeEvent on doneConnect] id=ISVLTK71 args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
7/5/2023 16:13:32 963 [INFO] [Transmuxing end] /live/15_640
7/5/2023 16:13:32 963 [INFO] [Transmuxing end] /live/15_854
7/5/2023 16:13:32 963 [INFO] [rtmp disconnect] id=9QAHATOC
[NodeEvent on doneConnect] id=9QAHATOC args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
7/5/2023 16:13:32 963 [INFO] [rtmp disconnect] id=YNOFQB7P
[NodeEvent on doneConnect] id=YNOFQB7P args={"app":"live","type":"nonprivate","flashVer":"FMLE/3.0 (compatible; Lavf59.27.100)","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live"}
7/5/2023 16:13:32 963 [INFO] [Fission end] /live/15
7/5/2023 16:13:46 963 [INFO] [rtmp publish] Close stream. id=OKRRCRT1 streamPath=/live/15 streamId=1
[NodeEvent on donePublish] id=OKRRCRT1 StreamPath=/live/15 args={"sign":"CORRECT_SIGN_HERE__REMOVED_TO_SHARE_IT_WITH_YOU"}
7/5/2023 16:13:46 963 [INFO] [rtmp disconnect] id=OKRRCRT1
[NodeEvent on doneConnect] id=OKRRCRT1 args={"app":"live","type":"nonprivate","supportsGoAway":true,"flashVer":"FMLE/3.0 (compatible; FMSc/1.0)","swfUrl":"rtmp://IP_PORT_HERE/live","tcUrl":"rtmp://IP_PORT_HERE/live"}
7/5/2023 16:13:46 963 [INFO] [rtmp play] Close stream. id=FTAZ3SW8 streamPath=/live/15 streamId=1
7/5/2023 16:13:46 963 [INFO] [rtmp disconnect] id=FTAZ3SW8
[NodeEvent on doneConnect] id=FTAZ3SW8 args={"app":"live","flashVer":"LNX 9,0,124,2","tcUrl":"rtmp://127.0.0.1:PORT_HERE/live","fpad":false,"capabilities":15,"audioCodecs":4071,"videoCodecs":252,"videoFunction":1}
7/5/2023 16:13:46 963 [INFO] [Transmuxing end] /live/15



-
Video encoding task not working with Django Celery Redis FFMPEG and GraphQL
18 juin 2023, par phanioI'm having a hard time trying to understand how is this FFMPEG encoding works while using Django, Celery, Redis, GraphQL and Docker too.


I have this video / courses platform project and want I'm trying to do using FFMPEG, Celery and Redis is to create different video resolutions so I can display them the way Youtube does inside the videoplayer ( the videoplayer is handled in frontend by Nextjs and Apollo Client ), now on the backend I've just learned that in order to use properly the FFMPEG to resize the oridinal video size, I need to use Celery and Redis to perform asyncronus tasks. I've found a few older posts here on stackoverflow and google, but is not quite enough info for someone who is using the ffmpeg and clery and redis for the first time ( I've started already step by step and created that example that adds two numbers together with celery, that works well ). Now I'm not sure what is wrong with my code, because first of all I'm not really sure where should I trigger the task from, I mean from which file, because at the end of the task I want to send the data through api using GrapQL Strawberry.


This is what I've tried by now :


So first things first my project structure looks like this


- backend #root directory
 --- backend
 -- __init__.py
 -- celery.py
 -- settings.py
 -- urls.py
 etc..

 --- static
 -- videos

 --- video
 -- models.py
 -- schema.py
 -- tasks.py
 -- types.py
 etc..

 --- .env

 --- db.sqlite3

 --- docker-compose.yml

 --- Dockerfile

 --- manage.py

 --- requirements.txt



here is my settings.py file :


from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

DEBUG = True

ALLOWED_HOSTS=["localhost", "0.0.0.0", "127.0.0.1"]

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# Application definition

INSTALLED_APPS = [
 "corsheaders",
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',

 "strawberry.django",
 "video"
]

etc...

STATIC_URL = '/static/'
MEDIA_URL = '/videos/'

STATICFILES_DIRS = [
 BASE_DIR / 'static',
 # BASE_DIR / 'frontend/build/static',
]

MEDIA_ROOT = BASE_DIR / 'static/videos'

STATIC_ROOT = BASE_DIR / 'staticfiles'

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

CORS_ALLOW_ALL_ORIGINS = True


CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'

# REDIS CACHE
CACHES = {
 "default": {
 "BACKEND": "django_redis.cache.RedisCache",
 "LOCATION": f"redis://127.0.0.1:6379/1",
 "OPTIONS": {
 "CLIENT_CLASS": "django_redis.client.DefaultClient",
 },
 }
}

# Docker
CELERY_BROKER_URL = os.environ.get("CELERY_BROKER", "redis://redis:6379/0")
CELERY_RESULT_BACKEND = os.environ.get("CELERY_BROKER", "redis://redis:6379/0")



This is my main urls.py file :


from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from django.urls.conf import include
from strawberry.django.views import GraphQLView

from video.schema import schema

urlpatterns = [
 path('admin/', admin.site.urls),
 path("graphql", GraphQLView.as_view(schema=schema)),
]

if settings.DEBUG:
 urlpatterns += static(settings.MEDIA_URL,
 document_root=settings.MEDIA_ROOT)
 urlpatterns += static(settings.STATIC_URL,
 document_root=settings.STATIC_ROOT)



This is my celery.py file :


from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')

backend = Celery('backend')

backend.config_from_object('django.conf:settings', namespace="CELERY")

backend.autodiscover_tasks()

@backend.task(bind=True)
def debug_task(self):
 print('Request: {0!r}'.format(self.request))



This is my init.py file :


from .celery import backend as celery_backend

__all__ = ('celery_backend',)



This is my Dockerfile :


FROM python:3
ENV PYTHONUNBUFFERED=1

WORKDIR /usr/src/backend

RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get install -y ffmpeg

COPY requirements.txt ./
RUN pip install -r requirements.txt



This is my docker-compose.yml file :


version: "3.8"

services:
 django:
 build: .
 container_name: django
 command: python manage.py runserver 0.0.0.0:8000
 volumes:
 - .:/usr/src/backend/
 ports:
 - "8000:8000"
 environment:
 - DEBUG=1
 - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
 - CELERY_BROKER=redis://redis:6379/0
 - CELERY_BACKEND=redis://redis:6379/0
 depends_on:
 - pgdb
 - redis

 celery:
 build: .
 command: celery -A backend worker -l INFO
 volumes:
 - .:/usr/src/backend
 depends_on:
 - django
 - redis

 pgdb:
 image: postgres
 container_name: pgdb
 environment:
 - POSTGRES_DB=postgres
 - POSTGRES_USER=postgres
 - POSTGRES_PASSWORD=postgres
 volumes:
 - pgdata:/var/lib/postgresql/data/

 redis:
 image: "redis:alpine"

volumes:
 pgdata:



And now inside my video app folder :


My models.py file :


- 

- here I've created separated fields for all resolution sizes, from video_file_2k to video_file_144, I was thinking that maybe after the process of the encoding this will populate those fields..




from django.db import models
from django.urls import reverse


class Video(models.Model):
 video_id = models.AutoField(primary_key=True, editable=False)
 slug = models.SlugField(max_length=255)
 title = models.CharField(max_length=150, blank=True, null=True)
 description = models.TextField(blank=True, null=True)
 video_file = models.FileField(null=False, blank=False)
 video_file_2k = models.FileField(null=True, blank=True)
 video_file_fullhd = models.FileField(null=True, blank=True)
 video_file_hd = models.FileField(null=True, blank=True)
 video_file_480 = models.FileField(null=True, blank=True)
 video_file_360 = models.FileField(null=True, blank=True)
 video_file_240 = models.FileField(null=True, blank=True)
 video_file_144 = models.FileField(null=True, blank=True)
 category = models.CharField(max_length=64, blank=False, null=False)
 created_at = models.DateTimeField(
 ("Created at"), auto_now_add=True, editable=False)
 updated_at = models.DateTimeField(("Updated at"), auto_now=True)

 class Meta:
 ordering = ("-created_at",)
 verbose_name = ("Video")
 verbose_name_plural = ("Videos")

 def get_absolute_url(self):
 return reverse("store:video_detail", args=[self.slug])

 def __str__(self):
 return self.title



This is my schema.py file :


import strawberry
from strawberry.file_uploads import Upload
from typing import List
from .types import VideoType
from .models import Video
from .tasks import task_video_encoding_1080p, task_video_encoding_720p


@strawberry.type
class Query:
 @strawberry.field
 def videos(self, category: str = None) -> List[VideoType]:
 if category:
 videos = Video.objects.filter(category=category)
 return videos
 return Video.objects.all()

 @strawberry.field
 def video(self, slug: str) -> VideoType:
 if slug == slug:
 video = Video.objects.get(slug=slug)
 return video

 @strawberry.field
 def video_by_id(self, video_id: int) -> VideoType:
 if video_id == video_id:
 video = Video.objects.get(pk=video_id)

 # Here I've tried to trigger my tasks, when I visited 0.0.0.0:8000/graphql url
 # and I was querying for a video by it's id , then I've got the error from celery 
 task_video_encoding_1080p.delay(video_id)
 task_video_encoding_720p.delay(video_id)

 return video


@strawberry.type
class Mutation:
 @strawberry.field
 def create_video(self, slug: str, title: str, description: str, video_file: Upload, video_file_2k: str, video_file_fullhd: str, video_file_hd: str, video_file_480: str, video_file_360: str, video_file_240: str, video_file_144: str, category: str) -> VideoType:

 video = Video(slug=slug, title=title, description=description,
 video_file=video_file, video_file_2k=video_file_2k, video_file_fullhd=video_file_fullhd, video_file_hd=video_file_hd, video_file_480=video_file_480, video_file_360=video_file_360, video_file_240=video_file_240, video_file_144=video_file_144,category=category)
 
 video.save()
 return video

 @strawberry.field
 def update_video(self, video_id: int, slug: str, title: str, description: str, video_file: str, category: str) -> VideoType:
 video = Video.objects.get(video_id=video_id)
 video.slug = slug
 video.title = title
 video.description = description
 video.video_file = video_file
 video.category = category
 video.save()
 return video

 @strawberry.field
 def delete_video(self, video_id: int) -> bool:
 video = Video.objects.get(video_id=video_id)
 video.delete
 return True


schema = strawberry.Schema(query=Query, mutation=Mutation)



This is my types.py file ( strawberry graphql related ) :


import strawberry

from .models import Video


@strawberry.django.type(Video)
class VideoType:
 video_id: int
 slug: str
 title: str
 description: str
 video_file: str
 video_file_2k: str
 video_file_fullhd: str
 video_file_hd: str
 video_file_480: str
 video_file_360: str
 video_file_240: str
 video_file_144: str
 category: str



And this is my tasks.py file :


from __future__ import absolute_import, unicode_literals
import os, subprocess
from django.conf import settings
from django.core.exceptions import ValidationError
from celery import shared_task
from celery.utils.log import get_task_logger
from .models import Video
FFMPEG_PATH = os.environ["IMAGEIO_FFMPEG_EXE"] = "/opt/homebrew/Cellar/ffmpeg/6.0/bin/ffmpeg"

logger = get_task_logger(__name__)


# CELERY TASKS
@shared_task
def add(x,y):
 return x + y


@shared_task
def task_video_encoding_720p(video_id):
 logger.info('Video Processing started')
 try:
 video = Video.objects.get(video_id=video_id)
 input_file_path = video.video_file.path
 input_file_url = video.video_file.url
 input_file_name = video.video_file.name

 # get the filename (without extension)
 filename = os.path.basename(input_file_url)

 # path to the new file, change it according to where you want to put it
 output_file_name = os.path.join('videos', 'mp4', '{}.mp4'.format(filename))
 output_file_path = os.path.join(settings.MEDIA_ROOT, output_file_name)

 # 2-pass encoding
 for i in range(1):
 new_video_720p = subprocess.call([FFMPEG_PATH, '-i', input_file_path, '-s', '1280x720', '-vcodec', 'mpeg4', '-acodec', 'libvo_aacenc', '-b', '10000k', '-pass', i, '-r', '30', output_file_path])
 # new_video_720p = subprocess.call([FFMPEG_PATH, '-i', input_file_path, '-s', '{}x{}'.format(height * 16/9, height), '-vcodec', 'mpeg4', '-acodec', 'libvo_aacenc', '-b', '10000k', '-pass', i, '-r', '30', output_file_path])

 if new_video_720p == 0:
 # save the new file in the database
 # video.video_file_hd.name = output_file_name
 video.save(update_fields=['video_file_hd'])
 logger.info('Video Processing Finished')
 return video

 else:
 logger.info('Proceesing Failed.') # Just for now

 except:
 raise ValidationError('Something went wrong')


@shared_task
# def task_video_encoding_1080p(video_id, height):
def task_video_encoding_1080p(video_id):
 logger.info('Video Processing started')
 try:
 video = Video.objects.get(video_id=video_id)
 input_file_path = video.video_file.url
 input_file_name = video.video_file.name

 # get the filename (without extension)
 filename = os.path.basename(input_file_path)

 # path to the new file, change it according to where you want to put it
 output_file_name = os.path.join('videos', 'mp4', '{}.mp4'.format(filename))
 output_file_path = os.path.join(settings.MEDIA_ROOT, output_file_name)

 for i in range(1):
 new_video_1080p = subprocess.call([FFMPEG_PATH, '-i', input_file_path, '-s', '1920x1080', '-vcodec', 'mpeg4', '-acodec', 'libvo_aacenc', '-b', '10000k', '-pass', i, '-r', '30', output_file_path])

 if new_video_1080p == 0:
 # save the new file in the database
 # video.video_file_hd.name = output_file_name
 video.save(update_fields=['video_file_fullhd'])
 logger.info('Video Processing Finished')
 return video
 else:
 logger.info('Proceesing Failed.') # Just for now

 except:
 raise ValidationError('Something went wrong')



In my first attempt I wasn't triggering the tasks no where, then I've tried to trigger the task from the schema.py file from graphql inside the video_by_id, but there I've got this error :


backend-celery-1 | django.core.exceptions.ValidationError: ['Something went wrong']
backend-celery-1 | [2023-06-18 16:38:52,859: ERROR/ForkPoolWorker-4] Task video.tasks.task_video_encoding_1080p[d33b1a42-5914-467c-ad5c-00565bc8be6f] raised unexpected: ValidationError(['Something went wrong'])
backend-celery-1 | Traceback (most recent call last):
backend-celery-1 | File "/usr/src/backend/video/tasks.py", line 81, in task_video_encoding_1080p
backend-celery-1 | new_video_1080p = subprocess.call([FFMPEG_PATH, '-i', input_file_path, '-s', '1920x1080', '-vcodec', 'mpeg4', '-acodec', 'libvo_aacenc', '-b', '10000k', '-pass', i, '-r', '30', output_file_path])
backend-celery-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-celery-1 | File "/usr/local/lib/python3.11/subprocess.py", line 389, in call
backend-celery-1 | with Popen(*popenargs, **kwargs) as p:
backend-celery-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
backend-celery-1 | File "/usr/local/lib/python3.11/subprocess.py", line 1026, in __init__
backend-celery-1 | self._execute_child(args, executable, preexec_fn, close_fds,
backend-celery-1 | File "/usr/local/lib/python3.11/subprocess.py", line 1883, in _execute_child
backend-celery-1 | self.pid = _fork_exec(
backend-celery-1 | ^^^^^^^^^^^
backend-celery-1 | TypeError: expected str, bytes or os.PathLike object, not int
backend-celery-1 | 
backend-celery-1 | During handling of the above exception, another exception occurred:
backend-celery-1 | 
backend-celery-1 | Traceback (most recent call last):
backend-celery-1 | File "/usr/local/lib/python3.11/site-packages/celery/app/trace.py", line 477, in trace_task
backend-celery-1 | R = retval = fun(*args, **kwargs)
backend-celery-1 | ^^^^^^^^^^^^^^^^^^^^
backend-celery-1 | File "/usr/local/lib/python3.11/site-packages/celery/app/trace.py", line 760, in __protected_call__
backend-celery-1 | return self.run(*args, **kwargs)
backend-celery-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^
backend-celery-1 | File "/usr/src/backend/video/tasks.py", line 93, in task_video_encoding_1080p
backend-celery-1 | raise ValidationError('Something went wrong')
backend-celery-1 | django.core.exceptions.ValidationError: ['Something went wrong']



If anyone has done this kind of project or something like this please any suggestion or help is much appreciated.


Thank you in advance !