• Slidy
  • Command-line cinema : Différence entre versions

    De Ustensile
    Aller à : navigation, rechercher
    (Extracting Frames from a Movie: ffmpeg)
     
    (18 révisions intermédiaires par 3 utilisateurs non affichées)
    Ligne 1 : Ligne 1 :
      
    ; Public domain on archive.org
     
    : https://archive.org/details/animationandcartoons?&and%5B%5D=collection%3A%22fav-publicdomaininfo%22
     
    : [[https://archive.org/search.php?query=fats%20waller∧%5B%5D=mediatype%3A%22audio%22|https://archive.org/search.php?query=fats%20waller∧[]=mediatype%3A%22audio%22]]
     
    ; Librivox
     
    : https://librivox.org/search?primary_key=2&search_category=language&search_page=1&search_form=get_results
     
    ; Europeana
     
    : https://www.europeana.eu/portal/en/search?q=+RIGHTS%3A*creative*+AND+NOT+RIGHTS%3A*nc*+AND+NOT+RIGHTS%3A*nd*&f%5BTYPE%5D%5B%5D=VIDEO
     
      
    MAKING CREDITS.... (need imagemagick sample...)
     
      
    ASSEMBLING THE SCENES
    +
    = Some Sources of copyleft materials online =
      
    ffmpeg concat files?!
    +
    * [https://archive.org/details/animationandcartoons?&and%5B%5D=collection%3A%22fav-publicdomaininfo%22 archive.org]
     +
    example: [https://archive.org/search.php?query=fats%20waller∧%5B%5D=mediatype%3A%22audio%22 Fats Waller on archive.org]
     +
    * [https://librivox.org/search?primary_key=2&search_category=language&search_page=1&search_form=get_results Librivox]
     +
    * [https://www.europeana.eu/portal/en/search?q=+RIGHTS%3A*creative*+AND+NOT+RIGHTS%3A*nc*+AND+NOT+RIGHTS%3A*nd*&f%5BTYPE%5D%5B%5D=VIDEO Europeana]
     +
     
     +
    = Wget =
     +
     
     +
    Wget is a simple way to download things from the web. These can be webpages, videos, sounds -- basically anything that has a proper URL to access it (for youtube videos or other "closed" sources see youtube-dl)
     +
     
     +
    wget URL
      
     
    = Youtube-dl =
     
    = Youtube-dl =
      
    <pre>youtube-dl URL</pre>
    +
    youtube-dl URL
    = Wget =
    +
     
     +
    There are *many* interesting options with youtube-dl
     +
     
     +
    Show formats available:
     +
     
     +
    youtube-dl -F URL
     +
     
     +
    Download a particular format (using its numeric code)
     +
     
     +
    youtube-dl -f43 URL
     +
     
     +
    Download a video + the automatically generated subtitles!
     +
     
     +
    youtube-dl --write-auto-sub URL
      
    <pre>wget URL</pre>
     
     
    = Mplayer =
     
    = Mplayer =
      
    Ligne 32 : Ligne 45 :
     
    f  : Toggle fullscreen
     
    f  : Toggle fullscreen
     
    s  : Save current frame (with -vf screenshot, see Mplayer Snapshots)
     
    s  : Save current frame (with -vf screenshot, see Mplayer Snapshots)
    q  : Quit</pre>
    +
    q  : Quit
     +
    </pre>
     +
     
     
    Time options
     
    Time options
      
    <pre># Play from 10 seconds to 1 minute 6 seconds.
    +
    Play from 10 seconds to 1 minute 6 seconds.
    mplayer -ss 10 -endpos 56
    +
     
     +
    mplayer -ss 10 -endpos 56
     +
     
     +
    Play from 01:10:30 - 01:11:00
     +
     
     +
    mplayer -ss 01:10:30 -endpos 30
      
    # Play from 01:10:30 - 01:11:00
     
    mplayer -ss 01:10:30 -endpos 30</pre>
     
     
    = Extracting Frames from a Movie: Mplayer Snapshots =
     
    = Extracting Frames from a Movie: Mplayer Snapshots =
      
    Ligne 51 : Ligne 69 :
     
    = Extracting Frames from a Movie: ffmpeg =
     
    = Extracting Frames from a Movie: ffmpeg =
      
    <pre>ffmpeg -i &lt;source&gt; &lt;destination%06d.jpg&gt;
    +
    ffmpeg uses a "printf" style code for working with image sequences. They also look like "%04d" where the number (4 in this case) means how many numbers to use in total so for instance, "foo%04d.jpg" would produce filenames like:
     +
     
     +
    foo0001.jpg
     +
    ...
     +
    foo0123.jpg
     +
     
     +
    And a code like "%09d.png" would produce things like:
     +
     
     +
    000000001.png
     +
    000000002.png
     +
    ...
     +
    999999999.png
     +
     
     +
    To extract all of the frames of a video:
     +
     
     +
    ffmpeg -i &lt;source&gt; &lt;destination%06d.jpg&gt;
     +
     
     +
    ffmpeg -ss 12:00 -i &lt;source&gt; -vframes 100 &lt;destination%04d.jpg&gt;
      
    ffmpeg -ss 12:00 -vframes 100 -i &lt;source&gt; &lt;destination%04d.jpg&gt;</pre>
     
     
    Timing (start + stop) can be controlled with -ss (skip start) and -t (total time of result)
     
    Timing (start + stop) can be controlled with -ss (skip start) and -t (total time of result)
      
     
    When -ss is before the -i ffmpeg attempts to JUMP which is fast only precise to the nearest key frame.
     
    When -ss is before the -i ffmpeg attempts to JUMP which is fast only precise to the nearest key frame.
      
    <pre># Extract 3 seconds starting at (near) 05:47 and save as snip.mp4  
    +
    Extract 3 seconds starting at (near) 05:47 and save as snip.mp4  
    ffmpeg -ss 05:47 -t 3 -i ten.mp4 snip.mp4</pre>
    +
     
     +
    ffmpeg -ss 05:47 -i ten.mp4 -t 3 snip.mp4
     +
     
     
    When you put -ss after the -i, the timing is more precise but it's slower as the movie is actually played (quickly) up to that precise point.
     
    When you put -ss after the -i, the timing is more precise but it's slower as the movie is actually played (quickly) up to that precise point.
      
    <pre># Extract 3 seconds starting at (exactly) 05:47 and save as snip.mp4
    +
    Extract 3 seconds starting at (exactly) 05:47 and save as snip.mp4  
    ffmpeg -i ten.mp4 -ss 05:47 -t 3 snip.mp4
    +
      
     +
    ffmpeg -i ten.mp4 -ss 05:47 -t 3 snip.mp4
      
    ffmpeg -ss 05:47 -i powersof10/ten.mp4 -vframes 10 snip%04d.jpg
    +
    ffmpeg -ss 05:47 -i powersof10/ten.mp4 -vframes 10 snip%04d.jpg
      
     +
    Starting at 05:47 output one frame every 5 seconds for 10 frames, save using names starting snip0001.jpg
      
    # Starting at 05:47 output one frame every 5 seconds for 10 frames, save using names starting snip0001.jpg
    +
    ffmpeg -ss 05:47 -i powersof10/ten.mp4 -r 1/5 -vframes 10 snip%04d.jpg
    ffmpeg -ss 05:47 -i powersof10/ten.mp4 -r 1/5 -vframes 10 snip%04d.jpg
    +
      
      
    # Combine Image Sequence starting frames000001.jpg and the audio in soundtrack.wav
    +
    Combine Image Sequence starting frames000001.jpg and the audio in soundtrack.wav, save result as ''result.mp4'':
    # Save result as &quot;result.mp4&quot;
    +
     
    ffmpeg -i frames%06d.jpg -i soundtrack.wav result.mp4</pre>
    +
    ffmpeg -i frames%06d.jpg -i soundtrack.wav result.mp4
     +
     
     
    Example:
     
    Example:
      
    <pre>ffmpeg -ri 10 -i frame%06d.jpg -i apologie.wav apologie.mp4</pre>
    +
    ffmpeg -ri 10 -i frame%06d.jpg -i apologie.wav apologie.mp4
    To force ffmpeg to OVERWRITE any previously existing file (useful when inside a larger script that you want to run again and again) -- add the -y to &quot;AUTO-YES&quot; the question about replacing the output.
    +
     
     +
    To force ffmpeg to OVERWRITE any previously existing file (useful when inside a larger script that you want to run again and again) -- add the -y to ''AUTO-YES'' the question about replacing the output.
     +
     
     +
    ffmpeg -r 10 -i frame%06d.jpg -i apologie.wav -y apologie.mp4
     +
     
     +
    = Frame to movie with ffmpeg =
     +
     
     +
    If you have a set of sequentially numbered files (as generated from a %0nd pattern:
     +
     
     +
    ffmpeg -f image2 -i anim%04d.jpg -s 160x120 -y anim.ogv
     +
     
     +
    Or if the numbering isn't sequential (there are gaps), you can use a traditional "glob" (asterisk), NB the use of the single quotes is important (prevents bash from expanding the glob, leaving that work to ffmpeg itself):
     +
     
     +
    ffmpeg -f image2 -pattern_type glob -i 'anim*.png' -s 160x120 -y anim.ogv
     +
     
     +
    = Extraction de sons =
     +
     
     +
    ffmpeg -i <source> output.wav
     +
     
     +
    or
     +
     
     +
    ffmpeg -i <source> output.mp3
     +
     
     +
    If you have a video and you want to simply separate the audio, ''without recompressing it'' (which is faster and doesn't change the quality / data), you can say something like:
     +
     
     +
    Copying just the ogg audio from a webm:
     +
     
     +
    ffmpeg -i mysource.webm -vn -c:a copy output.ogg
     +
     
     +
    Copying just the AAC audio from an MP4:
     +
     
     +
    ffmpeg -i mysource.mp4 -vn -c:a copy output.aac
      
    <pre>ffmpeg -r 10 -i frame%06d.jpg -i apologie.wav -y apologie.mp4</pre>
     
     
    = Padding audio =
     
    = Padding audio =
      
    Ligne 87 : Ligne 154 :
      
     
    <pre>sox input.wav output.wav pad 10 30</pre>
     
    <pre>sox input.wav output.wav pad 10 30</pre>
     +
     
    = Mixing audio =
     
    = Mixing audio =
      
     
    with ffmpeg
     
    with ffmpeg
      
    <source lang="bash">ffmpeg -i apologie.wav -i badmusic.wav -filter_complex amerge mix.mp3</source>
    +
    ffmpeg -i apologie.wav -i badmusic.wav -filter_complex amerge mix.mp3
     +
     
     
    it uses the duration of the shortest file.
     
    it uses the duration of the shortest file.
      
     
    you can also explicitly limit the number of audio channels with:
     
    you can also explicitly limit the number of audio channels with:
      
    <source lang="bash">ffmpeg -i apologie.wav -i badmusic.wav -filter_complex amerge -ac 1 mix.mp3</source>
    +
    ffmpeg -i apologie.wav -i badmusic.wav -filter_complex amerge -ac 1 mix.mp3
     +
     
     
    = Sequence (concatenate) audio / video =
     
    = Sequence (concatenate) audio / video =
      
    Ligne 113 : Ligne 183 :
     
    To do this with ffmpeg you need to make a sort of playlist file that looks like:
     
    To do this with ffmpeg you need to make a sort of playlist file that looks like:
      
    ''playlist.txt''
    +
    Create a file called ''playlist.txt'':
     +
     
     +
    file 'a.wav'
     +
    file 'b.wav'
     +
    file 'c.wav'
     +
     
     +
    Then the command to put the pieces together is:
     +
     
     +
    ffmpeg -f concat -i concat.txt concat.mp3
      
    <pre>file 'a.wav'
     
    file 'b.wav'
     
    file 'c.wav'</pre>
     
    <source lang="bash">ffmpeg -f concat -i concat.txt concat.mp3</source>
     
     
    See: https://trac.ffmpeg.org/wiki/Concatenate
     
    See: https://trac.ffmpeg.org/wiki/Concatenate
      
    Ligne 125 : Ligne 199 :
     
    Imagemagick! http://www.imagemagick.org/Usage/text/
     
    Imagemagick! http://www.imagemagick.org/Usage/text/
      
    convert -size 640x480 -gravity center -pointsize 72 -font CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY what about a very long text then?' title.png
    +
    convert -size 640x480 -gravity center -pointsize 72 -font CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY what about a very long text then?' title.png
      
     
    with a newline:
     
    with a newline:
      
    convert -size 640x480 -gravity center -pointsize 72 -font CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY!what about a very long text then?' title.png
    +
    convert -size 640x480 -gravity center -pointsize 72 -font CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY!what about a very long text then?' title.png
      
     
    What colors?
     
    What colors?
      
    convert -list color
    +
    convert -list color
      
     
    Random color!
     
    Random color!
      
    <source lang="bash">convert -list color | cut -d" " -f1 | shuf | head -n1</source>
    +
    convert -list color | cut -d" " -f1 | shuf | head -n1
     +
     
     
    There's also label:, but it doesn't do word wrap.
     
    There's also label:, but it doesn't do word wrap.
      
    <source lang="bash">convert -size 640x480 -gravity center -pointsize 72 -font ~/.fonts/coupeur-boldblock/CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY what about a very long text then?' title.png</source>
    +
    convert -size 640x480 -gravity center -pointsize 72 -font ~/.fonts/coupeur-boldblock/CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY what about a very long text then?' title.png</source>
    <pre>convert -size 640x480 -gravity center -pointsize 72 -font -background lightblue -fill blue label:'magic\nmagic!!!' whoami.png</pre>
    +
     
     +
    convert -size 640x480 -gravity center -pointsize 72 -font -background lightblue -fill blue label:'magic\nmagic!!!' whoami.png
     +
     
     
    title with random colors:
     
    title with random colors:
      
    <pre>convert -size 640x480 -gravity center -pointsize 72 \
    +
    convert -size 640x480 -gravity center -pointsize 72 \
     
         -font ~/.fonts/coupeur-boldblock/CoupeurBoldBlock.otf \
     
         -font ~/.fonts/coupeur-boldblock/CoupeurBoldBlock.otf \
     
         -background $(convert -list color | cut -d&quot; &quot; -f1 | shuf | head -n1) \
     
         -background $(convert -list color | cut -d&quot; &quot; -f1 | shuf | head -n1) \
     
         -fill $(convert -list color | cut -d&quot; &quot; -f1 | shuf | head -n1) \
     
         -fill $(convert -list color | cut -d&quot; &quot; -f1 | shuf | head -n1) \
         label:'Surprise!!!' surprise.png</pre>
    +
         label:'Surprise!!!' surprise.png
    TITLE
    +
      
    c1=<code>shuf imcolornames.txt | head -n 1</code> c2=<code>shuf imcolornames.txt | head -n 1</code> convert -size 640x480 -background $c1 -fill $c2 -pointsize 72 -gravity center -f ont /home/murtaugh/.fonts/OSP_Libertinage/Libertinage-s.ttf label:Contemporaneit y contemporaneity.png
    +
    TITLE
      
    NB: Using the @ sign with label to read from a file seems to be blocked (by some kind of security &quot;policy&quot;).... back quoting a file might work though?!
    +
    c1=`shuf imcolornames.txt | head -n 1`
     +
    c2=`shuf imcolornames.txt | head -n 1`
     +
    convert -size 640x480 -background $c1 -fill $c2 -pointsize 72 -gravity center -f ont /home/murtaugh/.fonts/OSP_Libertinage/Libertinage-s.ttf label:Contemporaneit y contemporaneity.png
      
    MORNING: RUn through all the different exercises. Talk about free / libre content sources.
    +
    NB: Using the @ sign with label to read from a file seems to be blocked (by some kind of security policy).... back quoting a file might work though?!

    Version actuelle en date du 24 octobre 2017 à 11:40


    Some Sources of copyleft materials online

    example: Fats Waller on archive.org

    Wget

    Wget is a simple way to download things from the web. These can be webpages, videos, sounds -- basically anything that has a proper URL to access it (for youtube videos or other "closed" sources see youtube-dl)

    wget URL
    

    Youtube-dl

    youtube-dl URL
    

    There are *many* interesting options with youtube-dl

    Show formats available:

    youtube-dl -F URL
    

    Download a particular format (using its numeric code)

    youtube-dl -f43 URL
    

    Download a video + the automatically generated subtitles!

    youtube-dl --write-auto-sub URL
    

    Mplayer

    Basic key commands when playing a movie with mplayer

    SPACE: Pause/Play
    . : Forward one frame
    Left + Right : Jump
    9 0 : Volume
    [ ] : Speed
    o   : Toggle display mode (shows current time and duration)
    f   : Toggle fullscreen
    s   : Save current frame (with -vf screenshot, see Mplayer Snapshots)
    q   : Quit
    

    Time options

    Play from 10 seconds to 1 minute 6 seconds.

    mplayer -ss 10 -endpos 56
    

    Play from 01:10:30 - 01:11:00

    mplayer -ss 01:10:30 -endpos 30
    

    Extracting Frames from a Movie: Mplayer Snapshots

    mplayer -vf screenshot <source>

    Then while the movie is playing ... press the s key each time you want to save a frame. You should find files named like:

    shot0001.png
    shot0002.png
    shot0003.png
    shot0004.png

    Extracting Frames from a Movie: ffmpeg

    ffmpeg uses a "printf" style code for working with image sequences. They also look like "%04d" where the number (4 in this case) means how many numbers to use in total so for instance, "foo%04d.jpg" would produce filenames like:

    foo0001.jpg
    ...
    foo0123.jpg
    

    And a code like "%09d.png" would produce things like:

    000000001.png
    000000002.png
    ...
    999999999.png
    

    To extract all of the frames of a video:

    ffmpeg -i <source> <destination%06d.jpg>
    
    ffmpeg -ss 12:00 -i <source> -vframes 100 <destination%04d.jpg>
    

    Timing (start + stop) can be controlled with -ss (skip start) and -t (total time of result)

    When -ss is before the -i ffmpeg attempts to JUMP which is fast only precise to the nearest key frame.

    Extract 3 seconds starting at (near) 05:47 and save as snip.mp4

    ffmpeg -ss 05:47 -i ten.mp4 -t 3 snip.mp4
    

    When you put -ss after the -i, the timing is more precise but it's slower as the movie is actually played (quickly) up to that precise point.

    Extract 3 seconds starting at (exactly) 05:47 and save as snip.mp4

    ffmpeg -i ten.mp4 -ss 05:47 -t 3 snip.mp4
    
    ffmpeg -ss 05:47 -i powersof10/ten.mp4 -vframes 10 snip%04d.jpg
    

    Starting at 05:47 output one frame every 5 seconds for 10 frames, save using names starting snip0001.jpg

    ffmpeg -ss 05:47 -i powersof10/ten.mp4 -r 1/5 -vframes 10 snip%04d.jpg
    


    Combine Image Sequence starting frames000001.jpg and the audio in soundtrack.wav, save result as result.mp4:

    ffmpeg -i frames%06d.jpg -i soundtrack.wav result.mp4
    

    Example:

    ffmpeg -ri 10 -i frame%06d.jpg -i apologie.wav apologie.mp4
    

    To force ffmpeg to OVERWRITE any previously existing file (useful when inside a larger script that you want to run again and again) -- add the -y to AUTO-YES the question about replacing the output.

    ffmpeg -r 10 -i frame%06d.jpg -i apologie.wav -y apologie.mp4
    

    Frame to movie with ffmpeg

    If you have a set of sequentially numbered files (as generated from a %0nd pattern:

    ffmpeg -f image2 -i anim%04d.jpg -s 160x120 -y anim.ogv
    

    Or if the numbering isn't sequential (there are gaps), you can use a traditional "glob" (asterisk), NB the use of the single quotes is important (prevents bash from expanding the glob, leaving that work to ffmpeg itself):

    ffmpeg -f image2 -pattern_type glob -i 'anim*.png' -s 160x120 -y anim.ogv
    

    Extraction de sons

    ffmpeg -i <source> output.wav
    

    or

    ffmpeg -i <source> output.mp3
    

    If you have a video and you want to simply separate the audio, without recompressing it (which is faster and doesn't change the quality / data), you can say something like:

    Copying just the ogg audio from a webm:

    ffmpeg -i mysource.webm -vn -c:a copy output.ogg
    

    Copying just the AAC audio from an MP4:

    ffmpeg -i mysource.mp4 -vn -c:a copy output.aac
    

    Padding audio

    Add 10 seconds to start and 30 seconds to end of input, save as output:

    sox input.wav output.wav pad 10 30

    Mixing audio

    with ffmpeg

    ffmpeg -i apologie.wav -i badmusic.wav -filter_complex amerge mix.mp3
    

    it uses the duration of the shortest file.

    you can also explicitly limit the number of audio channels with:

    ffmpeg -i apologie.wav -i badmusic.wav -filter_complex amerge -ac 1 mix.mp3
    

    Sequence (concatenate) audio / video

    For audio, you can use sox like this:

    sox a.wav b.wav c.wav output.wav

    For video, you can use mlt / melt .....

    melt a.mp4 b.mp4 c.mp4 -consumer avformat:output.mp4 b=1000k

    You can also use melt to combine still images as "inter-titles", the numbers with "out" are frames (so for output at 25 fps, 50 = 2 seconds):

    melt title2-0.png out=50 \
    video.mp4 \
    title2-1.png out=100 \
    -consumer avformat:output.mp4 b=1000k

    To do this with ffmpeg you need to make a sort of playlist file that looks like:

    Create a file called playlist.txt:

    file 'a.wav'
    file 'b.wav'
    file 'c.wav'
    

    Then the command to put the pieces together is:

    ffmpeg -f concat -i concat.txt concat.mp3
    

    See: https://trac.ffmpeg.org/wiki/Concatenate

    Text / Titles

    Imagemagick! http://www.imagemagick.org/Usage/text/

    convert -size 640x480 -gravity center -pointsize 72 -font CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY what about a very long text then?' title.png
    

    with a newline:

    convert -size 640x480 -gravity center -pointsize 72 -font CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY!what about a very long text then?' title.png
    

    What colors?

    convert -list color
    

    Random color!

    convert -list color | cut -d" " -f1 | shuf | head -n1
    

    There's also label:, but it doesn't do word wrap.

    convert -size 640x480 -gravity center -pointsize 72 -font ~/.fonts/coupeur-boldblock/CoupeurBoldBlock.otf -background lightblue -fill blue caption:'HEY what about a very long text then?' title.png</source>
    
    convert -size 640x480 -gravity center -pointsize 72 -font -background lightblue -fill blue label:'magic\nmagic!!!' whoami.png
    

    title with random colors:

    convert -size 640x480 -gravity center -pointsize 72 \
       -font ~/.fonts/coupeur-boldblock/CoupeurBoldBlock.otf \
       -background $(convert -list color | cut -d" " -f1 | shuf | head -n1) \
       -fill $(convert -list color | cut -d" " -f1 | shuf | head -n1) \
       label:'Surprise!!!' surprise.png
    

    TITLE

    c1=`shuf imcolornames.txt | head -n 1`
    c2=`shuf imcolornames.txt | head -n 1`
    convert -size 640x480 -background $c1 -fill $c2 -pointsize 72 -gravity center -f ont /home/murtaugh/.fonts/OSP_Libertinage/Libertinage-s.ttf label:Contemporaneit y contemporaneity.png
    

    NB: Using the @ sign with label to read from a file seems to be blocked (by some kind of security policy).... back quoting a file might work though?!