Recherche avancée

Médias (16)

Mot : - Tags -/mp3

Autres articles (52)

  • Pas question de marché, de cloud etc...

    10 avril 2011

    Le vocabulaire utilisé sur ce site essaie d’éviter toute référence à la mode qui fleurit allègrement
    sur le web 2.0 et dans les entreprises qui en vivent.
    Vous êtes donc invité à bannir l’utilisation des termes "Brand", "Cloud", "Marché" etc...
    Notre motivation est avant tout de créer un outil simple, accessible à pour tout le monde, favorisant
    le partage de créations sur Internet et permettant aux auteurs de garder une autonomie optimale.
    Aucun "contrat Gold ou Premium" n’est donc prévu, aucun (...)

  • List of compatible distributions

    26 avril 2011, par

    The table below is the list of Linux distributions compatible with the automated installation script of MediaSPIP. Distribution nameVersion nameVersion number Debian Squeeze 6.x.x Debian Weezy 7.x.x Debian Jessie 8.x.x Ubuntu The Precise Pangolin 12.04 LTS Ubuntu The Trusty Tahr 14.04
    If you want to help us improve this list, you can provide us access to a machine whose distribution is not mentioned above or send the necessary fixes to add (...)

  • Formulaire personnalisable

    21 juin 2013, par

    Cette page présente les champs disponibles dans le formulaire de publication d’un média et il indique les différents champs qu’on peut ajouter. Formulaire de création d’un Media
    Dans le cas d’un document de type média, les champs proposés par défaut sont : Texte Activer/Désactiver le forum ( on peut désactiver l’invite au commentaire pour chaque article ) Licence Ajout/suppression d’auteurs Tags
    On peut modifier ce formulaire dans la partie :
    Administration > Configuration des masques de formulaire. (...)

Sur d’autres sites (8182)

  • Python-FFMPEG Corruption Problems

    11 juillet 2023, par Gabriel Ruben Guzman

    I'm repurposing some python code to generate gifs/mp4s showcasing nba player movements dot form. (With the 'frames' used in the gifs being generated by matplotlib).

    


    The repo comes with two different functions for generating the gifs, watch_play and animate_play. Both of which use python command line functionalities to run ffmpeg and generate the mp4s.
I've been able to use the watch_play succesfully, bot every time I try using animate_play, which according to the documention is meant to be significantly faster than watch play, I run into the error showcased here.(I printed the cmd string being passed into the pipe, in the hopes it would make debugging easier) Error FFMPEG

    


    I've tried generating gifs/mp4s of various size and added a decent bit of code to lessen the volume of data being processed. (I'm essentially repurposing the code just to generate clips, so I've been able to remove a lot of the pbp/tracking data logs to speed up the run time) But no matter what I've done, gotten some variation of the screenshotted error.

    


    pipe: : corrupt input packet in stream 0&#xA;[rawvideo @ 0x55ccc0e2bb80] Invalid buffer size, packet size 691200 < expected frame_size 921600&#xA;Error while decoding stream #0:0 : Invalid argument

    &#xA;

    The code for animate_play

    &#xA;

    def animate_play(self, game_time=None, length=None, highlight_player=None,&#xA;                 commentary=True, show_spacing=None):&#xA;    """&#xA;    Method for animating plays in game.&#xA;    Outputs video file of play in {cwd}/temp.&#xA;    Individual frames are streamed directly to ffmpeg without writing them&#xA;    to the disk, which is a great speed improvement over watch_play&#xA;&#xA;    Args:&#xA;        game_time (int): time in game to start video&#xA;            (seconds into the game).&#xA;            Currently game_time can also be an tuple of length two&#xA;            with (starting_frame, ending_frame)if you want to&#xA;            watch a play using frames instead of game time.&#xA;        length (int): length of play to watch (seconds)&#xA;        highlight_player (str): If not None, video will highlight&#xA;            the circle of the inputed player for easy tracking.&#xA;        commentary (bool): Whether to include play-by-play commentary in&#xA;            the animation&#xA;        show_spacing (str) in [&#x27;home&#x27;, &#x27;away&#x27;]: show convex hull&#xA;            spacing of home or away team.&#xA;            If None, does not show spacing.&#xA;&#xA;    Returns: an instance of self, and outputs video file of play&#xA;    """&#xA;    if type(game_time) == tuple:&#xA;        starting_frame = game_time[0]&#xA;        ending_frame = game_time[1]&#xA;    else:&#xA;        game_time= self.start &#x2B;(self.quarter*720)&#xA;        end_time= self.end &#x2B;(self.quarter*720)&#xA;        length = end_time-game_time&#xA;        # Get starting and ending frame from requested &#xA;        # game_time and length&#xA;        print(&#x27;hit&#x27;)&#xA;        print(len(self.moments))&#xA;        print(game_time)&#xA;        print(end_time)&#xA;        print(length)&#xA;        print(game_time&#x2B;length)&#xA;        &#xA;        print(self.moments.game_time.min())&#xA;        print(self.moments.game_time.max())&#xA;&#xA;        sys.exit()&#xA;        starting_frame = self.moments[self.moments.game_time.round() ==&#xA;                                      game_time].index.values[0]&#xA;        ending_frame = self.moments[self.moments.game_time.round() ==&#xA;                                    game_time &#x2B; length].index.values[0]&#xA;&#xA;    # Make video of each frame&#xA;    filename = "./temp/{game_time}.mp4".format(game_time=game_time)&#xA;    if commentary:&#xA;        size = (960, 960)&#xA;    else:&#xA;        size = (480, 480)&#xA;    cmdstring = (&#x27;ffmpeg&#x27;,&#xA;                 &#x27;-y&#x27;, &#x27;-r&#x27;, &#x27;20&#x27;,  # fps&#xA;                 &#x27;-s&#x27;, &#x27;%dx%d&#x27; % size,  # size of image string&#xA;                 &#x27;-pix_fmt&#x27;, &#x27;argb&#x27;,  # Stream argb data from matplotlib&#xA;                 &#x27;-f&#x27;, &#x27;rawvideo&#x27;,&#x27;-i&#x27;, &#x27;-&#x27;,&#xA;                 &#x27;-vcodec&#x27;, &#x27;libx264&#x27;, filename)&#xA;    #print(pipe)&#xA;    #print(cmdstring)&#xA;    &#xA;    &#xA;&#xA;    # Stream plots to pipe&#xA;    pipe = Popen(cmdstring, stdin=PIPE)&#xA;    print(cmdstring)&#xA;    for frame in range(starting_frame, ending_frame):&#xA;        print(frame)&#xA;        self.plot_frame(frame, highlight_player=highlight_player,&#xA;                        commentary=commentary, show_spacing=show_spacing,&#xA;                        pipe=pipe)&#xA;    print(cmdstring)&#xA;    pipe.stdin.close()&#xA;    pipe.wait()&#xA;    return self&#xA;

    &#xA;

    The code for watch play

    &#xA;

    def watch_play(self, game_time=None, length=None, highlight_player=None,&#xA;               commentary=True, show_spacing=None):&#xA;&#xA;    """&#xA;    DEPRECIATED.  See animate_play() for similar (fastere) method&#xA;&#xA;    Method for viewing plays in game.&#xA;    Outputs video file of play in {cwd}/temp&#xA;&#xA;    Args:&#xA;        game_time (int): time in game to start video&#xA;            (seconds into the game).&#xA;            Currently game_time can also be an tuple of length&#xA;            two with (starting_frame, ending_frame) if you want&#xA;            to watch a play using frames instead of game time.&#xA;        length (int): length of play to watch (seconds)&#xA;        highlight_player (str): If not None, video will highlight&#xA;            the circle of the inputed player for easy tracking.&#xA;        commentary (bool): Whether to include play-by-play&#xA;            commentary underneath video&#xA;        show_spacing (str in [&#x27;home&#x27;, &#x27;away&#x27;]): show convex hull&#xA;            of home or away team.&#xA;            if None, does not display any convex hull&#xA;&#xA;    Returns: an instance of self, and outputs video file of play&#xA;    """&#xA;    print(&#x27;hit this point &#x27;)&#xA;    warnings.warn(("watch_play is extremely slow. "&#xA;                   "Use animate_play for similar functionality, "&#xA;                   "but greater efficiency"))&#xA;&#xA;    if type(game_time) == tuple:&#xA;        starting_frame = game_time[0]&#xA;        ending_frame = game_time[1]&#xA;    else:&#xA;        # Get starting and ending frame from requested game_time and length&#xA;        game_time= self.start &#x2B;(self.quarter*720)&#xA;        end_time= self.end &#x2B;(self.quarter*720)&#xA;        length = end_time-game_time&#xA;&#xA;&#xA;        starting_frame = self.moments[self.moments.game_time.round() ==&#xA;                                      game_time].index.values[0]&#xA;        ending_frame = self.moments[self.moments.game_time.round() ==&#xA;                                    game_time &#x2B; length].index.values[0]&#xA;    #print(self.moments.head(2))&#xA;    #print(starting_frame)&#xA;    #print(ending_frame)&#xA;    print(len(self.moments))&#xA;    # Make video of each frame&#xA;    title = str(starting_frame)&#x2B;&#x27;-&#x27;&#x2B;str(ending_frame)&#xA;    for frame in range(starting_frame, ending_frame):&#xA;        print(frame)&#xA;        self.plot_frame(frame, highlight_player=highlight_player,&#xA;                        commentary=commentary, show_spacing=show_spacing)&#xA;    command = (&#x27;ffmpeg -framerate 20 -start_number {starting_frame} &#x27;&#xA;               &#x27;-i %d.png -c:v libx264 -r 30 -pix_fmt yuv420p -vf &#x27;&#xA;               &#x27;"scale=trunc(iw/2)*2:trunc(ih/2)*2" {title}&#x27;&#xA;               &#x27;.mp4&#x27;).format(starting_frame=starting_frame,title=title)&#xA;    os.chdir(&#x27;temp&#x27;)&#xA;    os.system(command)&#xA;    os.chdir(&#x27;..&#x27;)&#xA;&#xA;    # Delete images&#xA;    for file in os.listdir(&#x27;./temp&#x27;):&#xA;        if os.path.splitext(file)[1] == &#x27;.png&#x27;:&#xA;            os.remove(&#x27;./temp/{file}&#x27;.format(file=file))&#xA;&#xA;    return self&#x27;&#xA;

    &#xA;

  • Streaming Anki Vector's camera

    25 novembre 2023, par Brendan Goode

    I am trying to stream my robot to Remo.tv with my Vector robot. The website recognizes I am going live but does not stream what the robots camera is seeing. I have confirmed the camera works by a local application that runs the SDK. The very end of the code is what is giving issues, it appears somebody ripped code from Cozmo and attempted to paste it into a Vector file. The problem is it seems like the camera is taking pictures and we reach the point where it attempts to send photo but fails ?

    &#xA;

    # This is a dummy file to allow the automatic loading of modules without error on none.&#xA;import anki_vector&#xA;import atexit&#xA;import time&#xA;import _thread as thread&#xA;import logging&#xA;import networking&#xA;&#xA;log = logging.getLogger(&#x27;RemoTV.vector&#x27;)&#xA;vector = None&#xA;reserve_control = None&#xA;robotKey = None&#xA;volume = 100 #this is stupid, but who cares&#xA;annotated = False&#xA;&#xA;def connect():&#xA;    global vector&#xA;    global reserve_control&#xA;&#xA;    log.debug("Connecting to Vector")&#xA;    vector = anki_vector.AsyncRobot()&#xA;    vector.connect()&#xA;    #reserve_control = anki_vector.behavior.ReserveBehaviorControl()&#xA;    &#xA;    atexit.register(exit)&#xA;&#xA;    return(vector)&#xA;&#xA;def exit():&#xA;    log.debug("Vector exiting")&#xA;    vector.disconnect()&#xA;    &#xA;def setup(robot_config):&#xA;    global forward_speed&#xA;    global turn_speed&#xA;    global volume&#xA;    global vector&#xA;    global charge_high&#xA;    global charge_low&#xA;    global stay_on_dock&#xA;&#xA;    global robotKey&#xA;    global server&#xA;    global no_mic&#xA;    global no_camera&#xA;    global ffmpeg_location&#xA;    global v4l2_ctl_location&#xA;    global x_res&#xA;    global y_res&#xA;    &#xA;    robotKey = robot_config.get(&#x27;robot&#x27;, &#x27;robot_key&#x27;)&#xA;&#xA;    if robot_config.has_option(&#x27;misc&#x27;, &#x27;video_server&#x27;):&#xA;        server = robot_config.get(&#x27;misc&#x27;, &#x27;video_server&#x27;)&#xA;    else:&#xA;        server = robot_config.get(&#x27;misc&#x27;, &#x27;server&#x27;)&#xA; &#xA;    no_mic = robot_config.getboolean(&#x27;camera&#x27;, &#x27;no_mic&#x27;)&#xA;    no_camera = robot_config.getboolean(&#x27;camera&#x27;, &#x27;no_camera&#x27;)&#xA;&#xA;    ffmpeg_location = robot_config.get(&#x27;ffmpeg&#x27;, &#x27;ffmpeg_location&#x27;)&#xA;    v4l2_ctl_location = robot_config.get(&#x27;ffmpeg&#x27;, &#x27;v4l2-ctl_location&#x27;)&#xA;&#xA;    x_res = robot_config.getint(&#x27;camera&#x27;, &#x27;x_res&#x27;)&#xA;    y_res = robot_config.getint(&#x27;camera&#x27;, &#x27;y_res&#x27;)&#xA;&#xA;&#xA;    if vector == None:&#xA;        vector = connect()&#xA;&#xA;    #x  mod_utils.repeat_task(30, check_battery, coz)&#xA;&#xA;    if robot_config.has_section(&#x27;cozmo&#x27;):&#xA;        forward_speed = robot_config.getint(&#x27;cozmo&#x27;, &#x27;forward_speed&#x27;)&#xA;        turn_speed = robot_config.getint(&#x27;cozmo&#x27;, &#x27;turn_speed&#x27;)&#xA;        volume = robot_config.getint(&#x27;cozmo&#x27;, &#x27;volume&#x27;)&#xA;        charge_high = robot_config.getfloat(&#x27;cozmo&#x27;, &#x27;charge_high&#x27;)&#xA;        charge_low = robot_config.getfloat(&#x27;cozmo&#x27;, &#x27;charge_low&#x27;)&#xA;        stay_on_dock = robot_config.getboolean(&#x27;cozmo&#x27;, &#x27;stay_on_dock&#x27;)&#xA;&#xA;#    if robot_config.getboolean(&#x27;tts&#x27;, &#x27;ext_chat&#x27;): #ext_chat enabled, add motor commands&#xA;#        extended_command.add_command(&#x27;.anim&#x27;, play_anim)&#xA;#        extended_command.add_command(&#x27;.forward_speed&#x27;, set_forward_speed)&#xA;#        extended_command.add_command(&#x27;.turn_speed&#x27;, set_turn_speed)&#xA;#        extended_command.add_command(&#x27;.vol&#x27;, set_volume)&#xA;#        extended_command.add_command(&#x27;.charge&#x27;, set_charging)&#xA;#        extended_command.add_command(&#x27;.stay&#x27;, set_stay_on_dock)&#xA;&#xA;    vector.audio.set_master_volume(volume) # set volume&#xA;&#xA;    return&#xA;    &#xA;def move(args):&#xA;    global charging&#xA;    global low_battery&#xA;    command = args[&#x27;button&#x27;][&#x27;command&#x27;]&#xA;&#xA;    try:&#xA;        if vector.status.is_on_charger and not charging:&#xA;            if low_battery:&#xA;                print("Started Charging")&#xA;                charging = 1&#xA;            else:&#xA;                if not stay_on_dock:&#xA;                    vector.drive_off_charger_contacts().wait_for_completed()&#xA;&#xA;        if command == &#x27;f&#x27;:&#xA;            vector.behavior.say_text("Moving {}".format(command))&#xA;&#xA;            #causes delays #coz.drive_straight(distance_mm(10), speed_mmps(50), False, True).wait_for_completed()&#xA;            vector.motors.set_wheel_motors(forward_speed, forward_speed, forward_speed*4, forward_speed*4 )&#xA;            time.sleep(0.7)&#xA;            vector.motors.set_wheel_motors(0, 0)&#xA;        elif command == &#x27;b&#x27;:&#xA;            #causes delays #coz.drive_straight(distance_mm(-10), speed_mmps(50), False, True).wait_for_completed()&#xA;            vector.motors.set_wheel_motors(-forward_speed, -forward_speed, -forward_speed*4, -forward_speed*4 )&#xA;            time.sleep(0.7)&#xA;            vector.motors.set_wheel_motors(0, 0)&#xA;        elif command == &#x27;l&#x27;:&#xA;            #causes delays #coz.turn_in_place(degrees(15), False).wait_for_completed()&#xA;            vector.motors.set_wheel_motors(-turn_speed, turn_speed, -turn_speed*4, turn_speed*4 )&#xA;            time.sleep(0.5)&#xA;            vector.motors.set_wheel_motors(0, 0)&#xA;        elif command == &#x27;r&#x27;:&#xA;            #causes delays #coz.turn_in_place(degrees(-15), False).wait_for_completed()&#xA;            vector.motors.set_wheel_motors(turn_speed, -turn_speed, turn_speed*4, -turn_speed*4 )&#xA;            time.sleep(0.5)&#xA;            vector.motors.set_wheel_motors(0, 0)&#xA;&#xA;        #move lift&#xA;        elif command == &#x27;w&#x27;:&#xA;            vector.behavior.say_text("w")&#xA;            vector.set_lift_height(height=1).wait_for_completed()&#xA;        elif command == &#x27;s&#x27;:&#xA;            vector.behavior.say_text("s")&#xA;            vector.set_lift_height(height=0).wait_for_completed()&#xA;&#xA;        #look up down&#xA;        #-25 (down) to 44.5 degrees (up)&#xA;        elif command == &#x27;q&#x27;:&#xA;            #head_angle_action = coz.set_head_angle(degrees(0))&#xA;            #clamped_head_angle = head_angle_action.angle.degrees&#xA;            #head_angle_action.wait_for_completed()&#xA;            vector.behaviour.set_head_angle(45)&#xA;            time.sleep(0.35)&#xA;            vector.behaviour.set_head_angle(0)&#xA;        elif command == &#x27;a&#x27;:&#xA;            #head_angle_action = coz.set_head_angle(degrees(44.5))&#xA;            #clamped_head_angle = head_angle_action.angle.degrees&#xA;            #head_angle_action.wait_for_completed()&#xA;            vector.behaviour.set_head_angle(-22.0)&#xA;            time.sleep(0.35)&#xA;            vector.behaviour.set_head_angle(0)&#xA;   &#xA;        #things to say with TTS disabled&#xA;        elif command == &#x27;sayhi&#x27;:&#xA;            tts.say( "hi! I&#x27;m cozmo!" )&#xA;        elif command == &#x27;saywatch&#x27;:&#xA;            tts.say( "watch this" )&#xA;        elif command == &#x27;saylove&#x27;:&#xA;            tts.say( "i love you" )&#xA;        elif command == &#x27;saybye&#x27;:&#xA;            tts.say( "bye" )&#xA;        elif command == &#x27;sayhappy&#x27;:&#xA;            tts.say( "I&#x27;m happy" )&#xA;        elif command == &#x27;saysad&#x27;:&#xA;            tts.say( "I&#x27;m sad" )&#xA;        elif command == &#x27;sayhowru&#x27;:&#xA;            tts.say( "how are you?" )&#xA;    except:&#xA;        return(False)&#xA;    return&#xA;&#xA;def start():&#xA;    log.debug("Starting Vector Video Process")&#xA;    try:&#xA;        thread.start_new_thread(video, ())&#xA;    except KeyboardInterrupt as e:&#xA;        pass        &#xA;    return&#xA;    &#xA;def video():&#xA;    global vector&#xA;    # Turn on image receiving by the camera&#xA;    vector.camera.init_camera_feed()&#xA;&#xA;    vector.behavior.say_text("hey everyone, lets robot!")&#xA;&#xA;    while True:&#xA;        time.sleep(0.25)&#xA;&#xA;        from subprocess import Popen, PIPE&#xA;        from sys import platform&#xA;&#xA;        log.debug("ffmpeg location : {}".format(ffmpeg_location))&#xA;&#xA;#        import os&#xA;#        if not os.path.isfile(ffmpeg_location):&#xA;#        print("Error: cannot find " &#x2B; str(ffmpeg_location) &#x2B; " check ffmpeg is installed. Terminating controller")&#xA;#        thread.interrupt_main()&#xA;#        thread.exit()&#xA;&#xA;        while not networking.authenticated:&#xA;            time.sleep(1)&#xA;&#xA;        p = Popen([ffmpeg_location, &#x27;-y&#x27;, &#x27;-f&#x27;, &#x27;image2pipe&#x27;, &#x27;-vcodec&#x27;, &#x27;png&#x27;, &#x27;-r&#x27;, &#x27;25&#x27;, &#x27;-i&#x27;, &#x27;-&#x27;, &#x27;-vcodec&#x27;, &#x27;mpeg1video&#x27;, &#x27;-r&#x27;, &#x27;25&#x27;, "-f", "mpegts", "-headers", "\"Authorization: Bearer {}\"".format(robotKey), "http://{}:1567/transmit?name={}-video".format(server, networking.channel_id)], stdin=PIPE)&#xA;        #p = Popen([ffmpeg_location, &#x27;-nostats&#x27;, &#x27;-y&#x27;, &#x27;-f&#x27;, &#x27;image2pipe&#x27;, &#x27;-vcodec&#x27;, &#x27;png&#x27;, &#x27;-r&#x27;, &#x27;25&#x27;, &#x27;-i&#x27;, &#x27;-&#x27;, &#x27;-vcodec&#x27;, &#x27;mpeg1video&#x27;, &#x27;-r&#x27;, &#x27;25&#x27;,&#x27;-b:v&#x27;, &#x27;400k&#x27;, "-f","mpegts", "-headers", "\"Authorization: Bearer {}\"".format(robotKey), "http://{}/transmit?name=rbot-390ddbe0-f1cc-4710-b3f1-9f477f4875f9-video".format(server)], stdin=PIPE)&#xA;        #p = Popen([ffmpeg_location, &#x27;-y&#x27;, &#x27;-f&#x27;, &#x27;image2pipe&#x27;, &#x27;-vcodec&#x27;, &#x27;png&#x27;, &#x27;-r&#x27;, &#x27;25&#x27;, &#x27;-i&#x27;, &#x27;-&#x27;, &#x27;-vcodec&#x27;, &#x27;mpeg1video&#x27;, &#x27;-r&#x27;, &#x27;25&#x27;, "-f", "mpegts", "-headers", "\"Authorization: Bearer {}\"".format(robotKey), "http://{}/transmit?name=rbot-390ddbe0-f1cc-4710-b3f1-9f477f4875f9-video".format(server, networking.channel_id)], stdin=PIPE)&#xA;        print(vector)&#xA;        image = vector.camera.latest_image&#xA;        image.raw_image.save("test.png", &#x27;PNG&#x27;)&#xA;        try:&#xA;            while True:&#xA;                if vector:&#xA;                    image = vector.camera.latest_image&#xA;                    if image:&#xA;                        if annotated:&#xA;                            image = image.annotate_image()&#xA;                        else:&#xA;                            image = image.raw_image&#xA;                        print("attempting to write image")&#xA;                        image.save(p.stdin, &#x27;PNG&#x27;)&#xA;&#xA;                else:&#xA;                    time.sleep(.1)&#xA;            log.debug("Lost Vector object, terminating video stream")&#xA;            p.stdin.close()&#xA;            p.wait()&#xA;        except Exception as e:&#xA;            log.debug("Vector Video Exception! {}".format(e))&#xA;            p.stdin.close()&#xA;            p.wait()&#xA;            pass               &#xA;    &#xA;

    &#xA;

    Here is the error we get

    &#xA;

    [vost#0:0/mpeg1video @ 000001c7153c1cc0] Error submitting a packet to the muxer: Error number -10053 occurred&#xA;[out#0/mpegts @ 000001c713448480] Error muxing a packet&#xA;[out#0/mpegts @ 000001c713448480] Error writing trailer: Error number -10053 occurred&#xA;[http @ 000001c7134cab00] URL read error: Error number -10053 occurred&#xA;[out#0/mpegts @ 000001c713448480] Error closing file: Error number -10053 occurred&#xA;[out#0/mpegts @ 000001c713448480] video:56kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown&#xA;frame=   25 fps=0.0 q=2.0 Lsize=      53kB time=00:00:01.32 bitrate= 325.9kbits/s speed=7.05x&#xA;Conversion failed!&#xA;&#xA;attempting to write image&#xA;

    &#xA;

    You can see our attempts to fix by commented out code in the #p section at the bottom.

    &#xA;

  • Read existing MP4 File and write into a new MP4 file using libavcodec

    4 octobre 2023, par Tahfimul

    I am new to the libavcodec space.

    &#xA;

    I am trying to read video and audio streams from an existing mp4 file and take the data from the two streams and then mux the two streams and write the muxed data into a new mp4 file using libavcodec in C++. Essentially, I am aiming to split the original (existing) mp4 file into small chunks of 1 second clips that then can be played back using a video player. I would like to preserve the original mp4 video's video stream (i.e. preserve its color, resolution and etc.) and preserve the mp4 video's audio stream (i.e. preserve its bit rate and etc.). I am trying to achieve this using libavcodec in C++. But there does not seem to be any tutorial or documentation online that points me to that direction.

    &#xA;

    So far, I have looked at and tried to implement a solution using this tutorial (tutorial#1) : https://github.com/leandromoreira/ffmpeg-libav-tutorial/blob/master/0_hello_world.c

    &#xA;

    However, tutorial#1 aimed to save each video frame from the existing (original) mp4 video stream into individual .pgm files, which meant that the .pgm files would store a grayscale image of each video frame.

    &#xA;

    Since, I want to preserve the colors of the original (existing) mp4 file, I looked at this tutorial (tutorial#2) that aimed to convert the grayscale video frame into color using the swscale library : https://www.youtube.com/watch?v=Y7SUm7Xf1sc&ab_channel=Bartholomew&#xA;However, in tutorial#2, they exported the output from swscale library to a GUI library to be viewed in a GUI application and did not show hwo to write the output data into a new mp4 file that can be played back by a video player.

    &#xA;

    So then, I looked at this tutorial(tutorial#3) which showed how to create an MP4 file using libavcodec : C++ FFmpeg create mp4 file&#xA;However, the problem with that solution is that I was not able to take a video frame from the original mp4 video and store it into another mp4 file. I kept getting errors when attempting to do so and I did not succeed in taking the data from the original(existing) mp4 file and storing it into a new mp4 file.

    &#xA;

    Here is the code that I have written so far :

    &#xA;

    #include<fstream>&#xA;#include &#xA;#include &#xA;#include &#xA;extern "C"&#xA;{&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>mathematics.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;#include <libavfilter></libavfilter>buffersrc.h>&#xA;#include <libavfilter></libavfilter>buffersink.h>&#xA;#include <libavutil></libavutil>time.h>&#xA;#include <libavutil></libavutil>opt.h>&#xA;}&#xA;#pragma comment(lib, "avfilter.lib")&#xA;#ifdef av_err2str&#xA;#undef av_err2str&#xA;#include <string>&#xA;av_always_inline std::string av_err2string(int errnum) {&#xA;    char str[AV_ERROR_MAX_STRING_SIZE];&#xA;    return av_make_error_string(str, AV_ERROR_MAX_STRING_SIZE, errnum);&#xA;}&#xA;#define av_err2str(err) av_err2string(err).c_str()&#xA;#endif  // av_err2str&#xA;&#xA;#include <chrono>&#xA;#include <thread>&#xA;&#xA;&#xA;// decode packets into frames&#xA;static int decode_packet(AVPacket *pPacket, AVCodecContext *pCodecContext, AVFrame *pFrame);&#xA;&#xA;static void pushFrame(AVFrame* frame, AVCodecContext* outputCodecContext, AVPacket * outputPacket, AVFormatContext* outputFormatContext, AVCodec *outputCodec) {&#xA;    &#xA;    std::cout&lt;&lt;"outputCodecContext: "&lt;format = AV_PIX_FMT_YUV420P;&#xA;        frame->width = 800;&#xA;        frame->height = 800;&#xA;        if ((err = av_frame_get_buffer(frame, 32)) &lt; 0) {&#xA;            std::cout &lt;&lt; "Failed to allocate picture" &lt;&lt; err &lt;&lt; std::endl;&#xA;            return;&#xA;        }&#xA;    }&#xA;    SwsContext* swsCtx = nullptr;&#xA;    if (!swsCtx) {&#xA;        swsCtx = sws_getContext(800, 800, AV_PIX_FMT_RGB24, 800, &#xA;            800, AV_PIX_FMT_YUV420P, SWS_BICUBIC, 0, 0, 0);&#xA;    }&#xA;    int inLinesize[1] = { 3 * 800 };&#xA;    // From RGB to YUV&#xA;    // sws_scale(swsCtx, (const uint8_t* const*)&amp;data, inLinesize, 0, 800, &#xA;        // frame->data, frame->linesize);&#xA;    std::cout&lt;&lt;"frame "&lt;pts = (1.0 / 30.0) * 90000 * (1);&#xA;    // std::cout &lt;&lt; videoFrame->pts &lt;&lt; " " &lt;&lt; cctx->time_base.num &lt;&lt; " " &lt;&lt; &#xA;    //     cctx->time_base.den &lt;&lt; " " &lt;&lt; 1 &lt;&lt; std::endl;&#xA;    if ((err = avcodec_send_frame(outputCodecContext, frame)) &lt; 0) {&#xA;        std::cout &lt;&lt; "Failed to send frame" &lt;&lt; err &lt;&lt; std::endl;&#xA;        return;&#xA;    }&#xA;    AV_TIME_BASE;&#xA;    AVPacket pkt;&#xA;    av_init_packet(&amp;pkt);&#xA;    pkt.data = NULL;&#xA;    pkt.size = 0;&#xA;    pkt.flags |= AV_PKT_FLAG_KEY;&#xA;    std::cout&lt;&lt;"here\n";&#xA;    if (avcodec_receive_packet(outputCodecContext, outputPacket) == 0) {&#xA;        static int counter = 0;&#xA;        if (counter == 0) {&#xA;            FILE* fp = fopen("dump_first_frame1.dat", "wb");&#xA;            fwrite(outputPacket->data, outputPacket->size, 1, fp);&#xA;            fclose(fp);&#xA;        }&#xA;        // std::cout &lt;&lt; "pkt key: " &lt;&lt; (pkt.flags &amp; AV_PKT_FLAG_KEY) &lt;&lt; " " &lt;&lt; &#xA;        //     pkt.size &lt;&lt; " " &lt;&lt; (counter&#x2B;&#x2B;) &lt;&lt; std::endl;&#xA;        // uint8_t* size = ((uint8_t*)pkt.data);&#xA;        // std::cout &lt;&lt; "first: " &lt;&lt; (int)size[0] &lt;&lt; " " &lt;&lt; (int)size[1] &lt;&lt; &#xA;        //     " " &lt;&lt; (int)size[2] &lt;&lt; " " &lt;&lt; (int)size[3] &lt;&lt; " " &lt;&lt; (int)size[4] &lt;&lt; &#xA;        //     " " &lt;&lt; (int)size[5] &lt;&lt; " " &lt;&lt; (int)size[6] &lt;&lt; " " &lt;&lt; (int)size[7] &lt;&lt; &#xA;        //     std::endl;&#xA;        av_interleaved_write_frame(outputFormatContext, outputPacket);&#xA;        av_packet_unref(outputPacket);&#xA;    }&#xA;}&#xA;&#xA;int main()&#xA;{&#xA;&#xA;    char* filename = "c&#x2B;&#x2B;.mp4";&#xA;&#xA;    AVFormatContext *pFormatContext = avformat_alloc_context();&#xA;&#xA;    AVOutputFormat* outputFormat = NULL;&#xA;&#xA;    AVFormatContext* outputFormatContext = nullptr;&#xA;&#xA;    AVCodecContext* outputCodecContext = nullptr;&#xA;&#xA;    if (!pFormatContext) {&#xA;        std::cerr&lt;&lt;"ERROR could not allocate memory for Format Context\n";&#xA;        return -1;&#xA;    }&#xA;&#xA;    if (avformat_open_input(&amp;pFormatContext, filename , NULL, NULL) != 0) {&#xA;        std::cerr&lt;&lt;"ERROR could not open the file\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;    std::cout&lt;&lt;"format: "&lt;iformat->name&lt;&lt;" , duration:"&lt;&lt;(double)(pFormatContext->duration/AV_TIME_BASE)&lt;&lt;"seconds, bit_rate:"&lt;bit_rate&lt;video_codec);&#xA;&#xA;    &#xA;    if (!outputCodec)&#xA;    {&#xA;        std::cout &lt;&lt; "can&#x27;t create output codec" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }   &#xA;    &#xA;&#xA;    AVStream* outputStream = avformat_new_stream(outputFormatContext, outputCodec);&#xA;&#xA;    if (!outputStream)&#xA;    {&#xA;        std::cout &lt;&lt; "can&#x27;t find output format" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    outputCodecContext = avcodec_alloc_context3(outputCodec);&#xA;&#xA;    if (!outputCodecContext)&#xA;    {&#xA;        std::cout &lt;&lt; "can&#x27;t create output codec context" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    AVCodec *pCodec = NULL;&#xA;&#xA;    AVCodecParameters *pCodecParameters =  NULL;&#xA;&#xA;    int video_stream_index = -1;&#xA;&#xA;    AVStream* stream = NULL;&#xA;    &#xA;    // loop though all the streams and print its main information&#xA;    for (int i = 0; i &lt; pFormatContext->nb_streams; i&#x2B;&#x2B;)&#xA;     {&#xA;        &#xA;        AVCodecParameters *pLocalCodecParameters =  NULL;&#xA;        pLocalCodecParameters = pFormatContext->streams[i]->codecpar;&#xA;&#xA;        AVCodec *pLocalCodec = NULL;&#xA;        pLocalCodec = avcodec_find_decoder(pLocalCodecParameters->codec_id);&#xA;        if (pLocalCodec==NULL) {&#xA;            std::cerr&lt;&lt;"ERROR unsupported codec!\n";&#xA;                // In this example if the codec is not found we just skip it&#xA;                continue;&#xA;            }&#xA;&#xA;&#xA;        if (pLocalCodecParameters->codec_type == AVMEDIA_TYPE_VIDEO) {&#xA;                if (video_stream_index == -1) {&#xA;                    video_stream_index = i;&#xA;                    pCodec = pLocalCodec;&#xA;                    pCodecParameters = pLocalCodecParameters;&#xA;                    stream = pFormatContext->streams[i];&#xA;                    std::cout&lt;&lt;"codec id: "&lt;codecpar->codec_id&lt;codecpar->codec_type&lt;codecpar->width&lt;codecpar->height&lt;codecpar->format&lt;codecpar->bit_rate&lt;codecpar->codec_id = outputFormat->video_codec;&#xA;                    // outputStream->codecpar->codec_id = stream->codecpar->codec_id;&#xA;                    outputStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;                    outputStream->codecpar->width = stream->codecpar->width;&#xA;                    outputStream->codecpar->height = stream->codecpar->height;&#xA;                    outputStream->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;                    outputStream->codecpar->bit_rate = stream->codecpar->bit_rate;&#xA;                    &#xA;                    avcodec_parameters_to_context(outputCodecContext, outputStream->codecpar);&#xA;                }       &#xA;&#xA;                std::cout&lt;&lt;"Video Codec: resolution " &lt;&lt; pLocalCodecParameters->width &lt;&lt; " x "&lt;height&lt;codec_type == AVMEDIA_TYPE_AUDIO) {&#xA;                std::cout&lt;&lt;"Audio Codec: "&lt;channels&lt;&lt;" channels, sample rate "&lt;sample_rate&lt;name &lt;&lt; " ID: " &lt;id&lt;&lt; " bit_rate: "&lt;bit_rate&lt;/ outputStream->codecpar->codec_id = outputFormat->video_codec;&#xA;    // outputStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;    // outputStream->codecpar->width = 300;&#xA;    // outputStream->codecpar->height = 300;&#xA;    // outputStream->codecpar->format = AV_PIX_FMT_YUV420P;&#xA;    // outputStream->codecpar->bit_rate = 200 * 1000;&#xA;    outputCodecContext->time_base = (AVRational){ 1, 1 };&#xA;    outputCodecContext->max_b_frames = 2;&#xA;    outputCodecContext->gop_size = 12;&#xA;    outputCodecContext->framerate = (AVRational){ 30, 1 };&#xA;&#xA;    if (avcodec_parameters_to_context(pCodecContext, pCodecParameters) &lt; 0)&#xA;    {&#xA;        std::cerr&lt;&lt;"failed to copy codec params to codec context\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;    // std::cout&lt;&lt;"pCodecContext->time_base: "&lt;time_base)&lt;/ outputCodecContext->time_base = pCodecContext->time_base;&#xA;    // outputCodecContext->max_b_frames = pCodecContext->max_b_frames;&#xA;    // outputCodecContext->gop_size = pCodecContext->gop_size;&#xA;    // outputCodecContext->framerate = pCodecContext->framerate;&#xA;&#xA;    if (outputStream->codecpar->codec_id == AV_CODEC_ID_H264) {&#xA;        // av_opt_set(pCodecContext, "preset", "ultrafast", 0);&#xA;        av_opt_set(outputCodecContext, "preset", "ultrafast", 0);&#xA;    }&#xA;    else if (outputStream->codecpar->codec_id == AV_CODEC_ID_H265)&#xA;    {&#xA;        // av_opt_set(pCodecContext, "preset", "ultrafast", 0);&#xA;        av_opt_set(outputCodecContext, "preset", "ultrafast", 0);&#xA;    }&#xA;&#xA;    // avcodec_parameters_from_context(stream->codecpar, pCodecContext);&#xA;    avcodec_parameters_from_context(outputStream->codecpar, outputCodecContext);&#xA;&#xA;    if (avcodec_open2(pCodecContext, pCodec, NULL) &lt; 0)&#xA;    {&#xA;        std::cerr&lt;&lt;"failed to open codec through avcodec_open2\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;    if (avcodec_open2(outputCodecContext, outputCodec, NULL) &lt; 0)&#xA;    {&#xA;        std::cerr&lt;&lt;"failed to open output codec through avcodec_open2\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;&#xA;    if (!(outputFormat->flags &amp; AVFMT_NOFILE)) {&#xA;        if (avio_open(&amp;outputFormatContext->pb, "test.mp4", AVIO_FLAG_WRITE) &lt; 0) {&#xA;            std::cout &lt;&lt; "Failed to open file" &lt;&lt; std::endl;&#xA;            return -1;&#xA;        }&#xA;    }&#xA;&#xA;    if (avformat_write_header(outputFormatContext, NULL) &lt; 0) {&#xA;        std::cout &lt;&lt; "Failed to write header" &lt;&lt; std::endl;&#xA;        return -1;&#xA;    }&#xA;&#xA;    av_dump_format(outputFormatContext, 0, "test.mp4", 1);&#xA;&#xA;&#xA;    AVFrame *pFrame = av_frame_alloc();&#xA;    if (!pFrame)&#xA;    {&#xA;        std::cerr&lt;&lt;"failed to allocate memory for AVFrame\n";&#xA;            return -1;&#xA;    }&#xA;    &#xA;    // https://ffmpeg.org/doxygen/trunk/structAVPacket.html&#xA;    AVPacket *pPacket = av_packet_alloc();&#xA;    if (!pPacket)&#xA;    {&#xA;            std::cerr&lt;&lt;"failed to allocate memory for AVPacket\n";&#xA;            return -1;&#xA;    }&#xA;&#xA;    int response = 0;&#xA;    int how_many_packets_to_process = 300;&#xA;&#xA;    // fill the Packet with data from the Stream&#xA;    // https://ffmpeg.org/doxygen/trunk/group__lavf__decoding.html#ga4fdb3084415a82e3810de6ee60e46a61&#xA;    while (av_read_frame(pFormatContext, pPacket) >= 0)&#xA;    {&#xA;            // if it&#x27;s the video stream&#xA;            if (pPacket->stream_index == video_stream_index) {&#xA;            std::cout&lt;&lt;"AVPacket->pts "&lt;pts;&#xA;                // if(av_write_frame(outputFormatContext, pPacket)&lt;0)&#xA;                //  std::cout&lt;&lt;"error writing output frame\n";&#xA;                // pushFrame(pFrame, outputCodecContext, pPacket, outputFormatContext, outputCodec);&#xA;                response = decode_packet(pPacket, pCodecContext, pFrame);&#xA;                if (response &lt; 0)&#xA;                    break;&#xA;                // stop it, otherwise we&#x27;ll be saving hundreds of frames&#xA;                if (--how_many_packets_to_process &lt;= 0) break;&#xA;            }&#xA;            // https://ffmpeg.org/doxygen/trunk/group__lavc__packet.html#ga63d5a489b419bd5d45cfd09091cbcbc2&#xA;            av_packet_unref(pPacket);&#xA;    }   &#xA;&#xA;    if(av_write_trailer(outputFormatContext)&lt;0)&#xA;        std::cout &lt;&lt;"Error writing output trailer\n";&#xA;&#xA;&#xA;    return 0;&#xA;}&#xA;&#xA;int save_frame_as_mpeg(AVCodecContext* pCodecCtx, AVFrame* pFrame, int FrameNo) {&#xA;    int ret = 0;&#xA;&#xA;    const AVCodec* mpegCodec = avcodec_find_encoder(pCodecCtx->codec_id);&#xA;    if (!mpegCodec) {&#xA;        std::cout&lt;&lt;"failed to open mpegCodec\n";&#xA;        return -1;&#xA;    }&#xA;    AVCodecContext* mpegContext = avcodec_alloc_context3(mpegCodec);&#xA;    if (!mpegContext) {&#xA;        std::cout&lt;&lt;"failed to open mpegContext\n";&#xA;        return -1;&#xA;    }&#xA;&#xA;    mpegContext->pix_fmt = pCodecCtx->pix_fmt;&#xA;    mpegContext->height = pFrame->height;&#xA;    mpegContext->width = pFrame->width;&#xA;    mpegContext->time_base = AVRational{ 1,10 };&#xA;&#xA;    ret = avcodec_open2(mpegContext, mpegCodec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;    FILE* MPEGFile;&#xA;    char MPEGFName[256];&#xA;&#xA;    AVPacket packet;&#xA;    packet.data = NULL;&#xA;    packet.size = 0;&#xA;    av_init_packet(&amp;packet);&#xA;&#xA;    int gotFrame;&#xA;&#xA;    ret = avcodec_send_frame(mpegContext, pFrame);&#xA;    if (ret &lt; 0) {&#xA;        std::cout&lt;&lt;"failed to send frame for mpegContext\n";&#xA;        return ret;&#xA;    }&#xA;&#xA;    ret = avcodec_receive_packet(mpegContext, &amp;packet);&#xA;    if (ret &lt; 0) {&#xA;        std::cout&lt;&lt;"failed to receive packet for mpegContext\terrocode: "&lt;pix_fmt = pCodecCtx->pix_fmt;&#xA;    jpegContext->height = pFrame->height;&#xA;    jpegContext->width = pFrame->width;&#xA;    jpegContext->time_base = AVRational{ 1,10 };&#xA;&#xA;    ret = avcodec_open2(jpegContext, jpegCodec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;    FILE* JPEGFile;&#xA;    char JPEGFName[256];&#xA;&#xA;    AVPacket packet;&#xA;    packet.data = NULL;&#xA;    packet.size = 0;&#xA;    av_init_packet(&amp;packet);&#xA;&#xA;    int gotFrame;&#xA;&#xA;    ret = avcodec_send_frame(jpegContext, pFrame);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;&#xA;    ret = avcodec_receive_packet(jpegContext, &amp;packet);&#xA;    if (ret &lt; 0) {&#xA;        return ret;&#xA;    }&#xA;&#xA;    sprintf(JPEGFName, "c:\\folder\\dvr-%06d.jpg", FrameNo);&#xA;    JPEGFile = fopen(JPEGFName, "wb");&#xA;    fwrite(packet.data, 1, packet.size, JPEGFile);&#xA;    fclose(JPEGFile);&#xA;&#xA;    av_packet_unref(&amp;packet);&#xA;    avcodec_close(jpegContext);&#xA;    return 0;&#xA;}&#xA;&#xA;static int decode_packet(AVPacket *pPacket, AVCodecContext *pCodecContext, AVFrame *pFrame)&#xA;{&#xA;  // Supply raw packet data as input to a decoder&#xA;  // https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga58bc4bf1e0ac59e27362597e467efff3&#xA;  int response = avcodec_send_packet(pCodecContext, pPacket);&#xA;  if (response &lt; 0) {&#xA;      std::cerr&lt;&lt;"Error while sending a packet to the decoder: "&lt;= 0)&#xA;  {&#xA;    // Return decoded output data (into a frame) from a decoder&#xA;    // https://ffmpeg.org/doxygen/trunk/group__lavc__decoding.html#ga11e6542c4e66d3028668788a1a74217c&#xA;    response = avcodec_receive_frame(pCodecContext, pFrame);&#xA;    if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {&#xA;      break;&#xA;    } else if (response &lt; 0) {&#xA;        std::cerr&lt;&lt;"Error while receiving a frame from the decoder: "&lt;= 0) {&#xA;&#xA;      response = save_frame_as_jpeg(pCodecContext, pFrame, pCodecContext->frame_number);&#xA;&#xA;      if(response&lt;0)&#xA;      {&#xA;        std::cerr&lt;&lt;"Failed to save frame as jpeg\n";&#xA;        return -1;&#xA;      }&#xA;&#xA;      response = save_frame_as_mpeg(pCodecContext, pFrame, pCodecContext->frame_number);&#xA;&#xA;      if(response&lt;0)&#xA;      {&#xA;        std::cerr&lt;&lt;"Failed to save frame as mpeg\n";&#xA;        return -1;&#xA;      }&#xA;&#xA;&#xA;     std::cout&lt;&lt;&#xA;          "Frame "&lt;frame_number&lt;&lt; "type= "&lt;pict_type)&lt;&lt;" size= "&lt;pkt_size&lt;&lt;" bytes, format= "&lt;format&lt;&lt;" "&lt;pts&lt;&lt;"pts key_frame "&lt;key_frame&lt;&lt; " [DTS"&lt;coded_picture_number&lt;&lt;" ]\n";&#xA;      &#xA;      char frame_filename[1024];&#xA;      snprintf(frame_filename, sizeof(frame_filename), "%s-%d.pgm", "frame", pCodecContext->frame_number);&#xA;      // Check if the frame is a planar YUV 4:2:0, 12bpp&#xA;      // That is the format of the provided .mp4 file&#xA;      // RGB formats will definitely not give a gray image&#xA;      // Other YUV image may do so, but untested, so give a warning&#xA;      if (pFrame->format != AV_PIX_FMT_YUV420P)&#xA;      {&#xA;          std::cout&lt;&lt;"Warning: the generated file may not be a grayscale image, but could e.g. be just the R component if the video format is RGB\n";&#xA;      }&#xA;   &#xA;    }&#xA;  }&#xA;  return 0;&#xA;}&#xA;</thread></chrono></string></fstream>

    &#xA;

    The question that I am seeking an answer to is How can I use libavcodec to split an mp4 file into 1 second chunk clips (those clips will be in mp4 format) ?

    &#xA;