Recherche avancée

Médias (0)

Mot : - Tags -/utilisateurs

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

Autres articles (55)

  • Websites made ​​with MediaSPIP

    2 mai 2011, par

    This page lists some websites based on MediaSPIP.

  • MediaSPIP Core : La Configuration

    9 novembre 2010, par

    MediaSPIP Core fournit par défaut trois pages différentes de configuration (ces pages utilisent le plugin de configuration CFG pour fonctionner) : une page spécifique à la configuration générale du squelettes ; une page spécifique à la configuration de la page d’accueil du site ; une page spécifique à la configuration des secteurs ;
    Il fournit également une page supplémentaire qui n’apparait que lorsque certains plugins sont activés permettant de contrôler l’affichage et les fonctionnalités spécifiques (...)

  • Creating farms of unique websites

    13 avril 2011, par

    MediaSPIP platforms can be installed as a farm, with a single "core" hosted on a dedicated server and used by multiple websites.
    This allows (among other things) : implementation costs to be shared between several different projects / individuals rapid deployment of multiple unique sites creation of groups of like-minded sites, making it possible to browse media in a more controlled and selective environment than the major "open" (...)

Sur d’autres sites (9084)

  • Using PyAV to encode mono audio to file, params match docs, but still causes Errno 22

    20 février 2023, par andrew8088

    While trying to use PyAV to encode live mono audio from a microphone to a compressed audio stream (using mp2 or flac as encoder), the program kept raising an exception ValueError: [Errno 22] Invalid argument.

    


    To remove the live microphone source as a cause of the problem, and to make the problematic code easier for others to run/test, I have removed the mic source and now just generate a pure tone as a sequence of input buffers.

    


    All attempts to figure out the missing or mismatched or incorrect argument have just resulted in seeing documentation and examples that are the same as my code.

    


    I would like to know from someone who has used PyAV successfully for mono audio what the correct method and parameters are for encoding mono frames into the mono stream.

    


    The package used is av 10.0.0 installed with
pip3 install av --no-binary av
so it uses my package-manager provided ffmpeg library, which is version 4.2.7.

    


    The problematic python code is :

    


    #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Recreating an error 22 when encoding sound with PyAV.

Created on Sun Feb 19 08:10:29 2023
@author: andrewm
"""
import typing
import sys
import math
import fractions

import av
from av import AudioFrame

""" Ensure some PyAudio constants are still defined without changing 
    the PyAudio recording callback function and without depending 
    on PyAudio simply for reproducing the PyAV bug [Errno 22] thrown in 
    File "av/filter/context.pyx", line 89, in av.filter.context.FilterContext.push
"""
class PA_Stub():
    paContinue = True
    paComplete= False

pyaudio = PA_Stub()


"""Generate pure tone at given frequency with amplitude 0...1.0 at 
   sampling frewuency fs and beginning at phase offset 'phase'.
   Returns the new phase after the sinusoid has cycled over the 
   sampling window length.
"""
def generate_tone(
        freq:int, phase:float, amp:float, fs, samp_fmt, buffer:bytearray
) -> float:
    assert samp_fmt == "s16", "Only s16 supported atm"
    samp_size_bytes = 2
    n_samples = int(len(buffer)/samp_size_bytes)
    window = [int(0) for i in range(n_samples)]
    theta = phase
    phase_inc = 2*math.pi * freq / fs
    for i in range(n_samples):
        v = amp * math.sin(theta)
        theta += phase_inc
        s = int((2**15-1)*v)
        window[i] = s
    for sample_i in range(len(window)):
        byte_i = sample_i * samp_size_bytes
        enc = window[sample_i].to_bytes(
                2, byteorder=sys.byteorder, signed=True
        )
        buffer[byte_i] = enc[0]
        buffer[byte_i+1] = enc[1]
    return theta


channels = 1
fs = 44100  # Record at 44100 samples per second
fft_size_samps = 256
chunk_samps = fft_size_samps * 10  # Record in chunks that are multiples of fft windows.

# print(f"fft_size_samps={fft_size_samps}\nchunk_samps={chunk_samps}")

seconds = 3.0
out_filename = "testoutput.wav"

# Store data in chunks for 3 seconds
sample_limit = int(fs * seconds)
sample_len = 0
frames = []  # Initialize array to store frames

ffmpeg_codec_name = 'mp2'  # flac, mp3, or libvorbis make same error.

sample_size_bytes = 2
buffer = bytearray(int(chunk_samps*sample_size_bytes))
chunkperiod = chunk_samps / fs
total_chunks = int(math.ceil(seconds / chunkperiod))
phase = 0.0

### uncomment if you want to see the synthetic data being used as a mic input.
# with open("test.raw","wb") as raw_out:
#     for ci in range(total_chunks):
#         phase = generate_tone(2600, phase, 0.8, fs, "s16", buffer)
#         raw_out.write(buffer)
# print("finished gen test")
# sys.exit(0)
# #---- 

# Using mp2 or mkv as the container format gets the same error.
with av.open(out_filename+'.mp2', "w", format="mp2") as output_con:
    output_con.metadata["title"] = "My title"
    output_con.metadata["key"] = "value"
    channel_layout = "mono"
    sample_fmt = "s16p"

    ostream = output_con.add_stream(ffmpeg_codec_name, fs, layout=channel_layout)
    assert ostream is not None, "No stream!"
    cctx = ostream.codec_context
    cctx.sample_rate = fs
    cctx.time_base = fractions.Fraction(numerator=1,denominator=fs)
    cctx.format = sample_fmt
    cctx.channels = channels
    cctx.layout = channel_layout
    print(cctx, f"layout#{cctx.channel_layout}")
    
    # Define PyAudio-style callback for recording plus PyAV transcoding.
    def rec_callback(in_data, frame_count, time_info, status):
        global sample_len
        global ostream
        frames.append(in_data)
        nsamples = int(len(in_data) / (channels*sample_size_bytes))
        
        frame = AudioFrame(format=sample_fmt, layout=channel_layout, samples=nsamples)
        frame.sample_rate = fs
        frame.time_base = fractions.Fraction(numerator=1,denominator=fs)
        frame.pts = sample_len
        frame.planes[0].update(in_data)
        print(frame, len(in_data))
        
        for out_packet in ostream.encode(frame):
            output_con.mux(out_packet)
        for out_packet in ostream.encode(None):
            output_con.mux(out_packet)
        
        sample_len += nsamples
        retflag = pyaudio.paContinue if sample_lencode>

    


    If you uncomment the RAW output part you will find the generated data can be imported as PCM s16 Mono 44100Hz into Audacity and plays the expected tone, so the generated audio data does not seem to be the problem.

    


    The normal program console output up until the exception is :

    


    mp2 at 0x7f8e38202cf0> layout#4
Beginning
 5120
. 5120


    


    The stack trace is :

    


    Traceback (most recent call last):&#xA;&#xA;  File "Dev/multichan_recording/av_encode.py", line 147, in <module>&#xA;    ret_data, ret_flag = rec_callback(buffer, ci, {}, 1)&#xA;&#xA;  File "Dev/multichan_recording/av_encode.py", line 121, in rec_callback&#xA;    for out_packet in ostream.encode(frame):&#xA;&#xA;  File "av/stream.pyx", line 153, in av.stream.Stream.encode&#xA;&#xA;  File "av/codec/context.pyx", line 484, in av.codec.context.CodecContext.encode&#xA;&#xA;  File "av/audio/codeccontext.pyx", line 42, in av.audio.codeccontext.AudioCodecContext._prepare_frames_for_encode&#xA;&#xA;  File "av/audio/resampler.pyx", line 101, in av.audio.resampler.AudioResampler.resample&#xA;&#xA;  File "av/filter/graph.pyx", line 211, in av.filter.graph.Graph.push&#xA;&#xA;  File "av/filter/context.pyx", line 89, in av.filter.context.FilterContext.push&#xA;&#xA;  File "av/error.pyx", line 336, in av.error.err_check&#xA;&#xA;ValueError: [Errno 22] Invalid argument&#xA;&#xA;</module>

    &#xA;

    edit : It's interesting that the error happens on the 2nd AudioFrame, as apparently the first one was encoded okay, because they are given the same attribute values aside from the Presentation Time Stamp (pts), but leaving this out and letting PyAV/ffmpeg generate the PTS by itself does not fix the error, so an incorrect PTS does not seem the cause.

    &#xA;

    After a brief glance in av/filter/context.pyx the exception must come from a bad return value from res = lib.av_buffersrc_write_frame(self.ptr, frame.ptr)
    &#xA;Trying to dig into av_buffersrc_write_frame from the ffmpeg source it is not clear what could be causing this error. The only obvious one is a mismatch between channel layouts, but my code is setting the layout the same in the Stream and the Frame. That problem had been found by an old question pyav - cannot save stream as mono and their answer (that one parameter required is undocumented) is the only reason the code now has the layout='mono' argument when making the stream.

    &#xA;

    The program output shows layout #4 is being used, and from https://github.com/FFmpeg/FFmpeg/blob/release/4.2/libavutil/channel_layout.h you can see this is the value for symbol AV_CH_FRONT_CENTER which is the only channel in the MONO layout.

    &#xA;

    The mismatch is surely some other object property or an undocumented parameter requirement.

    &#xA;

    How do you encode mono audio to a compressed stream with PyAV ?

    &#xA;

  • Use FFMPEG as a demuxer-muxer when knowing all needed information about input stream

    7 février 2023, par Javierd98

    I'm working on a project where I need to use FFMEPG only as a demuxer-muxer in order to receive a MPEG source with an audio and video streams and output the different streams separately. I also need it to introduce the smallest possible delay.&#xA;In order to do this, I have all the needed information about the input : used container, audio and video codecs, audio and video rate and audio channels, so I would like to provide FFMPEG with that information and avoid the proving phase. However, I can't get FFMPEG to work without specifying a big probesize, which introduces latency, and, depending on the input, causes FFMPEG to fail due to not being able to get the needed info (which I have beforehand).

    &#xA;

    I've been researching the different options that FFMPEG provides, and I've found the following ones which could help me :

    &#xA;

      &#xA;
    • -codec:a and -codec:v to provide the information about the input's audio and video codecs.
    • &#xA;

    • -f to provide the information about the used container.
    • &#xA;

    • -r:v to provide the information about the video framerate. (I've also tried with -framerate, but it seems that is only supported by a few demuxers, and mpeg is not one of them).
    • &#xA;

    • -probesize to avoid spending time probing the input, as I can provide the inpromation.
    • &#xA;

    • -copy_unknown to simply copy the streams instead of failing if it can't be recognized.
    • &#xA;

    &#xA;

    With all this, I'm using the following command :&#xA;./ffmpeg -hide_banner -loglevel debug -codec:v h264 -codec:a pcm_alaw -flags low_delay -probesize 32 -analyzeduration 0 -r:v 15 -f mpeg -i udp://127.0.0.1:41071?listen -y -map 0:a:0? -acodec copy -copy_unknown -ar 8000 -payload_type 8 -f rtp udp://127.0.0.1:33605?pkt_size=1200 -map 0:v:0? -vcodec copy -copy_unknown -payload_type 96 -f rtp udp://127.0.0.1:48527?pkt_size=1200

    &#xA;

    Splitting the commandline.&#xA;Reading option &#x27;-hide_banner&#x27; ... matched as option &#x27;hide_banner&#x27; (do not show program banner) with argument &#x27;1&#x27;.&#xA;Reading option &#x27;-loglevel&#x27; ... matched as option &#x27;loglevel&#x27; (set logging level) with argument &#x27;debug&#x27;.&#xA;Reading option &#x27;-codec:v&#x27; ... matched as option &#x27;codec&#x27; (codec name) with argument &#x27;h264&#x27;.&#xA;Reading option &#x27;-codec:a&#x27; ... matched as option &#x27;codec&#x27; (codec name) with argument &#x27;pcm_alaw&#x27;.&#xA;Reading option &#x27;-flags&#x27; ... matched as AVOption &#x27;flags&#x27; with argument &#x27;low_delay&#x27;.&#xA;Reading option &#x27;-probesize&#x27; ... matched as AVOption &#x27;probesize&#x27; with argument &#x27;32&#x27;.&#xA;Reading option &#x27;-analyzeduration&#x27; ... matched as AVOption &#x27;analyzeduration&#x27; with argument &#x27;0&#x27;.&#xA;Reading option &#x27;-r:v&#x27; ... matched as option &#x27;r&#x27; (set frame rate (Hz value, fraction or abbreviation)) with argument &#x27;15&#x27;.&#xA;Reading option &#x27;-f&#x27; ... matched as option &#x27;f&#x27; (force format) with argument &#x27;mpeg&#x27;.&#xA;Reading option &#x27;-i&#x27; ... matched as input url with argument &#x27;udp://127.0.0.1:41071?listen&#x27;.&#xA;Reading option &#x27;-y&#x27; ... matched as option &#x27;y&#x27; (overwrite output files) with argument &#x27;1&#x27;.&#xA;Reading option &#x27;-map&#x27; ... matched as option &#x27;map&#x27; (set input stream mapping) with argument &#x27;0:a:0?&#x27;.&#xA;Reading option &#x27;-acodec&#x27; ... matched as option &#x27;acodec&#x27; (force audio codec (&#x27;copy&#x27; to copy stream)) with argument &#x27;copy&#x27;.&#xA;Reading option &#x27;-copy_unknown&#x27; ... matched as option &#x27;copy_unknown&#x27; (Copy unknown stream types) with argument &#x27;1&#x27;.&#xA;Reading option &#x27;-ar&#x27; ... matched as option &#x27;ar&#x27; (set audio sampling rate (in Hz)) with argument &#x27;8000&#x27;.&#xA;Reading option &#x27;-payload_type&#x27; ... matched as AVOption &#x27;payload_type&#x27; with argument &#x27;8&#x27;.&#xA;Reading option &#x27;-f&#x27; ... matched as option &#x27;f&#x27; (force format) with argument &#x27;rtp&#x27;.&#xA;Reading option &#x27;udp://127.0.0.1:33605?pkt_size=1200&#x27; ... matched as output url.&#xA;Reading option &#x27;-map&#x27; ... matched as option &#x27;map&#x27; (set input stream mapping) with argument &#x27;0:v:0?&#x27;.&#xA;Reading option &#x27;-vcodec&#x27; ... matched as option &#x27;vcodec&#x27; (force video codec (&#x27;copy&#x27; to copy stream)) with argument &#x27;copy&#x27;.&#xA;Reading option &#x27;-copy_unknown&#x27; ... matched as option &#x27;copy_unknown&#x27; (Copy unknown stream types) with argument &#x27;1&#x27;.&#xA;Reading option &#x27;-payload_type&#x27; ... matched as AVOption &#x27;payload_type&#x27; with argument &#x27;96&#x27;.&#xA;Reading option &#x27;-f&#x27; ... matched as option &#x27;f&#x27; (force format) with argument &#x27;rtp&#x27;.&#xA;Reading option &#x27;udp://127.0.0.1:48527?pkt_size=1200&#x27; ... matched as output url.&#xA;Finished splitting the commandline.&#xA;Parsing a group of options: global .&#xA;Applying option hide_banner (do not show program banner) with argument 1.&#xA;Applying option loglevel (set logging level) with argument debug.&#xA;Applying option y (overwrite output files) with argument 1.&#xA;Applying option copy_unknown (Copy unknown stream types) with argument 1.&#xA;    Last message repeated 1 times&#xA;Successfully parsed a group of options.&#xA;Parsing a group of options: input url udp://127.0.0.1:41071?listen.&#xA;Applying option codec:v (codec name) with argument h264.&#xA;Applying option codec:a (codec name) with argument pcm_alaw.&#xA;Applying option r:v (set frame rate (Hz value, fraction or abbreviation)) with argument 15.&#xA;Applying option f (force format) with argument mpeg.&#xA;Successfully parsed a group of options.&#xA;Opening an input file: udp://127.0.0.1:41071?listen.&#xA;[mpeg @ 0x55a769cc3d40] Opening &#x27;udp://127.0.0.1:41071?listen&#x27; for reading&#xA;[udp @ 0x55a769cc4b00] No default whitelist set&#xA;[udp @ 0x55a769cc4b00] end receive buffer size reported is 425984&#xA;[mpeg @ 0x55a769cc3d40] Before avformat_find_stream_info() pos: 6 bytes read:40 seeks:0 nb_streams:0&#xA;[mpeg @ 0x55a769cc3d40] probing stream 1 pp:2500&#xA;[mpeg @ 0x55a769cc3d40] probed stream 1&#xA;[mpeg @ 0x55a769cc3d40] parser not found for codec pcm_alaw, packets or times may be invalid.&#xA;[mpeg @ 0x55a769cc3d40] Probe buffer size limit of 32 bytes reached&#xA;[mpeg @ 0x55a769cc3d40] Stream #0: not enough frames to estimate rate; consider increasing probesize&#xA;[mpeg @ 0x55a769cc3d40] Could not find codec parameters for stream 0 (Video: h264, 1 reference frame, none): unspecified size&#xA;Consider increasing the value for the &#x27;analyzeduration&#x27; (0) and &#x27;probesize&#x27; (32) options&#xA;[mpeg @ 0x55a769cc3d40] After avformat_find_stream_info() pos: 87804 bytes read:88476 seeks:0 frames:1&#xA;Input #0, mpeg, from &#x27;udp://127.0.0.1:41071?listen&#x27;:&#xA;  Duration: N/A, start: 50436.772978, bitrate: 64 kb/s&#xA;  Stream #0:0[0x1e0], 0, 1/90000: Video: h264, 1 reference frame, none, 90k tbr, 90k tbn&#xA;  Stream #0:1[0x1c0], 1, 1/90000: Audio: pcm_alaw, 8000 Hz, mono, s16, 64 kb/s&#xA;Successfully opened the file.&#xA;Parsing a group of options: output url udp://127.0.0.1:33605?pkt_size=1200.&#xA;Applying option map (set input stream mapping) with argument 0:a:0?.&#xA;Applying option acodec (force audio codec (&#x27;copy&#x27; to copy stream)) with argument copy.&#xA;Applying option ar (set audio sampling rate (in Hz)) with argument 8000.&#xA;Applying option f (force format) with argument rtp.&#xA;Successfully parsed a group of options.&#xA;Opening an output file: udp://127.0.0.1:33605?pkt_size=1200.&#xA;[udp @ 0x55a769cf6500] No default whitelist set&#xA;Successfully opened the file.&#xA;Parsing a group of options: output url udp://127.0.0.1:48527?pkt_size=1200.&#xA;Applying option map (set input stream mapping) with argument 0:v:0?.&#xA;Applying option vcodec (force video codec (&#x27;copy&#x27; to copy stream)) with argument copy.&#xA;Applying option f (force format) with argument rtp.&#xA;Successfully parsed a group of options.&#xA;Opening an output file: udp://127.0.0.1:48527?pkt_size=1200.&#xA;[udp @ 0x55a769d08fc0] No default whitelist set&#xA;Successfully opened the file.&#xA;Output #0, rtp, to &#x27;udp://127.0.0.1:33605?pkt_size=1200&#x27;:&#xA;  Metadata:&#xA;    encoder         : Lavf59.16.100&#xA;  Stream #0:0, 0, 1/8000: Audio: pcm_alaw, 8000 Hz, mono, s16, 64 kb/s&#xA;[rtp @ 0x55a769d06f40] dimensions not set&#xA;Could not write header for output file #1 (incorrect codec parameters ?): Invalid argument&#xA;Error initializing output stream 1:0 -- &#xA;Stream mapping:&#xA;  Stream #0:1 -> #0:0 (copy)&#xA;  Stream #0:0 -> #1:0 (copy)&#xA;    Last message repeated 1 times&#xA;[AVIOContext @ 0x55a769d06d40] Statistics: 0 bytes written, 0 seeks, 0 writeouts&#xA;[AVIOContext @ 0x55a769d19800] Statistics: 0 bytes written, 0 seeks, 0 writeouts&#xA;[AVIOContext @ 0x55a769cd4fc0] Statistics: 88476 bytes read, 0 seeks&#xA;

    &#xA;

    I'm using FFMPEG 5.0.2, which is a fairly updated version.

    &#xA;

    As you can see, the -copy_unknown option is having no effect, as FFMPEG still crashes when trying to recognize the input (I've also tried specifying it on the input, but the result is the same). From the logs, I think the main problem is that FFMPEG is not interpreting the framerate correctly, which prevents it from demuxing the video. I've been searching for another way to specify it, but couldn't find it.

    &#xA;

    Is this a bug ? Or maybe 'm missing something ? Does somebody know how could I achieve my goal ?

    &#xA;

  • avfilter/phase_template : Fix left-shift of negative numbers

    6 novembre 2022, par Andreas Rheinhardt
    avfilter/phase_template : Fix left-shift of negative numbers
    

    Affected the filter-phase FATE-test.

    Signed-off-by : Andreas Rheinhardt <andreas.rheinhardt@outlook.com>

    • [DH] libavfilter/phase_template.c