
Recherche avancée
Autres articles (27)
-
Emballe médias : à quoi cela sert ?
4 février 2011, parCe plugin vise à gérer des sites de mise en ligne de documents de tous types.
Il crée des "médias", à savoir : un "média" est un article au sens SPIP créé automatiquement lors du téléversement d’un document qu’il soit audio, vidéo, image ou textuel ; un seul document ne peut être lié à un article dit "média" ; -
Selection of projects using MediaSPIP
2 mai 2011, parThe examples below are representative elements of MediaSPIP specific uses for specific projects.
MediaSPIP farm @ Infini
The non profit organizationInfini develops hospitality activities, internet access point, training, realizing innovative projects in the field of information and communication technologies and Communication, and hosting of websites. It plays a unique and prominent role in the Brest (France) area, at the national level, among the half-dozen such association. Its members (...) -
HTML5 audio and video support
13 avril 2011, parMediaSPIP uses HTML5 video and audio tags to play multimedia files, taking advantage of the latest W3C innovations supported by modern browsers.
The MediaSPIP player used has been created specifically for MediaSPIP and can be easily adapted to fit in with a specific theme.
For older browsers the Flowplayer flash fallback is used.
MediaSPIP allows for media playback on major mobile platforms with the above (...)
Sur d’autres sites (5030)
-
FFMPEG not working from Python subprocess.Popen but works from command line
21 novembre 2017, par Stuart ClarkeI’m trying to run a
FFMPEG
script from withinpython 3.5.2
. theFFMPEG
command runs fine from the command line but fails within the python script.The function I use to execute the
FFMPEG
command isdef ffmpeg(args):
err = None
command = [
ffmpegcmd, '-y',
'-loglevel', loglevel
] + args
print("\n\n\n")
print(command)
print("\n\n\n")
print(" ".join(command))
ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout=subprocess.PIPE, universal_newlines=True)
while err == None:
out, err = ffmpeg.communicate()
time.sleep(1)
if err and loglevel=='fatal':
print(" ".join(command))
raise Exception(err)
print(err)The value for args is :
['-i', '/var/www/temp/film.mp4', '-vf', 'scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:x=(1920-iw)/2:y=(1080-ih)/2:color=black', '/var/www/temp/videoHD.mp4']
such that the resultant command being passed to
subprocess.Popen
is :['ffmpeg', '-y', '-loglevel', 'verbose', '-i', '/var/www/temp/film.mp4', '-vf', 'scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:x=(1920-iw)/2:y=(1080-ih)/2:color=black', '/var/www/temp/videoHD.mp4']
The value of loglevel is
verbose
only for troubleshooting but is intended to run asfatal
. There are also someprint
commands only in place for troubleshooting.The reason for wanting to increase the resolution is so that it can be combined later with higher resolution videos.
The result when running from within
python
is :ffmpeg version 3.3.4-2~16.04.york0 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
configuration: --prefix=/usr --extra-version='2~16.04.york0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
[h264 @ 0x7f741a37a560] Reinit context to 640x368, pix_fmt: yuv420p
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/var/www/temp/film.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Duration: 00:06:19.80, start: 0.000000, bitrate: 451 kb/s
Stream #0:0(und): Video: h264 (High), 1 reference frame (avc1 / 0x31637661), yuv420p(left), 640x358 (640x368), 313 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[h264 @ 0x7f741a3bc700] Reinit context to 640x368, pix_fmt: yuv420p
[graph_1_in_0_1 @ 0x7f741a5315e0] tb:1/48000 samplefmt:fltp samplerate:48000 chlayout:0x3
[Parsed_scale_0 @ 0x7f741a54e000] w:1920 h:1080 flags:'bicubic' interl:0
[graph 0 input from stream 0:0 @ 0x7f741a54f6a0] w:640 h:358 pixfmt:yuv420p tb:1/15360 fr:30/1 sar:0/1 sws_param:flags=2
[Parsed_scale_0 @ 0x7f741a54e000] w:640 h:358 fmt:yuv420p sar:0/1 -> w:1920 h:1074 fmt:yuv420p sar:0/1 flags:0x4
[Parsed_pad_1 @ 0x7f741a54eac0] w:1920 h:1074 -> w:1920 h:1080 x:0 y:2 color:0x000000FF
[libx264 @ 0x7f741a5509e0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x7f741a5509e0] profile High, level 4.0
[libx264 @ 0x7f741a5509e0] 264 - core 148 r2795 aaa9aa8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/var/www/temp/videoHD.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Stream #0:0(und): Video: h264 (libx264), 1 reference frame ([33][0][0][0] / 0x0021), yuv420p(left), 1920x1080, q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc57.89.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(und): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, delay 1024, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc57.89.100 aacThis results in a file with no content.
When I run the command from the command line I need to add single quotes around the
-vf
video filtersscale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:x=(1920-iw)/2:y=(1080-ih)/2:color=black
so the whole command becomes :ffmpeg -y -loglevel verbose -i /var/www/temp/film.mp4 -vf 'scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:x=(1920-iw)/2:y=(1080-ih)/2:color=black' /var/www/temp/videoHD.mp4
Running this command gives the required result.
ffmpeg version 3.3.4-2~16.04.york0 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
configuration: --prefix=/usr --extra-version='2~16.04.york0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
[h264 @ 0x7f3a55c17560] Reinit context to 640x368, pix_fmt: yuv420p
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/var/www/temp/film.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Duration: 00:06:19.80, start: 0.000000, bitrate: 451 kb/s
Stream #0:0(und): Video: h264 (High), 1 reference frame (avc1 / 0x31637661), yuv420p(left), 640x358 (640x368), 313 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[h264 @ 0x7f3a55c59700] Reinit context to 640x368, pix_fmt: yuv420p
[graph_1_in_0_1 @ 0x7f3a55dce5e0] tb:1/48000 samplefmt:fltp samplerate:48000 chlayout:0x3
[Parsed_scale_0 @ 0x7f3a55deb000] w:1920 h:1080 flags:'bicubic' interl:0
[graph 0 input from stream 0:0 @ 0x7f3a55dec6a0] w:640 h:358 pixfmt:yuv420p tb:1/15360 fr:30/1 sar:0/1 sws_param:flags=2
[Parsed_scale_0 @ 0x7f3a55deb000] w:640 h:358 fmt:yuv420p sar:0/1 -> w:1920 h:1074 fmt:yuv420p sar:0/1 flags:0x4
[Parsed_pad_1 @ 0x7f3a55debac0] w:1920 h:1074 -> w:1920 h:1080 x:0 y:2 color:0x000000FF
[libx264 @ 0x7f3a55ded9e0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x7f3a55ded9e0] profile High, level 4.0
[libx264 @ 0x7f3a55ded9e0] 264 - core 148 r2795 aaa9aa8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/var/www/temp/videoHD.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Stream #0:0(und): Video: h264 (libx264), 1 reference frame ([33][0][0][0] / 0x0021), yuv420p(left), 1920x1080, q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc57.89.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(und): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, delay 1024, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc57.89.100 aac
frame= 46 fps=0.0 q=0.0 size= 0kB time=00:00:01.62 bitrate= 0.0kbits/s
frame= 59 fps= 47 q=29.0 size= 116kB time=00:00:02.04 bitrate= 464.8kbits/
frame= 74 fps= 41 q=29.0 size= 223kB time=00:00:02.56 bitrate= 714.8kbits/
frame= 87 fps= 35 q=29.0 size= 344kB time=00:00:02.98 bitrate= 942.4kbits/
frame= 98 fps= 33 q=29.0 size= 427kB time=00:00:03.34 bitrate=1043.4kbits/
blah
blah
blah
frame=11347 fps= 21 q=29.0 size= 97550kB time=00:06:18.32 bitrate=2112.3kbits/
frame=11363 fps= 21 q=29.0 size= 97713kB time=00:06:18.85 bitrate=2112.8kbits/
frame=11380 fps= 21 q=29.0 size= 97795kB time=00:06:19.41 bitrate=2111.5kbits/
No more output streams to write to, finishing.
frame=11392 fps= 21 q=-1.0 Lsize= 98519kB time=00:06:19.77 bitrate=2125.1kbits/s speed=0.69x
video:92124kB audio:5986kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.416313%
Input file #0 (/var/www/temp/film.mp4):
Input stream #0:0 (video): 11392 packets read (14892948 bytes); 11392 frames decoded;
Input stream #0:1 (audio): 17803 packets read (6112540 bytes); 17802 frames decoded (18229248 samples);
Total: 29195 packets (21005488 bytes) demuxed
Output file #0 (/var/www/temp/videoHD.mp4):
Output stream #0:0 (video): 11392 frames encoded; 11392 packets muxed (94334785 bytes);
Output stream #0:1 (audio): 17802 frames encoded (18229248 samples); 17803 packets muxed (6129981 bytes);
Total: 29195 packets (100464766 bytes) muxed
[libx264 @ 0x7f3a55ded9e0] frame I:74 Avg QP:19.08 size: 51540
[libx264 @ 0x7f3a55ded9e0] frame P:3506 Avg QP:21.34 size: 19348
[libx264 @ 0x7f3a55ded9e0] frame B:7812 Avg QP:24.07 size: 2904
[libx264 @ 0x7f3a55ded9e0] consecutive B-frames: 1.3% 20.2% 4.6% 73.9%
[libx264 @ 0x7f3a55ded9e0] mb I I16..4: 19.7% 73.6% 6.7%
[libx264 @ 0x7f3a55ded9e0] mb P I16..4: 3.2% 6.4% 0.1% P16..4: 52.5% 7.6% 2.6% 0.0% 0.0% skip:27.6%
[libx264 @ 0x7f3a55ded9e0] mb B I16..4: 0.2% 0.2% 0.0% B16..8: 23.2% 0.4% 0.0% direct: 0.5% skip:75.5% L0:51.8% L1:46.6% BI: 1.6%
[libx264 @ 0x7f3a55ded9e0] 8x8 transform intra:66.4% inter:94.0%
[libx264 @ 0x7f3a55ded9e0] coded y,uvDC,uvAC intra: 33.6% 53.4% 7.3% inter: 5.3% 14.2% 0.0%
[libx264 @ 0x7f3a55ded9e0] i16 v,h,dc,p: 22% 27% 5% 46%
[libx264 @ 0x7f3a55ded9e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 19% 20% 4% 7% 7% 7% 5% 4%
[libx264 @ 0x7f3a55ded9e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 31% 28% 11% 3% 9% 7% 6% 3% 2%
[libx264 @ 0x7f3a55ded9e0] i8c dc,h,v,p: 49% 21% 20% 10%
[libx264 @ 0x7f3a55ded9e0] Weighted P-Frames: Y:0.5% UV:0.1%
[libx264 @ 0x7f3a55ded9e0] ref P L0: 63.4% 8.9% 21.4% 6.3% 0.0%
[libx264 @ 0x7f3a55ded9e0] ref B L0: 89.3% 9.1% 1.6%
[libx264 @ 0x7f3a55ded9e0] ref B L1: 98.1% 1.9%
[libx264 @ 0x7f3a55ded9e0] kb/s:1987.38
[aac @ 0x7f3a55deee80] Qavg: 1160.814and the resulting video is fine.
Any ideas ?
Cheers,
Stu
Edit
It was suggested that I use a new function to do much the same thing. So I added the following function and used it for the command that I’m having trouble with :
def ffmpegPro(args):
err = None
for i, arg in enumerate(args):
if(" " in arg or "(" in arg or "[" in arg):
args[i] = "\"" + arg + "\""
command = [
ffmpegcmd, '-y',
'-loglevel', loglevel
] + args
commandStr = " ".join(command)
print("\n\n\n")
print(command)
print("\n\n\n")
print(commandStr)
ffmpeg = subprocess.Popen(commandStr, stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True, shell=True)
while err == None:
out, err = ffmpeg.communicate()
time.sleep(1)
if ffmpeg.returncode == 0 and all([os.path.exists(f) for f in (videoStorage['data_small'], videoStorage['data_big'])]):
print("Job done.")
else:
print("ERROR")
print(err)The resulting command can now be used in
python
or at the command line without needing to be changed at all. The command is :ffmpeg -y -loglevel verbose -i /var/www/temp/film.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:x=(1920-iw)/2:y=(1080-ih)/2:color=black" /var/www/temp/videoHD.mp4
The result when executed in
python
isffmpeg version 3.3.4-2~16.04.york0 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
configuration: --prefix=/usr --extra-version='2~16.04.york0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
[h264 @ 0x7fb75053d560] Reinit context to 640x368, pix_fmt: yuv420p
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/var/www/temp/film.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Duration: 00:06:19.80, start: 0.000000, bitrate: 451 kb/s
Stream #0:0(und): Video: h264 (High), 1 reference frame (avc1 / 0x31637661), yuv420p(left), 640x358 (640x368), 313 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[h264 @ 0x7fb75057f700] Reinit context to 640x368, pix_fmt: yuv420p
[graph_1_in_0_1 @ 0x7fb7506f45e0] tb:1/48000 samplefmt:fltp samplerate:48000 chlayout:0x3
[Parsed_scale_0 @ 0x7fb750711000] w:1920 h:1080 flags:'bicubic' interl:0
[graph 0 input from stream 0:0 @ 0x7fb7507126a0] w:640 h:358 pixfmt:yuv420p tb:1/15360 fr:30/1 sar:0/1 sws_param:flags=2
[Parsed_scale_0 @ 0x7fb750711000] w:640 h:358 fmt:yuv420p sar:0/1 -> w:1920 h:1074 fmt:yuv420p sar:0/1 flags:0x4
[Parsed_pad_1 @ 0x7fb750711ac0] w:1920 h:1074 -> w:1920 h:1080 x:0 y:2 color:0x000000FF
[libx264 @ 0x7fb7507139e0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x7fb7507139e0] profile High, level 4.0
[libx264 @ 0x7fb7507139e0] 264 - core 148 r2795 aaa9aa8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/var/www/temp/videoHD.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Stream #0:0(und): Video: h264 (libx264), 1 reference frame ([33][0][0][0] / 0x0021), yuv420p(left), 1920x1080, q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc57.89.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(und): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, delay 1024, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc57.89.100 aac
KilledNote the
Killed
at the end. Is this significant and why is it only happening when executed in the script ?Here is the output from the command line which results in a successful video file.
ffmpeg version 3.3.4-2~16.04.york0 Copyright (c) 2000-2017 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 20160609
configuration: --prefix=/usr --extra-version='2~16.04.york0' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
[h264 @ 0x7fb52cfb3560] Reinit context to 640x368, pix_fmt: yuv420p
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/var/www/temp/film.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Duration: 00:06:19.80, start: 0.000000, bitrate: 451 kb/s
Stream #0:0(und): Video: h264 (High), 1 reference frame (avc1 / 0x31637661), yuv420p(left), 640x358 (640x368), 313 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[h264 @ 0x7fb52cff5700] Reinit context to 640x368, pix_fmt: yuv420p
[graph_1_in_0_1 @ 0x7fb52d16a5e0] tb:1/48000 samplefmt:fltp samplerate:48000 chlayout:0x3
[Parsed_scale_0 @ 0x7fb52d187000] w:1920 h:1080 flags:'bicubic' interl:0
[graph 0 input from stream 0:0 @ 0x7fb52d1886a0] w:640 h:358 pixfmt:yuv420p tb:1/15360 fr:30/1 sar:0/1 sws_param:flags=2
[Parsed_scale_0 @ 0x7fb52d187000] w:640 h:358 fmt:yuv420p sar:0/1 -> w:1920 h:1074 fmt:yuv420p sar:0/1 flags:0x4
[Parsed_pad_1 @ 0x7fb52d187ac0] w:1920 h:1074 -> w:1920 h:1080 x:0 y:2 color:0x000000FF
[libx264 @ 0x7fb52d1899e0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x7fb52d1899e0] profile High, level 4.0
[libx264 @ 0x7fb52d1899e0] 264 - core 148 r2795 aaa9aa8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/var/www/temp/videoHD.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Stream #0:0(und): Video: h264 (libx264), 1 reference frame ([33][0][0][0] / 0x0021), yuv420p(left), 1920x1080, q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc57.89.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(und): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, delay 1024, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc57.89.100 aac
frame= 47 fps=0.0 q=0.0 size= 0kB time=00:00:01.66 bitrate= 0.0kbits/s
frame= 59 fps= 41 q=29.0 size= 116kB time=00:00:02.04 bitrate= 464.8kbits/
frame= 71 fps= 35 q=29.0 size= 201kB time=00:00:02.45 bitrate= 671.3kbits/
frame= 80 fps= 31 q=29.0 size= 276kB time=00:00:02.75 bitrate= 822.0kbits/
Blah
Blah
Blah
frame=11337 fps= 21 q=29.0 size= 97503kB time=00:06:17.98 bitrate=2113.2kbits/
frame=11351 fps= 21 q=26.0 size= 97572kB time=00:06:18.45 bitrate=2112.1kbits/
frame=11371 fps= 21 q=29.0 size= 97745kB time=00:06:19.11 bitrate=2112.1kbits/
frame=11390 fps= 21 q=29.0 size= 97852kB time=00:06:19.73 bitrate=2111.0kbits/
No more output streams to write to, finishing.
frame=11392 fps= 21 q=-1.0 Lsize= 98519kB time=00:06:19.77 bitrate=2125.1kbits/s speed=0.688x
video:92124kB audio:5986kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.416313%
Input file #0 (/var/www/temp/film.mp4):
Input stream #0:0 (video): 11392 packets read (14892948 bytes); 11392 frames decoded;
Input stream #0:1 (audio): 17803 packets read (6112540 bytes); 17802 frames decoded (18229248 samples);
Total: 29195 packets (21005488 bytes) demuxed
Output file #0 (/var/www/temp/videoHD.mp4):
Output stream #0:0 (video): 11392 frames encoded; 11392 packets muxed (94334785 bytes);
Output stream #0:1 (audio): 17802 frames encoded (18229248 samples); 17803 packets muxed (6129981 bytes);
Total: 29195 packets (100464766 bytes) muxed
[libx264 @ 0x7fb52d1899e0] frame I:74 Avg QP:19.08 size: 51540
[libx264 @ 0x7fb52d1899e0] frame P:3506 Avg QP:21.34 size: 19348
[libx264 @ 0x7fb52d1899e0] frame B:7812 Avg QP:24.07 size: 2904
[libx264 @ 0x7fb52d1899e0] consecutive B-frames: 1.3% 20.2% 4.6% 73.9%
[libx264 @ 0x7fb52d1899e0] mb I I16..4: 19.7% 73.6% 6.7%
[libx264 @ 0x7fb52d1899e0] mb P I16..4: 3.2% 6.4% 0.1% P16..4: 52.5% 7.6% 2.6% 0.0% 0.0% skip:27.6%
[libx264 @ 0x7fb52d1899e0] mb B I16..4: 0.2% 0.2% 0.0% B16..8: 23.2% 0.4% 0.0% direct: 0.5% skip:75.5% L0:51.8% L1:46.6% BI: 1.6%
[libx264 @ 0x7fb52d1899e0] 8x8 transform intra:66.4% inter:94.0%
[libx264 @ 0x7fb52d1899e0] coded y,uvDC,uvAC intra: 33.6% 53.4% 7.3% inter: 5.3% 14.2% 0.0%
[libx264 @ 0x7fb52d1899e0] i16 v,h,dc,p: 22% 27% 5% 46%
[libx264 @ 0x7fb52d1899e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 19% 20% 4% 7% 7% 7% 5% 4%
[libx264 @ 0x7fb52d1899e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 31% 28% 11% 3% 9% 7% 6% 3% 2%
[libx264 @ 0x7fb52d1899e0] i8c dc,h,v,p: 49% 21% 20% 10%
[libx264 @ 0x7fb52d1899e0] Weighted P-Frames: Y:0.5% UV:0.1%
[libx264 @ 0x7fb52d1899e0] ref P L0: 63.4% 8.9% 21.4% 6.3% 0.0%
[libx264 @ 0x7fb52d1899e0] ref B L0: 89.3% 9.1% 1.6%
[libx264 @ 0x7fb52d1899e0] ref B L1: 98.1% 1.9%
[libx264 @ 0x7fb52d1899e0] kb/s:1987.38
[aac @ 0x7fb52d18ae80] Qavg: 1160.814Any more ideas why this is being handled differently in
python
as opposed to the command line ?I’m believe this is a version issue and that is why the below answer did not work for me, things were fine before I upgraded but since then a lot of things have been playing up. I upgraded both
ffmpeg
andpython
versions and don’t really want to go back if it can be avoided.Thanks,
Stu
-
Ffmpeg in Android Studio
7 décembre 2016, par Ahmed Abd-Elmegedi’am trying to add Ffmpeg library to use it in Live Streaming app based in Youtube APi when i add it and i use Cmake Build tool the header files only shown in the c native class which i create and i want it to be included in the other header files
which came with library how can i do it in cmake script or onther way ?This my cmake build script
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
ffmpeg-jni
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/ffmpeg-jni.c )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
ffmpeg-jni
# Links the target library to the log library
# included in the NDK.
${log-lib} )
include_directories(src/main/cpp/include/ffmpeglib)
include_directories(src/main/cpp/include/libavcodec)
include_directories(src/main/cpp/include/libavformat)
include_directories(src/main/cpp/include/libavutil)This my native class i create as native file i android studio and i also have an error in jintJNIcall not found
#include <android></android>log.h>
#include
#include
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/opt.h"
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jboolean
JNICALL Java_com_example_venrto_ffmpegtest_Ffmpeg_init(JNIEnv *env, jobject thiz,
jint width, jint height,
jint audio_sample_rate,
jstring rtmp_url);
JNIEXPORT void JNICALL Java_com_example_venrto_ffmpegtest_Ffmpeg_shutdown(JNIEnv
*env,
jobject thiz
);
JNIEXPORT jintJNICALL
Java_com_example_venrto_ffmpegtest_Ffmpeg_encodeVideoFrame(JNIEnv
*env,
jobject thiz,
jbyteArray
yuv_image);
JNIEXPORT jint
JNICALL Java_com_example_venrto_ffmpegtest_Ffmpeg_encodeAudioFrame(JNIEnv *env,
jobject thiz,
jshortArray audio_data,
jint length);
#ifdef __cplusplus
}
#endif
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "ffmpeg-jni", __VA_ARGS__)
#define URL_WRONLY 2
static AVFormatContext *fmt_context;
static AVStream *video_stream;
static AVStream *audio_stream;
static int pts
= 0;
static int last_audio_pts = 0;
// Buffers for UV format conversion
static unsigned char *u_buf;
static unsigned char *v_buf;
static int enable_audio = 1;
static int64_t audio_samples_written = 0;
static int audio_sample_rate = 0;
// Stupid buffer for audio samples. Not even a proper ring buffer
#define AUDIO_MAX_BUF_SIZE 16384 // 2x what we get from Java
static short audio_buf[AUDIO_MAX_BUF_SIZE];
static int audio_buf_size = 0;
void AudioBuffer_Push(const short *audio, int num_samples) {
if (audio_buf_size >= AUDIO_MAX_BUF_SIZE - num_samples) {
LOGI("AUDIO BUFFER OVERFLOW: %i + %i > %i", audio_buf_size, num_samples,
AUDIO_MAX_BUF_SIZE);
return;
}
for (int i = 0; i < num_samples; i++) {
audio_buf[audio_buf_size++] = audio[i];
}
}
int AudioBuffer_Size() { return audio_buf_size; }
short *AudioBuffer_Get() { return audio_buf; }
void AudioBuffer_Pop(int num_samples) {
if (num_samples > audio_buf_size) {
LOGI("Audio buffer Pop WTF: %i vs %i", num_samples, audio_buf_size);
return;
}
memmove(audio_buf, audio_buf + num_samples, num_samples * sizeof(short));
audio_buf_size -= num_samples;
}
void AudioBuffer_Clear() {
memset(audio_buf, 0, sizeof(audio_buf));
audio_buf_size = 0;
}
static void log_callback(void *ptr, int level, const char *fmt, va_list vl) {
char x[2048];
vsnprintf(x, 2048, fmt, vl);
LOGI(x);
}
JNIEXPORT jboolean
JNICALL Java_com_example_venrto_ffmpegtest_Ffmpeg_init(JNIEnv *env, jobject thiz,
jint width, jint height,
jint audio_sample_rate_param,
jstring rtmp_url) {
avcodec_register_all();
av_register_all();
av_log_set_callback(log_callback);
fmt_context = avformat_alloc_context();
AVOutputFormat *ofmt = av_guess_format("flv", NULL, NULL);
if (ofmt) {
LOGI("av_guess_format returned %s", ofmt->long_name);
} else {
LOGI("av_guess_format fail");
return JNI_FALSE;
}
fmt_context->oformat = ofmt;
LOGI("creating video stream");
video_stream = av_new_stream(fmt_context, 0);
if (enable_audio) {
LOGI("creating audio stream");
audio_stream = av_new_stream(fmt_context, 1);
}
// Open Video Codec.
// ======================
AVCodec *video_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!video_codec) {
LOGI("Did not find the video codec");
return JNI_FALSE; // leak!
} else {
LOGI("Video codec found!");
}
AVCodecContext *video_codec_ctx = video_stream->codec;
video_codec_ctx->codec_id = video_codec->id;
video_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
video_codec_ctx->level = 31;
video_codec_ctx->width = width;
video_codec_ctx->height = height;
video_codec_ctx->pix_fmt = PIX_FMT_YUV420P;
video_codec_ctx->rc_max_rate = 0;
video_codec_ctx->rc_buffer_size = 0;
video_codec_ctx->gop_size = 12;
video_codec_ctx->max_b_frames = 0;
video_codec_ctx->slices = 8;
video_codec_ctx->b_frame_strategy = 1;
video_codec_ctx->coder_type = 0;
video_codec_ctx->me_cmp = 1;
video_codec_ctx->me_range = 16;
video_codec_ctx->qmin = 10;
video_codec_ctx->qmax = 51;
video_codec_ctx->keyint_min = 25;
video_codec_ctx->refs = 3;
video_codec_ctx->trellis = 0;
video_codec_ctx->scenechange_threshold = 40;
video_codec_ctx->flags |= CODEC_FLAG_LOOP_FILTER;
video_codec_ctx->me_method = ME_HEX;
video_codec_ctx->me_subpel_quality = 6;
video_codec_ctx->i_quant_factor = 0.71;
video_codec_ctx->qcompress = 0.6;
video_codec_ctx->max_qdiff = 4;
video_codec_ctx->time_base.den = 10;
video_codec_ctx->time_base.num = 1;
video_codec_ctx->bit_rate = 3200 * 1000;
video_codec_ctx->bit_rate_tolerance = 0;
video_codec_ctx->flags2 |= 0x00000100;
fmt_context->bit_rate = 4000 * 1000;
av_opt_set(video_codec_ctx, "partitions", "i8x8,i4x4,p8x8,b8x8", 0);
av_opt_set_int(video_codec_ctx, "direct-pred", 1, 0);
av_opt_set_int(video_codec_ctx, "rc-lookahead", 0, 0);
av_opt_set_int(video_codec_ctx, "fast-pskip", 1, 0);
av_opt_set_int(video_codec_ctx, "mixed-refs", 1, 0);
av_opt_set_int(video_codec_ctx, "8x8dct", 0, 0);
av_opt_set_int(video_codec_ctx, "weightb", 0, 0);
if (fmt_context->oformat->flags & AVFMT_GLOBALHEADER)
video_codec_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
LOGI("Opening video codec");
AVDictionary *vopts = NULL;
av_dict_set(&vopts, "profile", "main", 0);
//av_dict_set(&vopts, "vprofile", "main", 0);
av_dict_set(&vopts, "rc-lookahead", 0, 0);
av_dict_set(&vopts, "tune", "film", 0);
av_dict_set(&vopts, "preset", "ultrafast", 0);
av_opt_set(video_codec_ctx->priv_data, "tune", "film", 0);
av_opt_set(video_codec_ctx->priv_data, "preset", "ultrafast", 0);
av_opt_set(video_codec_ctx->priv_data, "tune", "film", 0);
int open_res = avcodec_open2(video_codec_ctx, video_codec, &vopts);
if (open_res < 0) {
LOGI("Error opening video codec: %i", open_res);
return JNI_FALSE; // leak!
}
// Open Audio Codec.
// ======================
if (enable_audio) {
AudioBuffer_Clear();
audio_sample_rate = audio_sample_rate_param;
AVCodec *audio_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (!audio_codec) {
LOGI("Did not find the audio codec");
return JNI_FALSE; // leak!
} else {
LOGI("Audio codec found!");
}
AVCodecContext *audio_codec_ctx = audio_stream->codec;
audio_codec_ctx->codec_id = audio_codec->id;
audio_codec_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
audio_codec_ctx->bit_rate = 128000;
audio_codec_ctx->bit_rate_tolerance = 16000;
audio_codec_ctx->channels = 1;
audio_codec_ctx->profile = FF_PROFILE_AAC_LOW;
audio_codec_ctx->sample_fmt = AV_SAMPLE_FMT_FLT;
audio_codec_ctx->sample_rate = 44100;
LOGI("Opening audio codec");
AVDictionary *opts = NULL;
av_dict_set(&opts, "strict", "experimental", 0);
open_res = avcodec_open2(audio_codec_ctx, audio_codec, &opts);
LOGI("audio frame size: %i", audio_codec_ctx->frame_size);
if (open_res < 0) {
LOGI("Error opening audio codec: %i", open_res);
return JNI_FALSE; // leak!
}
}
const jbyte *url = (*env)->GetStringUTFChars(env, rtmp_url, NULL);
// Point to an output file
if (!(ofmt->flags & AVFMT_NOFILE)) {
if (avio_open(&fmt_context->pb, url, URL_WRONLY) < 0) {
LOGI("ERROR: Could not open file %s", url);
return JNI_FALSE; // leak!
}
}
(*env)->ReleaseStringUTFChars(env, rtmp_url, url);
LOGI("Writing output header.");
// Write file header
if (avformat_write_header(fmt_context, NULL) != 0) {
LOGI("ERROR: av_write_header failed");
return JNI_FALSE;
}
pts = 0;
last_audio_pts = 0;
audio_samples_written = 0;
// Initialize buffers for UV format conversion
int frame_size = video_codec_ctx->width * video_codec_ctx->height;
u_buf = (unsigned char *) av_malloc(frame_size / 4);
v_buf = (unsigned char *) av_malloc(frame_size / 4);
LOGI("ffmpeg encoding init done");
return JNI_TRUE;
}
JNIEXPORT void JNICALL
Java_com_example_venrto_ffmpegtest_Ffmpeg_shutdown(JNIEnv
*env,
jobject thiz
) {
av_write_trailer(fmt_context);
avio_close(fmt_context
->pb);
avcodec_close(video_stream
->codec);
if (enable_audio) {
avcodec_close(audio_stream
->codec);
}
av_free(fmt_context);
av_free(u_buf);
av_free(v_buf);
fmt_context = NULL;
u_buf = NULL;
v_buf = NULL;
}
JNIEXPORT jintJNICALL
Java_com_example_venrto_ffmpegtest_Ffmpeg_encodeVideoFrame(JNIEnv
*env,
jobject thiz,
jbyteArray
yuv_image) {
int yuv_length = (*env)->GetArrayLength(env, yuv_image);
unsigned char *yuv_data = (*env)->GetByteArrayElements(env, yuv_image, 0);
AVCodecContext *video_codec_ctx = video_stream->codec;
//LOGI("Yuv size: %i w: %i h: %i", yuv_length, video_codec_ctx->width, video_codec_ctx->height);
int frame_size = video_codec_ctx->width * video_codec_ctx->height;
const unsigned char *uv = yuv_data + frame_size;
// Convert YUV from NV12 to I420. Y channel is the same so we don't touch it,
// we just have to deinterleave UV.
for (
int i = 0;
i < frame_size / 4; i++) {
v_buf[i] = uv[i * 2];
u_buf[i] = uv[i * 2 + 1];
}
AVFrame source;
memset(&source, 0, sizeof(AVFrame));
source.data[0] =
yuv_data;
source.data[1] =
u_buf;
source.data[2] =
v_buf;
source.linesize[0] = video_codec_ctx->
width;
source.linesize[1] = video_codec_ctx->width / 2;
source.linesize[2] = video_codec_ctx->width / 2;
// only for bitrate regulation. irrelevant for sync.
source.
pts = pts;
pts++;
int out_length = frame_size + (frame_size / 2);
unsigned char *out = (unsigned char *) av_malloc(out_length);
int compressed_length = avcodec_encode_video(video_codec_ctx, out, out_length, &source);
(*env)->
ReleaseByteArrayElements(env, yuv_image, yuv_data,
0);
// Write to file too
if (compressed_length > 0) {
AVPacket pkt;
av_init_packet(&pkt);
pkt.
pts = last_audio_pts;
if (video_codec_ctx->coded_frame && video_codec_ctx->coded_frame->key_frame) {
pkt.flags |= 0x0001;
}
pkt.
stream_index = video_stream->index;
pkt.
data = out;
pkt.
size = compressed_length;
if (
av_interleaved_write_frame(fmt_context,
&pkt) != 0) {
LOGI("Error writing video frame");
}
} else {
LOGI("??? compressed_length <= 0");
}
last_audio_pts++;
av_free(out);
return
compressed_length;
}
JNIEXPORT jintJNICALL
Java_com_example_venrto_ffmpegtest_Ffmpeg_encodeAudioFrame(JNIEnv
*env,
jobject thiz,
jshortArray
audio_data,
jint length
) {
if (!enable_audio) {
return 0;
}
short *audio = (*env)->GetShortArrayElements(env, audio_data, 0);
//LOGI("java audio buffer size: %i", length);
AVCodecContext *audio_codec_ctx = audio_stream->codec;
unsigned char *out = av_malloc(128000);
AudioBuffer_Push(audio, length
);
int total_compressed = 0;
while (
AudioBuffer_Size()
>= audio_codec_ctx->frame_size) {
AVPacket pkt;
av_init_packet(&pkt);
int compressed_length = avcodec_encode_audio(audio_codec_ctx, out, 128000,
AudioBuffer_Get());
total_compressed +=
compressed_length;
audio_samples_written += audio_codec_ctx->
frame_size;
int new_pts = (audio_samples_written * 1000) / audio_sample_rate;
if (compressed_length > 0) {
pkt.
size = compressed_length;
pkt.
pts = new_pts;
last_audio_pts = new_pts;
//LOGI("audio_samples_written: %i comp_length: %i pts: %i", (int)audio_samples_written, (int)compressed_length, (int)new_pts);
pkt.flags |= 0x0001;
pkt.
stream_index = audio_stream->index;
pkt.
data = out;
if (
av_interleaved_write_frame(fmt_context,
&pkt) != 0) {
LOGI("Error writing audio frame");
}
}
AudioBuffer_Pop(audio_codec_ctx
->frame_size);
}
(*env)->
ReleaseShortArrayElements(env, audio_data, audio,
0);
av_free(out);
return
total_compressed;
}this an example for a header file which i have an error include
#include "libavutil/samplefmt.h"
#include "libavutil/avutil.h"
#include "libavutil/cpu.h"
#include "libavutil/dict.h"
#include "libavutil/log.h"
#include "libavutil/pixfmt.h"
#include "libavutil/rational.h"
#include "libavutil/version.h"in the libavcodec/avcodec.h
This code based in this example :
https://github.com/youtube/yt-watchme -
FFmpeg transcoding video H.264 -> AV1 leads to distorted image
21 juin 2023, par user1584149I'm reading video from stdin via pipe similar to this one Python gets stuck at pipe.stdin.write(image.tostring())


FFmpeg is using libsvtav1 codec with options according to documentation


ffmpeg -y -f rawvideo -i pipe:0 -s {frame_width}x{frame_height} -r {frame_rate} -c:a copy -c:v libsvtav1 -preset 6 -crf 30 -svtav1-params "color-format=1:film-grain-denoise=0:mbr=2M" {output_file} 



Here's couple of details about used FFmpeg version as well as input video


ffmpeg version n5.1.3-14-ge5b5dd6653-20230621 Copyright (c) 2000-2022 the FFmpeg developers
 built with gcc 13.1.0 (crosstool-NG 1.25.0.196_227d99d)
 configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-ffbuild-linux-gnu- --arch=x86_64 --target-os=linux --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-openssl --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --enable-libpulse --enable-libvmaf --enable-libxcb --enable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --disable-chromaprint --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --disable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --disable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --enable-libdrm --enable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --disable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags=-pie --extra-libs='-ldl -lgomp' --extra-version=20230621
 libavutil 57. 28.100 / 57. 28.100
 libavcodec 59. 37.100 / 59. 37.100
 libavformat 59. 27.100 / 59. 27.100
 libavdevice 59. 7.100 / 59. 7.100
 libavfilter 8. 44.100 / 8. 44.100
 libswscale 6. 7.100 / 6. 7.100
 libswresample 4. 7.100 / 4. 7.100
 libpostproc 56. 6.100 / 56. 6.100
Input #0, rawvideo, from 'pipe:0':
 Duration: N/A, start: 0.000000, bitrate: 331776 kb/s
 Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1280x720, 331776 kb/s, 30 tbr, 30 tbn
Stream mapping:
 Stream #0:0 -> #0:0 (rawvideo (native) -> av1 (libsvtav1))
Svt[info]: -------------------------------------------
Svt[info]: SVT [version]: SVT-AV1 Encoder Lib v1.5.0-12-g0f8b3a81
Svt[info]: SVT [build] : GCC 13.1.0 64 bit
Svt[info]: LIB Build date: Jun 20 2023 23:48:14
Svt[info]: -------------------------------------------



The output video transcoded by SVT-AV1 codec is blemished as you can see on the images below. Normally I would say it could be caused by wrong pixel format, but input stream has yuv420 format what is the same as encoder's pixel format parameter —color-format.


Please share your experience or ideas how to get it transcoding correctly.


Thanks in advance.