Recherche avancée

Médias (91)

Autres articles (62)

  • Gestion des droits de création et d’édition des objets

    8 février 2011, par

    Par défaut, beaucoup de fonctionnalités sont limitées aux administrateurs mais restent configurables indépendamment pour modifier leur statut minimal d’utilisation notamment : la rédaction de contenus sur le site modifiables dans la gestion des templates de formulaires ; l’ajout de notes aux articles ; l’ajout de légendes et d’annotations sur les images ;

  • Des sites réalisés avec MediaSPIP

    2 mai 2011, par

    Cette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
    Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page.

  • Dépôt de média et thèmes par FTP

    31 mai 2013, par

    L’outil MédiaSPIP traite aussi les média transférés par la voie FTP. Si vous préférez déposer par cette voie, récupérez les identifiants d’accès vers votre site MédiaSPIP et utilisez votre client FTP favori.
    Vous trouverez dès le départ les dossiers suivants dans votre espace FTP : config/ : dossier de configuration du site IMG/ : dossier des média déjà traités et en ligne sur le site local/ : répertoire cache du site web themes/ : les thèmes ou les feuilles de style personnalisées tmp/ : dossier de travail (...)

Sur d’autres sites (6205)

  • Rails using stremio-ffmpeg gem to watermark a movie while uploading

    16 juin 2016, par Felix

    I am using carrierwave to upload a movie to amazon s3 that works perfect.

    Now I want to add a watermark to the movie while uploading or after uploading, I don’t know, what’s the best way ?

    I tried this :

    movie_controller.rb action uploadVideo

    movie = FFMPEG::Movie.new(@vid.video.url)

    puts "........................................"
    puts movie.inspect

    if @vid.save

    Just to find out if the video is catched from stremio.

    But then I got the error that the movie is not found, the problem is that it doesn’t look in the amazon s3 bucket, it looks on my local server

    No such file or directory - the file '/uploads/tmp/1462954331-3471-8766/VID-20160424-WA0013.mp4' does not exist

    What could be the solution ? or should I do this in carrierwave uploader ?

    UPDATE :

    When I do this after the .save action it looks at amazon s3 bucket .. but also says that the movie isn’t there. But if I call it directly in browser it’s displayed.

    UPDATE Code :

    #Laedt ein Video hoch
    def uploadMovie
     @user = User.find_by_id session[:user_id]
     #Holt alle Channels für die er eine Berechtigung hat (Eingeloggter user)
     @user = User.find_by_id session[:user_id]
     @knowledgeproviderList = @user.knowledgeprovider
     @channels = Channel.where(knowledgeprovider_id:     @knowledgeproviderList.pluck(:id))
     @vid = Movie.new(movies_params)
     @channel = Channel.find(params[:vid][:channel_id])
     @vid.channel = @channel

     #Fügt dem Movie einen Tag hinzu
     createTag params
     createCategory params



     if @vid.save
       flash[:notice] = t("flash.saved")

       #movie = FFMPEG::Movie.new(@vid.video.url)

       #puts "........................................"
       #puts movie.inspect

       redirect_to :action => :add
      else
        redirect_to :action => :add
     end
    end

    Update error :

    movie = FFMPEG::Movie.new(@vid.video.current_path)

    options = {watermark: "mages/header.png", resolution: "640x360", watermark_filter: { position: "RT", padding_x: 10, padding_y: 10 } }
    movie.transcode("movie.flv", options)

    enter image description here

    Update :
    Added the code in the uploader :

     process :watermark_movie

        def watermark_movie
          if self.file.path
                options = {watermark: "images/header.png", resolution: "640x360", watermark_filter: { position: "RT", padding_x: 10, padding_y: 10 } }
                self.model.file = FFMPEG::Movie.new(self.file.path).transcode("#{root}/#{cache_dir}/#{self.cache_id}/file.mp4", options)
           end
         end

    Error :

    multi_json (1.12.0) lib/multi_json/adapter.rb:19:in `load'
    multi_json (1.12.0) lib/multi_json.rb:122:in `load'
    streamio-ffmpeg (2.0.0) lib/ffmpeg/movie.rb:28:in `initialize'
    app/uploaders/movie_uploader.rb:40:in `new'
    app/uploaders/movie_uploader.rb:40:in `watermark_movie'
    carrierwave (0.11.2) lib/carrierwave/uploader/processing.rb:84:in `block in process!'
    carrierwave (0.11.2) lib/carrierwave/uploader/processing.rb:76:in `each'
    carrierwave (0.11.2) lib/carrierwave/uploader/processing.rb:76:in `process!'
    carrierwave_backgrounder (0.4.2) lib/backgrounder/delay.rb:14:in `process!'
    carrierwave (0.11.2) lib/carrierwave/uploader/callbacks.rb:18:in `block in with_callbacks'
    carrierwave (0.11.2) lib/carrierwave/uploader/callbacks.rb:18:in `each'
    carrierwave (0.11.2) lib/carrierwave/uploader/callbacks.rb:18:in `with_callbacks'
    carrierwave (0.11.2) lib/carrierwave/uploader/cache.rb:134:in `cache!'
    carrierwave (0.11.2) lib/carrierwave/mount.rb:329:in `cache'
    carrierwave (0.11.2) lib/carrierwave/mount.rb:163:in `video='
    carrierwave (0.11.2) lib/carrierwave/orm/activerecord.rb:39:in `video='
    activerecord (4.2.1) lib/active_record/attribute_assignment.rb:54:in `public_send'
    activerecord (4.2.1) lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
    activerecord (4.2.1) lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
    actionpack (4.2.1) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
    actionpack (4.2.1) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
    activerecord (4.2.1) lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
    activerecord (4.2.1) lib/active_record/core.rb:559:in `init_attributes'
    activerecord (4.2.1) lib/active_record/core.rb:281:in `initialize'
    activerecord (4.2.1) lib/active_record/inheritance.rb:61:in `new'
    activerecord (4.2.1) lib/active_record/inheritance.rb:61:in `new'
    app/controllers/movies_controller.rb:71:in `uploadMovie'
    actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    actionpack (4.2.1) lib/abstract_controller/base.rb:198:in `process_action'
    actionpack (4.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
    actionpack (4.2.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
    activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
    activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
    activesupport (4.2.1) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
    activesupport (4.2.1) lib/active_support/callbacks.rb:505:in `call'
    activesupport (4.2.1) lib/active_support/callbacks.rb:505:in `call'
    activesupport (4.2.1) lib/active_support/callbacks.rb:92:in `_run_callbacks'
    activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
    activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (4.2.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
    actionpack (4.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
    actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
    activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
    activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
    actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
    actionpack (4.2.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
    activerecord (4.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
    actionpack (4.2.1) lib/abstract_controller/base.rb:137:in `process'
    actionview (4.2.1) lib/action_view/rendering.rb:30:in `process'
    actionpack (4.2.1) lib/action_controller/metal.rb:196:in `dispatch'
    actionpack (4.2.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
    actionpack (4.2.1) lib/action_controller/metal.rb:237:in `block in action'
    actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `call'
    actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
    actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:43:in `serve'
    actionpack (4.2.1) lib/action_dispatch/journey/router.rb:43:in `block in serve'
    actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `each'
    actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `serve'
    actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:819:in `call'
    rack (1.6.4) lib/rack/etag.rb:24:in `call'
    rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
    rack (1.6.4) lib/rack/head.rb:13:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/flash.rb:260:in `call'
    rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
    rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
    activerecord (4.2.1) lib/active_record/query_cache.rb:36:in `call'
    activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
    activerecord (4.2.1) lib/active_record/migration.rb:378:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
    activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `call'
    activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `_run_callbacks'
    activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
    activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
    web-console (2.3.0) lib/web_console/middleware.rb:20:in `block in call'
    web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
    web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
    railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
    railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
    activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
    activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
    railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
    rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
    rack (1.6.4) lib/rack/runtime.rb:18:in `call'
    activesupport (4.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    rack (1.6.4) lib/rack/lock.rb:17:in `call'
    actionpack (4.2.1) lib/action_dispatch/middleware/static.rb:113:in `call'
    rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
    railties (4.2.1) lib/rails/engine.rb:518:in `call'
    railties (4.2.1) lib/rails/application.rb:164:in `call'
    rack (1.6.4) lib/rack/lock.rb:17:in `call'
    rack (1.6.4) lib/rack/content_length.rb:15:in `call'
    rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
    /home/felix/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
    /home/felix/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
    /home/felix/.rvm/rubies/ruby-2.0.0-p643/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

    UPDATE — Movie Uploader

    # encoding: utf-8
    class MovieUploader < CarrierWave::Uploader::Base

     # Include RMagick or MiniMagick support:
     # include CarrierWave::RMagick
     include CarrierWave::MiniMagick
     include CarrierWave::Video
     include CarrierWave::Video::Thumbnailer
     include CarrierWave::Backgrounder::Delay

     require 'rubygems'
     require 'streamio-ffmpeg'

     # Choose what kind of storage to use for this uploader:
     storage :fog

     # Override the directory where uploaded files will be stored.
     # This is a sensible default for uploaders that are meant to be mounted:
     def store_dir
       "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
     end

     process :watermark_movie

     def watermark_movie
       puts "ssssssssssssssssssssssssssssssssss"
       puts self.file.inspect

       if self.file.path
         options = {watermark: "http://felix-hohlwegler.de/holz-soft/include/designs/design13/images/header.png", resolution: "640x360", watermark_filter: { position: "RT", padding_x: 10, padding_y: 10 } }
         self.model.file = FFMPEG::Movie.new(self.file.path).transcode("#{root}/#{cache_dir}/#{self.cache_id}/file.mp4", options)
       end
     end

     version :thumb do
       process thumbnail: [{format: 'png', quality: 10, size: 1200, strip: false, seek: 10, logger: Rails.logger}]
       def full_filename for_file
         png_name for_file, version_name
       end
     end

     def png_name for_file, version_name
       %Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.png}
     end


     # Add a white list of extensions which are allowed to be uploaded.
     # For images you might use something like this:
      def extension_white_list
        %w(mov avi mkv mpeg mpeg2 mp4 3gp)
      end
    end
  • current version of ffmpeg3.3.2 encode twice time of my video,the duration last two times long

    20 juin 2017, par jimmy

    I have a video with 00:01:41.46 play time,and transcode it perfect with ffmpeg-2.1.1,but when I upgrade FFmpeg version to 3.3.2 and compile it with the same compile option and same transcode option as 2.1.1 version used, I got a video with 00:03:21.82 long, I played that video to see what was added after 1:41.46, and find that it just repeat the video from the beginning with no audio, I find out no idea to this issue, it seems to be a bug that FFmpeg introducing, anyone help me !

    here is the log that I had transcoded with ffmpeg 3.3.2 :

    ffmpeg -i natural.mp4 -c:v libx264 -c:a copy output.mp4
    ffmpeg version 3.3.2 Copyright (c) 2000-2017 the FFmpeg developers
    built with gcc 4.6 (Ubuntu/Linaro 4.6.4-6ubuntu2)

    configuration: --enable-gpl --enable-nonfree --yasmexe=release/bin/yasm -- prefix=release --cc= --enable-static --disable-shared --disable-debug --extra-cflags=-Irelease/include --extra-ldflags='-Lrelease/lib -lm -ldl' --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc --enable-postproc --enable-bzlib --enable-zlib --enable-parsers --enable-libx264 --enable-libmp3lame --enable-libfdk-aac --enable-libspeex --enable-pthreads --extra->libs=-lpthread --enable-encoders --enable-decoders --enable-avfilter --enable->muxers --enable-demuxers

    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
    libswscale      4.  6.100 /  4.  6.100
    libswresample   2.  7.100 /  2.  7.100
    libpostproc    54.  5.100 / 54.  5.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'natural.mp4':
     Metadata:
     major_brand     : mp42
     minor_version   : 0
     compatible_brands: mp42isom
     creation_time   : 2009-07-11T08:32:34.000000Z
     encoder         : mp4creator 1.6.1d
     Duration: 00:01:41.46, start: 0.000000, bitrate: 9063 kb/s
     Stream #0:0(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1440x1080 [SAR 4:3 DAR 16:9], 8976 kb/s, 23.98 fps, 23.98 tbr, 90k tbn, 2997 tbc (default)
    Metadata:
       creation_time   : 2009-07-11T08:32:34.000000Z
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
       creation_time   : 2009-07-11T08:33:09.000000Z
    Stream mapping:
     Stream #0:0 -> #0:0 (mpeg4 (native) -> h264 (libx264))
     Stream #0:1 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    [libx264 @ 0x2d951e0] using SAR=4/3
    [libx264 @ 0x2d951e0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX AVX2 FMA3 LZCNT BMI2
    [libx264 @ 0x2d951e0] profile High, level 4.0
    [libx264 @ 0x2d951e0] 264 - core 138 r1+3473M a37a143 - H.264/MPEG-4 AVC codec >- Copyleft 2003-2013 - 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=23 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 'output.mp4':
    Metadata:
     major_brand     : mp42
     minor_version   : 0
     compatible_brands: mp42isom
     encoder         : Lavf57.71.100
    Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1440x1080 [SAR 4:3 DAR 16:9], q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc (default)
     Metadata:
       creation_time   : 2009-07-11T08:32:34.000000Z
       encoder         : Lavc57.89.100 libx264
     Side data:
       cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
     Stream #0:1(eng): Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 125 kb/s (default)
     Metadata:
       creation_time   : 2009-07-11T08:33:09.000000Z
    frame= 4842 fps= 27 q=-1.0 Lsize=  107608kB time=00:03:21.82
    bitrate=4367.8kbits/s speed=1.11x    
       video:105973kB audio:1556kB subtitle:0kB other streams:0kB global headers:0kB >muxing overhead: 0.074199%
    [libx264 @ 0x2d951e0] frame I:54    Avg QP:19.81  size: 77248
    [libx264 @ 0x2d951e0] frame P:3960  Avg QP:22.01  size: 25371
    [libx264 @ 0x2d951e0] frame B:828   Avg QP:23.01  size:  4680
    [libx264 @ 0x2d951e0] consecutive B-frames: 75.2%  4.6%  4.3% 15.9%
    [libx264 @ 0x2d951e0] mb I  I16..4: 17.0% 79.0%  3.9%
    [libx264 @ 0x2d951e0] mb P  I16..4:  2.1%  7.8%  0.2%  P16..4: 43.9%  9.7% 6.4%  0.0%  0.0%    skip:29.9%
    [libx264 @ 0x2d951e0] mb B  I16..4:  0.2%  1.1%  0.0%  B16..8: 29.2%  1.2%  0.2%  direct: 0.7%  skip:67.3%  L0:49.0% L1:47.7% BI: 3.3%
    [libx264 @ 0x2d951e0] 8x8 transform intra:78.1% inter:81.6%
    [libx264 @ 0x2d951e0] coded y,uvDC,uvAC intra: 52.3% 66.4% 11.9% inter: 20.5% >24.2% 0.8%
    [libx264 @ 0x2d951e0] i16 v,h,dc,p: 32% 22% 28% 18%
    [libx264 @ 0x2d951e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 19% 43%  3%  3%  3%  5%  2%  4%
    [libx264 @ 0x2d951e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 24% 12%  4%  8%  7% 11%  4%  5%
    [libx264 @ 0x2d951e0] i8c dc,h,v,p: 50% 23% 23%  4%
    [libx264 @ 0x2d951e0] Weighted P-Frames: Y:1.5% UV:0.3%
    [libx264 @ 0x2d951e0] ref P L0: 71.9% 17.0%  8.5%  2.5%  0.0%
    [libx264 @ 0x2d951e0] ref B L0: 89.7%  9.2%  1.2%
    [libx264 @ 0x2d951e0] ref B L1: 97.5%  2.5%
    [libx264 @ 0x2d951e0] kb/s:4298.67

    Here is the output when use ffmpeg -i natural.mp4 -c copy out.mp4,the output still last double times long :

    ./ffmpeg -i natural.mp4 -c copy out.mp4
    ffmpeg version 3.3.2 Copyright (c) 2000-2017 the FFmpeg developers
    built with gcc 4.6 (Ubuntu/Linaro 4.6.4-6ubuntu2)
     configuration: --enable-gpl --enable-nonfree --yasmexe=release/bin/yasm --prefix=release --cc= --enable-static --disable-shared --disable-debug --extra-cflags=-Irelease/include --extra-ldflags='-Lrelease/lib -lm -ldl' --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc --enable-postproc --enable-bzlib --enable-zlib --enable-parsers --enable-libx264 --enable-libmp3lame --enable-libfdk-aac --enable-libspeex --enable-pthreads --extra-libs=-lpthread --enable-encoders --enable-decoders --enable-avfilter --enable-muxers --enable-demuxers
     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
     libswscale      4.  6.100 /  4.  6.100
     libswresample   2.  7.100 /  2.  7.100
     libpostproc    54.  5.100 / 54.  5.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'natural.mp4':
     Metadata:
       major_brand     : mp42
       minor_version   : 0
       compatible_brands: mp42isom
       creation_time   : 2009-07-11T08:32:34.000000Z
       encoder         : mp4creator 1.6.1d
     Duration: 00:01:41.46, start: 0.000000, bitrate: 9063 kb/s
       Stream #0:0(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1440x1080 [SAR 4:3 DAR 16:9], 8976 kb/s, 23.98 fps, 23.98 tbr, 90k tbn, 2997 tbc (default)
       Metadata:
         creation_time   : 2009-07-11T08:32:34.000000Z
       Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
       Metadata:
         creation_time   : 2009-07-11T08:33:09.000000Z
    Output #0, mp4, to 'out.mp4':
     Metadata:
       major_brand     : mp42
       minor_version   : 0
       compatible_brands: mp42isom
       encoder         : Lavf57.71.100
       Stream #0:0(eng): Video: mpeg4 (Simple Profile) ( [0][0][0] / 0x0020), yuv420p, 1440x1080 [SAR 4:3 DAR 16:9], q=2-31, 8976 kb/s, 23.98 fps, 23.98 tbr, 90k tbn, 90k tbc (default)
       Metadata:
         creation_time   : 2009-07-11T08:32:34.000000Z
       Stream #0:1(eng): Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 125 kb/s (default)
       Metadata:
         creation_time   : 2009-07-11T08:33:09.000000Z
    Stream mapping:
     Stream #0:0 -> #0:0 (copy)
     Stream #0:1 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    frame= 4842 fps=2243 q=-1.0 Lsize=  222944kB time=00:03:21.91
    bitrate=9045.4kbits/s speed=93.5x    
    video:221299kB audio:1556kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.039627%

    Here is the output what ffmpeg-2.1.1 generate,It’s executed correctly :

    ./ffmpeg -i natural.mp4 -c copy output2.mp4
    ffmpeg version 2.1.1 Copyright (c) 2000-2013 the FFmpeg developers
     built on Mar 10 2017 13:56:14 with gcc 4.6 (Ubuntu/Linaro 4.6.4-6ubuntu2)
     configuration: --enable-gpl --enable-nonfree --yasmexe=release/bin/yasm --prefix=release --cc= --enable-static --disable-shared --disable-debug --extra-cflags='-I${ffmpeg_exported_release_dir}/include' --extra-ldflags='-L${ffmpeg_exported_release_dir}/lib -lm -ldl' --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc --enable-postproc --enable-bzlib --enable-zlib --enable-parsers --enable-libx264 --enable-libmp3lame --enable-libfdk-aac --enable-libspeex --enable-pthreads --extra-libs=-lpthread --enable-encoders --enable-decoders --enable-avfilter --enable-muxers --enable-demuxers
     libavutil      52. 48.101 / 52. 48.101
     libavcodec     55. 39.101 / 55. 39.101
     libavformat    55. 19.104 / 55. 19.104
     libavdevice    55.  5.100 / 55.  5.100
     libavfilter     3. 90.100 /  3. 90.100
     libswscale      2.  5.101 /  2.  5.101
     libswresample   0. 17.104 /  0. 17.104
     libpostproc    52.  3.100 / 52.  3.100
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x2d2e900] multiple edit list entries, a/v desync might occur, patch welcome
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'natural.mp4':
     Metadata:
       major_brand     : mp42
       minor_version   : 0
       compatible_brands: mp42isom
       creation_time   : 2009-07-11 08:32:34
       encoder         : mp4creator 1.6.1d
     Duration: 00:01:41.46, start: 0.000000, bitrate: 9063 kb/s
       Stream #0:0(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1440x1080 [SAR 4:3 DAR 16:9], 8976 kb/s, 23.98 fps, 23.98 tbr, 90k tbn, 2997 tbc (default)
       Metadata:
         creation_time   : 2009-07-11 08:32:34
       Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
       Metadata:
         creation_time   : 2009-07-11 08:33:09
    Output #0, mp4, to 'output2.mp4':
     Metadata:
       major_brand     : mp42
       minor_version   : 0
       compatible_brands: mp42isom
       encoder         : Lavf55.19.104
       Stream #0:0(eng): Video: mpeg4 ( [0][0][0] / 0x0020), yuv420p, 1440x1080 [SAR 4:3 DAR 16:9], q=2-31, 8976 kb/s, 23.98 fps, 90k tbn, 90k tbc (default)
       Metadata:
         creation_time   : 2009-07-11 08:32:34
       Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, 125 kb/s (default)
       Metadata:
         creation_time   : 2009-07-11 08:33:09
    Stream mapping:
     Stream #0:0 -> #0:0 (copy)
     Stream #0:1 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    frame= 2421 fps=0.0 q=-1.0 Lsize=  112273kB time=00:01:41.49 bitrate=9062.0kbits/s    
    video:110650kB audio:1556kB subtitle:0 global headers:0kB muxing overhead 0.060302%

    Here is the video link

  • AVIOContext custom stream playback glitches/corruption on UDP stream

    21 décembre 2017, par WLGfx

    I’ve written a general all round player (for Android OS) which plays file based streams as well as streams from network sources, and it all works good. File based streams I’ve got the seeking which works too.

    What it is I’m now struggling with is now I’m using AVIOContext to latch onto a UDP stream which saves the packet data partially in memory and then to transient storage. This is so I can pause live TV and also seek within it.

    However, after much faffing about, during playback (seeking is only partial at the moment), either the video frame rate will drop from 25FPS (will be deinterlaced on higher spec devices) down to between 17 and 19 frames per second, or, it will glitch and grey out.

    When I playback the record TS data from the file, it plays perfect, so the UDP buffering and writing out the overflow is sound. (This is currently not true now, only currently a minor issue)

    I’m at the point were I’ve spent a lot of time on this and I’m at a loss as to why I get either frame drops or glitches.

    The class def :

    #define PKT_SIZE (188)
    #define PKT_SIZE7 (PKT_SIZE * 7)

    #define UDP_QUEUE_SIZE (12000)
    #define UDP_THRESHOLD (100)

    #define FILE_QUEUE_PKTS (200000)

    #define AVIO_QUEUE_SIZE (24)
    #define AVIO_THRESHOLD (10)

    extern "C" {
    #include "libavformat/avio.h"
    };

    /*
    * PracticalSocket class as found here with examples:
    * http://cs.baylor.edu/~donahoo/practical/CSockets/practical/
    */

    #include "PracticalSocket.h"

    class FFIOBufferManager2 {

    public:

       AVIOContext *avioContext = nullptr;

       bool quit = false;

       char udp_buffer[UDP_QUEUE_SIZE][PKT_SIZE7];
       int udp_write_pos, udp_size; // by PKT_SIZE7
       char *get_udp_buffer(int index);
       int get_udp_buffer_size() { return udp_size; }

       int file_write_pos, file_size; // by PKT_SIZE7
       std::fstream file_out, file_in;
       std::mutex udp_mutex, file_mutex;
       std::thread udp_thread, file_thread;

       static void udp_thread_func(FFIOBufferManager2 *io, const char *ip, int port);
       static void file_thread_func(FFIOBufferManager2 *io, const char *dir);
       void udp_thread_run(const char *ip, int port);
       void file_thread_run();

       char avio_buffer[AVIO_QUEUE_SIZE * 7 * PKT_SIZE];
       int64_t avio_read_offset; // controlled by udp mutex (quickest)
       static int avio_read(void *ptr, uint8_t *buff, int buf_size);
       static int64_t avio_seek(void *ptr, int64_t pos, int whence);
       int avio_read_run(uint8_t *buf, int buf_size);
       int64_t avio_seek_run(int64_t pos, int whence);
       void write_udp_overflow();

       void start(const char *ip, int port, const char *dir);

       void get_size_and_pos(int64_t *size, int64_t *pos);

       ~FFIOBufferManager2();
    };

    The classes methods :

    #include
    #include "FFIOBufferManager2.h"

    #include "LOG.h"

    void FFIOBufferManager2::start(const char *ip, int port, const char *dir) {

       file_write_pos = 0;
       file_size = 0;

       udp_write_pos = 0;
       udp_size = 0;
       avio_read_offset = 0;

       file_thread = std::thread(&FFIOBufferManager2::file_thread_func, this, dir);
       udp_thread = std::thread(&FFIOBufferManager2::udp_thread_func, this, ip, port);

       LOGD("Initialising avioContext");

       avioContext = avio_alloc_context((uint8_t*)avio_buffer,
                                        AVIO_QUEUE_SIZE * PKT_SIZE7,
                                        0,
                                        this,
                                        avio_read,
                                        NULL,
                                        avio_seek);
    }

    void FFIOBufferManager2::udp_thread_func(FFIOBufferManager2 *io, const char *ip, int port) {

       LOGD("AVIO UDP thread started address %s port %d - %08X", ip, port, (uint)io);

       io->udp_thread_run(ip, port); // run inside class

       LOGD("AVIO UDP thread stopped");
    }

    void FFIOBufferManager2::udp_thread_run(const char *ip, int port) {

       std::string addr = ip;

       UDPSocket socket(addr, (uint16_t)port);
       socket.joinGroup(addr);

       LOGD("UDP loop starting");

       while (!quit) {

           if (socket.recv(get_udp_buffer(udp_write_pos), PKT_SIZE7) == PKT_SIZE7) {

               udp_mutex.lock();

               udp_write_pos = (udp_write_pos + 1) % UDP_QUEUE_SIZE;
               udp_size++;
               if (udp_size >= UDP_QUEUE_SIZE) udp_size--;
               else avio_read_offset += PKT_SIZE7;

               udp_mutex.unlock();
           }
       }
    }

    void FFIOBufferManager2::file_thread_func(FFIOBufferManager2 *io, const char *dir) {

       LOGD("AVIO FILE thread started");

       std::string file = dir;
       const char *tsfile_name = "/tsdata.ts";
       file += tsfile_name;

       LOGD("Deleting old file %s", file.c_str());

       remove(file.c_str());

       {
           fstream temp; // create the ts file
           temp.open(file.c_str());
           temp.close();
       }

       LOGD("Opening %s for read and write", file.c_str());

       io->file_out.open(file, fstream::out | fstream::binary);
       io->file_in.open(file, fstream::in | fstream::binary);

       io->file_thread_run(); // continue inside the class to lessen pointer use

       LOGD("AVIO FILE thread stopped");
    }

    void FFIOBufferManager2::file_thread_run() {

       LOGD("FILE thread run");

       if (!file_out.is_open() || !file_in.is_open()) {

           LOGE("TS data file, error opening...");
           quit = true;
           return;
       }

       int udp_threshold = UDP_QUEUE_SIZE - (UDP_THRESHOLD * 4);

       while (!quit) {

           if (udp_size >= udp_threshold) write_udp_overflow();
           else usleep(1000 * 1);
       }
    }

    void FFIOBufferManager2::write_udp_overflow() {

       file_mutex.lock();

       udp_mutex.lock();
       int udp_write_pos_current = udp_write_pos;
       int udp_size_current = udp_size;
       udp_mutex.unlock();

       int udp_index = udp_write_pos_current - udp_size_current;
       if (udp_index < 0) udp_index += UDP_QUEUE_SIZE;
       int written = 0;

       //file_out.seekp((int64_t)file_write_pos * PKT_SIZE7);

       while (written < UDP_THRESHOLD) {
           file_out.write(get_udp_buffer(udp_index), PKT_SIZE7);
           written++;
           udp_index = (udp_index + 1) % UDP_QUEUE_SIZE;

           file_write_pos++;
           if (file_write_pos >= FILE_QUEUE_PKTS) {
               file_write_pos = 0;
               file_out.seekp(0);
           }
           file_size++;
           if (file_size > FILE_QUEUE_PKTS) file_size = FILE_QUEUE_PKTS;
       }

       udp_mutex.lock();
       udp_size -= UDP_THRESHOLD; // we've written this amount out
       udp_mutex.unlock();

       //file_out.flush();

       file_mutex.unlock();

       //LOGD("Written UDP overflow at %d of %d blocks file size %d",
       //     udp_index, written, file_size);
    }

    char *FFIOBufferManager2::get_udp_buffer(int index) {
       if (index < 0 || index >= UDP_QUEUE_SIZE) return nullptr;
       return ((char*)udp_buffer + (index * PKT_SIZE7));
    }

    /*
    * The avio_read and avio_seek now work on either 188 byte alignment or
    * byte alignment for the benefit of ffmpeg - byte positioning at the moment
    *
    * The file_mutex allows for either a read or write operation at a time
    */

    int FFIOBufferManager2::avio_read(void *ptr, uint8_t *buff, int buf_size) {
       FFIOBufferManager2 *io = (FFIOBufferManager2*)ptr;
       return io->avio_read_run(buff, buf_size);
    }

    int64_t FFIOBufferManager2::avio_seek(void *ptr, int64_t pos, int whence) {
       FFIOBufferManager2 *io = (FFIOBufferManager2*)ptr;
       return io->avio_seek_run(pos, whence);
    }

    int FFIOBufferManager2::avio_read_run(uint8_t *buf, int buf_size) {

       file_mutex.lock();
       udp_mutex.lock();

       int64_t cur_udp_write_pos = (int64_t) udp_write_pos * PKT_SIZE7;
       int64_t cur_udp_size = (int64_t) udp_size * PKT_SIZE7;

       int64_t cur_file_write_pos = (int64_t) file_write_pos * PKT_SIZE7;
       int64_t cur_file_size = (int64_t) file_size * PKT_SIZE7;

       int64_t cur_avio_read_offset = avio_read_offset; // already int64_t (under the udp_mutex)

       udp_mutex.unlock();

       if (cur_avio_read_offset < (AVIO_THRESHOLD * 4) * PKT_SIZE7) {
           file_mutex.unlock();
           return 0;
       }

       int64_t udp_buffer_max = (int64_t) (UDP_QUEUE_SIZE * PKT_SIZE7);
       int64_t file_buffer_max = (int64_t) (FILE_QUEUE_PKTS * PKT_SIZE7);
       uint8_t *ptr_udp_buffer = (uint8_t*)udp_buffer;
       int cur_written = 0;

       int file_reads = 0, udp_reads = 0; // for debugging

       int64_t cur_file_offset = cur_file_write_pos - cur_udp_size - cur_avio_read_offset;
       while (cur_file_offset < 0) cur_file_offset += file_buffer_max;

       if (cur_file_offset >= 0) {
           file_in.seekg(cur_file_offset);

           while (//cur_avio_read_offset > 0
                  cur_avio_read_offset > cur_udp_size
                  && cur_written < buf_size) { // read from file first

               file_in.read(&avio_buffer[cur_written], PKT_SIZE); // get 1 or 188 byte/s

               cur_file_offset+=PKT_SIZE;
               if (cur_file_offset >= file_buffer_max) { // back to file beginning
                   cur_file_offset = 0;
                   file_in.seekg(0);
               }

               cur_avio_read_offset-=PKT_SIZE;

               cur_written+=PKT_SIZE;
               file_reads+=PKT_SIZE;
           }
       }

       int64_t cur_udp_offset = (cur_udp_write_pos - cur_avio_read_offset);
       if (cur_udp_offset < 0) cur_udp_offset += udp_buffer_max;

       while (cur_avio_read_offset > AVIO_THRESHOLD * PKT_SIZE7
               && cur_avio_read_offset <= cur_udp_size
               && cur_written < buf_size) { // read the rest from udp buffer

           buf[cur_written] = ptr_udp_buffer[cur_udp_offset]; // get byte

           cur_udp_offset = (cur_udp_offset + 1) % udp_buffer_max;
           if (cur_udp_offset == 0) LOGD("AVIO UDP BUFFER to start");

           cur_avio_read_offset--;

           cur_written++;
           udp_reads++;
       }

       udp_mutex.lock();
       avio_read_offset -= cur_written;
       udp_mutex.unlock();

       file_mutex.unlock();

       if (cur_written) {
           LOGD("AVIO_READ: Written %d of %d, avio_offset %lld, file reads %d, udp reads %d, udp offset %lld, file offset %lld, file size %lld",
                cur_written, buf_size,
                cur_avio_read_offset,
                file_reads, udp_reads,
                cur_udp_write_pos, cur_file_write_pos, cur_file_size);
       }

       return cur_written;
    }

    int64_t FFIOBufferManager2::avio_seek_run(int64_t pos, int whence) {
       // SEEK_SET(0), SEEK_CUR(1), SEEK_END(2), AVSEEK_SIZE

       int64_t new_pos = -1;
       int64_t full_length = (udp_size + file_size) * PKT_SIZE7;

       switch (whence) {

           case AVSEEK_SIZE:
               LOGD("AVSEEK_SIZE pos %lld", pos);
               break;

           case SEEK_SET:
               LOGD("AVSEEK_SET pos %lld", pos);
               if (pos > full_length) new_pos = full_length;
               else new_pos = full_length - pos;
               break;

           case SEEK_CUR:
               LOGD("AVSEEK_CUR pos %lld", pos);
               break;

           case SEEK_END:
               LOGD("AVSEEK_END pos %lld", pos);
               new_pos = pos;
               break;

           default:
               LOGD("UNKNOWN AVIO SEEK whence %d pos %lld", whence, pos);
               break;
       }

       if (new_pos >= 0) {

           udp_mutex.lock();

           new_pos = (new_pos / PKT_SIZE) * PKT_SIZE; // align to packet boundary
           avio_read_offset = new_pos;
           //file_out.seekg(full_length - new_pos);

           udp_mutex.unlock();

           return full_length - new_pos;
       }

       return -1;
    }

    FFIOBufferManager2::~FFIOBufferManager2() {
       if (avioContext) ;// TODO whoops
       quit = true;
       if (udp_thread.joinable()) udp_thread.join();
       if (file_thread.joinable()) file_thread.join();
    }

    void FFIOBufferManager2::get_size_and_pos(int64_t *size, int64_t *pos) {
       file_mutex.lock();
       udp_mutex.lock();

       *size = (udp_size + file_size) * PKT_SIZE7;
       *pos = *size - avio_read_offset;

       udp_mutex.unlock();
       file_mutex.unlock();
    }

    It’ll play for a few seconds before any of the glitches start to appear. I have checked against the udp_buffer and the avio_buffer, but my suspicions lie with one of two things :

    1. Reading and writing to the file.
    2. the avio_read method is wrong.

    Has anybody got any input as to why this is occurring ? Any thoughts would be greatly appreciated.

    If you need any more information I’ll be glad to provide more details.

    EDIT : Seeking now actually moves to any point within the stream, but now doesn’t read from the file recording. Although that’s only a minor issue at the moment.

    The main two issues still stand, frame rate drops dramatically and the glitches after approximately 8 seconds.