Recherche avancée

Médias (0)

Mot : - Tags -/organisation

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (105)

Sur d’autres sites (20540)

  • ffmpeg how to set max_num_reorder_frames H264

    13 juin, par Vasil Yordanov

    Anyone know how can I set max_num_reorder_frames to 0 when I am encoding H264 video ?
You can find in the docs as uint8_t H264RawVUI::bitstream_restriction_flag

    


    PS. Based on the discussion in the comments. What I actually want to accomplish is to have all the frames written in the order in which they were encoded. My use-case is - I have 1000 images for example. I encode each one of them using the codec, but then when I investigate a little bit and check the actual packets in the H264 container, I see that I have cases when one frame is written twice (for example ... 1,2,3,3,4,5,6,7,7 ...) what I want is once I decode the the H264 container I want to get the same images which I encoded. Is that possible and how ?

    


    P.P.S : I don't think the g=1 works - giving some more code for reference. This is what I currently have :

    


    import numpy as np
import ffmpeg, subprocess, av

width, height, encoding_profile, pixel_format = 1280, 800, 'main', 'yuv420p'

# here I create 256 frames where each one has unique pixels all zeros, ones, twos and etc.
np_images = []
for i in range(256):
    np_image = i + np.zeros((height, width, 3), dtype=np.uint8)
    np_images.append(np_image)

print(f'number of numpy images: {len(np_images)}')

encoder = (ffmpeg
    .input('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(width, height))
    .output('pipe:', format='H264', pix_fmt=pixel_format, vcodec='libx264', profile='main', g=1)
    .run_async(pipe_stdin=True, pipe_stdout=True)
)

for timestamp, frame in enumerate(np_images):
    encoder.stdin.write(
        frame
        .astype(np.uint8)
        .tobytes()
    )

encoder.stdin.close()
output = encoder.stdout.read()
encoder.stdout.close()

# here I decode the encoded frames using PyAV
frame_decoder = av.CodecContext.create("h264", "r")
frame_decoder.thread_count = 0
frame_decoder.thread_type = 'NONE'
packets = frame_decoder.parse(output)
decoded_frames = []

for packet in packets:
    frame = frame_decoder.decode(packet)
    decoded_frames.extend(frame)

decoded_frames.extend(frame_decoder.decode())
print(f'number of decoded frames: {len(decoded_frames)}')
print('keyframe boolean mask')
print([e.key_frame for e in decoded_frames])

decoded_np_images = []

for frame in decoded_frames:
    decoded_np_images.append(np.array(frame.to_image()))

print(f'number of decoded numpy images: {len(decoded_np_images)}')

# here I check what the decoded frames contain (all zeros, ones, twos and etc.)
print([e[0,0,0].item() for e in decoded_np_images])


    


    the particular problem which I am facing is that in the output you can observe this :

    


    


    number of decoded numpy images : 255

    


    [0, 1, 2, 3, 3, 4, 5, 6, 8, 9, 10,
10, 11, 12, 13, 15, 16, 17, 17, 18, 19, 20, 22, 23, 24, 24, 25, 26,
27, 29, 30, 31, 31, 32, 33, 34, 36, 37, 38, 39, 39, 40, 41, 43, 44,
45, 46, 46, 47, 48, 50, 51, 52, 53, 53, 54, 55, 57, 58, 59, 60, 60,
61, 62, 64, 65, 66, 67, 67, 68, 69, 71, 72, 73, 74, 74, 75, 76, 78,
79, 80, 81, 81, 82, 83, 85, 86, 87, 88, 88, 89, 90, 91, 93, 94, 95,
95, 96, 97, 98, 100, 101, 102, 102, 103, 104, 105, 107, 108, 109, 109,
110, 111, 112, 114, 115, 116, 116, 117, 118, 119, 121, 122, 123, 123,
124, 125, 126, 128, 129, 130, 131, 131, 132, 133, 135, 136, 137, 138,
138, 139, 140, 142, 143, 144, 145, 145, 146, 147, 149, 150, 151, 152,
152, 153, 154, 156, 157, 158, 159, 159, 160, 161, 163, 164, 165, 166,
166, 167, 168, 170, 171, 172, 173, 173, 174, 175, 176, 178, 179, 180,
180, 181, 182, 183, 185, 186, 187, 187, 188, 189, 190, 192, 193, 194,
194, 195, 196, 197, 199, 200, 201, 201, 202, 203, 204, 206, 207, 208,
208, 209, 210, 211, 213, 214, 215, 216, 216, 217, 218, 220, 221, 222,
223, 223, 224, 225, 227, 228, 229, 230, 230, 231, 232, 234, 235, 236,
237, 237, 238, 239, 241, 242, 243, 244, 244, 245, 246, 248, 249, 250,
251, 251, 252, 253]

    


    


    I still have frames which are appearing twice (and respectively some are missing)

    


  • ffprobe to bitrate variable stopped working

    6 novembre 2023, par Bricktop

    I have a simple script to encode a video using the same bitrate as the original. I use ffprobe to fetch the bitrate like this :

    


    ffprobe "%file%" -v 0 -select_streams v:0 -show_entries stream=bit_rate -print_format compact=p=0:nokey=1 >%temp%\bitrate.txt


    


    However, while fixing a but in the script where I had an odd number of " marks, I suddenly ran into this problem with ffprobe :

    


    Argument ' -v 0 -select_streams v:0 -show_entries stream=bit_rate -print_format compact=p=0:nokey=1 >C:\Users\ADMINI~1\AppData\Local\Temp\bitrate.txt' provided as input filename, but 'D:\VIDEO\AMBIANCE\SCOPITONE\MUSIC TELEVISION\This Here - Calm - OFFICIAL VIDEO (1080p 25fps AV1-128kbit AAC).mp4' was already specified.


    


    I am trying to understand this, scanning insanely for yet another " or something in my code but can't figure it out. Here is the full code :

    


    :: write file to queue (first)
move /y "%~dpn0.txt" "%temp%\%~n0.tmp" >nul
echo "%~1" >"%~dpn0.txt"
type "%temp%\%~n0.tmp" >>"%~dpn0.txt"

:: desyncronize instances (todo: try support for adding 9 files at a time)
timeout /t %time:~9,1% /nobreak
:: if not first instance exit
tasklist /fi "imagename eq handbrakecli.exe" | find /i "handbrakecli" && exit
title Transcode

:: delegate queue
for /f "delims=" %%f in (%~dpn0.txt) do (
    set "name=%%~nf"
    set "file=%%~f"
    rem todo: if file has x264 or other video codec mentioned, change to x265
    set "code=%%~dpnf (x265 transcoded)%%~xf"
    call :transcode
)
echo all done!
exit /b

:transcode
title "%name%"
if not exist "%file%" echo %date% %time% source file missing %file% >>%~dpn0.log & goto cleanup
if exist "%code%" echo %date% %time% target file exists %file% >>%~dpn0.log & goto cleanup

:: determine appropriate bitrate (does not seem to work on .webm files, closing the script as a result)
%~dp0ffprobe "%file%" -v 0 -select_streams v:0 -show_entries stream=bit_rate -print_format compact=p=0:nokey=1 >%temp%\bitrate.txt
set /p bitrate=<%temp%\bitrate.txt
:: reduce to full kilobytes
set "bitrate=%bitrate:~0,-3%"
if not defined bitrate echo failed to fetch bitrate & echo %date% %time% no bitrate for %file% >>%~dpn0.log & exit /b
if %bitrate% gtr 7000 set bitrate=7000

:: transcode
%~dp0HandBrakeCLI -i "%file%" -o "%code%" --encoder x265_10bit --encoder-preset slow --encoder-profile main444-10 --vb %bitrate% --two-pass --turbo --audio 1-9 --aencoder copy --audio-copy-mask aac,ac3,mp2,mp3,opus --audio-fallback opus --ab 160 --drc 2.0

:: remove current file from queue, regardless
:cleanup
findstr /v /c:"%file%" "%~dpn0.txt" >"%temp%\%~n0.tmp"
move /y "%temp%\%~n0.tmp" "%~dpn0.txt"


    


    It appears that the set "file=%%~f" is the problem, somehow it shows up as set "file=D:\VIDEO\this video here.mp4" " where the last two characters " should not belong, and I don't know what to change to fix this.

    


    Every type of improvement to the script is very welcomed !

    


  • FFmpeg concatenation changes the duration and playback speed of the input videos

    24 février 2017, par Barnaby

    I have two input videos that I am concatenating using the FFmpeg concat demuxer :

    ffmpeg -f concat safe '0' -i /path/to/file.txt -c copy /path/to/output.mp4

    The file.txt contains the two file paths as follows :

    ffconcat version 1.0

    file /path/to/input/file1.mp4

    file /path/to/input/file2.mp4

    Unfortunately, the concatenated video has two issues :

    • The duration of the first input video is 4 seconds. In the output video, the last frame freezes for several seconds before the second input video starts.
    • The playback speed of the second input video is slowed down so that instead of being 7 seconds long, the video plays for around 14 or 15 seconds.

    I have tried specifying the duration of each video in the .txt file, as specified by the FFmpeg docs, but this doesn’t seem to make a difference to the output.

    I’m a bit of a newbie to FFmpeg, so any help is greatly appreciated !

    Edit - The properties of each input video as given by FFprobe :

    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'tmp/video/16382802.mp4':
     Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       encoder         : Lavf57.25.100
     Duration: 00:00:04.43, start: 0.000000, bitrate: 644 kb/s
       Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 360x640, 640 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
       Metadata:
         handler_name    : VideoHandler


    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'tmp/video/16382805.mp4':
     Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       encoder         : Lavf57.25.100
     Duration: 00:00:07.31, start: 0.023220, bitrate: 836 kb/s
       Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 360x640, 669 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
       Metadata:
         handler_name    : VideoHandler
       Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, 5.1, fltp, 158 kb/s (default)
       Metadata:
         handler_name    : SoundHandler

    Edit 2 - Stack trace when rewrapping the video (audio is lost) :

    ffmpeg version 3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
     built with Apple LLVM version 7.3.0 (clang-703.0.31)
     configuration: --prefix=/usr/local/Cellar/ffmpeg/3.0.1 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-opencl --enable-libx264 --enable-libmp3lame --enable-libxvid --enable-libfreetype --enable-ffplay --enable-libfdk-aac --enable-frei0r --enable-nonfree --enable-vda
     libavutil      55. 17.103 / 55. 17.103
     libavcodec     57. 24.102 / 57. 24.102
     libavformat    57. 25.100 / 57. 25.100
     libavdevice    57.  0.101 / 57.  0.101
     libavfilter     6. 31.100 /  6. 31.100
     libavresample   3.  0.  0 /  3.  0.  0
     libswscale      4.  0.100 /  4.  0.100
     libswresample   2.  0.101 /  2.  0.101
     libpostproc    54.  0.100 / 54.  0.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'https://s3-eu-west-1.amazonaws.com/stories.snaplytics.io/fTYbaN78DBVEQI0js0ydhNw/d3ef9a13-454c-4015-8412-cbd890e70e24.mp4':
     Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       encoder         : Lavf57.25.100
     Duration: 00:00:07.31, start: 0.023220, bitrate: 746 kb/s
       Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 360x640, 669 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
       Metadata:
         handler_name    : VideoHandler
       Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 67 kb/s (default)
       Metadata:
         handler_name    : SoundHandler
    Input #1, lavfi, from 'anullsrc':
     Duration: N/A, start: 0.000000, bitrate: 705 kb/s
       Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s
    Output #0, mp4, to '/Users/barnabytaylor/Documents/fanbytes-dashboard/tmp/video/16382805.mp4':
     Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       encoder         : Lavf57.25.100
       Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 360x640, q=2-31, 669 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 30k tbc (default)
       Metadata:
         handler_name    : VideoHandler
       Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, 5.1, fltp, 341 kb/s
       Metadata:
         encoder         : Lavc57.24.102 aac
    Stream mapping:
     Stream #0:0 -> #0:0 (copy)
     Stream #1:0 -> #0:1 (pcm_u8 (native) -> aac (native))
    Press [q] to stop, [?] for help
    frame=  219 fps= 57 q=-1.0 Lsize=   98706kB time=00:00:07.31 bitrate=110550.2kbits/s speed=1.91x    
    video:598kB audio:6kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 16257.420898%
    [aac @ 0x7ffbe2806e00] Qavg: 65536.000