Recherche avancée

Médias (33)

Mot : - Tags -/creative commons

Sur d’autres sites (139)

  • How can I add chapters to a mp4 file using bash script and ffmpeg

    17 septembre 2023, par Rick T

    I'm trying to add chapters to an mp4 file using bash and ffmpeg. The chapters are located in a file called chapters.txt. When the bash script is run which is called add_chapters.sh it reads the chapters.txt file and creates a file called chapters.ffmetadata. The issue is that the START and END times in the file chapters.ffmetadata aren't being created correctly. How can I fix this ?

    


      

    1. I have a file called chapters.txt that has the chapters in it.

      


      00:00:00=Intro
00:02:12=What are selections
00:03:19=Booleans


      


    2. 


    3. The bash file I run is add_chapters.sh

      


      The code in it is :

      


      #!/bin/bash

input_file="chapters.txt"
output_file="chapters.ffmetadata"

# Create the FFmetadata chapter file
echo ";FFMETADATA1" > "$output_file"

previous_seconds=0

while IFS="=" read -r timestamp chapter_name; do
    # Convert start time to seconds
    seconds=$(date -u -d "1970-01-01 $timestamp" +"%s")

    # Calculate the end time (previous chapter's start time)
    end_seconds=$previous_seconds

    # Update previous_seconds for the next iteration
    previous_seconds=$seconds

    echo "[CHAPTER]" >> "$output_file"
    echo "TIMEBASE=1/1000" >> "$output_file"
    echo "START=${end_seconds}000" >> "$output_file"
    echo "END=${seconds}000" >> "$output_file"
    echo "title=$chapter_name" >> "$output_file"
done < "$input_file"

echo "Chapter file '$output_file' created successfully."

# Run FFmpeg to add chapters to the video
ffmpeg -i input_video.mp4 -i chapters.ffmetadata -map_metadata 1 -c:v copy -c:a copy -y output_video.mp4


      


    4. 


    5. It creates a file called chapters.ffmetadata

      


      ;FFMETADATA1
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=0000
title=Intro
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=132000
title=What are selections
[CHAPTER]
TIMEBASE=1/1000
START=132000
END=199000
title=Booleans
[CHAPTER]
TIMEBASE=1/1000
START=199000
END=0000
title=


      


      The START and END times aren't being calculated correctly. How can I fix this ?

      


    6. 


    



    


    UPDATE #1

    


    I'm almost there thanks to @Freeman code / answer.

    


    The issues in the generated
chapters.ffmetadata file are just.

    


      

    1. The first START= and END= are 0000

      


    2. 


    3. The Intro Chapter doesn't show up (most likely caused by the first START= and END= being 0000)

      


    4. 


    5. The Chapters seem to be shifted down by one timecode line (most likely caused by the first START= and END= being 0000)

      


       ;FFMETADATA1
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=0000
title=Intro
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=132000
title=What are selections ?
[CHAPTER]
TIMEBASE=1/1000
START=132000
END=199000
title=Booleans
[CHAPTER]
TIMEBASE=1/1000
START=199000
END=0000
title=
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=4459000
title=

      


    6. 


    



    


    UPDATE #2

    


    Strange ; I tried the changes @Freeman suggested but an error comes up now.

    


    Chapter end time 0 before start 199000
chapters.ffmetadata : Cannot allocate memory

    


    ;FFMETADATA1
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=0000
title=Intro
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=132000
title=What are selections?
[CHAPTER]
TIMEBASE=1/1000
START=132000
END=199000
title=Booleans
[CHAPTER]
TIMEBASE=1/1000
START=199000
END=0000
title=
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=4459000
title=


    



    


    UPDATE #3 using his awk changes

    


    ;FFMETADATA1
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=0000
title=Intro
[CHAPTER]
TIMEBASE=1/1000
START=0000
END=132000
title=What are selections?
[CHAPTER]
TIMEBASE=1/1000
START=132000
END=199000
title=Booleans
[CHAPTER]
TIMEBASE=1/1000
START=199000
END=0000
title=


    


  • How to clone last frame when using overlay in ffmpeg ? [closed]

    6 mai, par mikezang

    I use this code to slide previous and next pages up, this is no problems, can I remove png file 1970~1979-last.png and use the last frame of video file 1970~1979.mp4 to instead it ?

    


    script-01.txt

    


    color=white:864x504[c];
[0:v]scale=864:504:force_original_aspect_ratio=decrease,pad=864:504:-1:-1,setsar=1[s0];
[1:v]scale=864:504:force_original_aspect_ratio=decrease,pad=864:504:-1:-1,setsar=1[s1];
[c][s0]overlay=y=0-h*t[c];
[c][s1]overlay=y='if(between(t,0,18),max(H-h*t,0),max(0-h*(t-18),0-H))'[v],
[v]tpad=stop_mode=clone:stop_duration=10[s2];
[s2]drawtext=fontfile='/WINDOWS/Fonts/Arial.ttf':text='%{eif\:18-t\:d}':
box=1:boxborderw=10:boxcolor=orange@0.4:
x=w-tw-10:y=10:fontsize=24:fontcolor=red:enable='between(t,8,18)';


    


    script-01.bat

    


    ffmpeg -hide_banner -loglevel fatal ^
    -loop 1 -i "C:\myimages\1970~1979-last.png" ^
    -i "C:\myvideos\years\1980~1989.mp4" ^
    -filter_complex_script script-01.txt ^
    -t 18 -y v01.mp4


    


    I got it what I only need video files and capture last frame by pipe line to next step, so that the last frame image never neede ! Though the first slide has little problem when countdown finished !

    


    script-01.bat

    


    ffmpeg -hide_banner -loglevel fatal ^
    -sseof -0.03 -i "C:\myvideos\years\1970~1979.mp4" ^
    -vframes 1 -c:v png -f image2pipe - | ffmpeg  ^
    -hide_banner -loglevel fatal -i - ^
    -i "C:\myvideos\years\1980~1989.mp4" ^
    -filter_complex_script script-01.txt ^
    -t 18 -y v01.mp4


    


  • Scale with scale_vaapi and than pad, got error. Seems ok without "pad"

    24 mai 2023, par huangda1982

    My computer : customized Linux, haswell
I am trying to scale 1280x720 video to 640x450 in 640x480 frame with black borders. To do so, I run this command :

    


    ffmpeg -v warning -hide_banner -init_hw_device vaapi=vaapi0:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device vaapi0 -filter_hw_device vaapi0 -i 'my_video.mp4' -vf 'scale_vaapi=640:450,pad=640:480:0:14' -c:v h264_vaapi -q:v 18 -profile:v main -level 3.1 -c:a aac -q:a 3 -f mp4 out.mp4 -loglevel verbose


    


    And I got this error :

    


    Impossible to convert between the formats supported by the filter &#x27;Parsed_scale_vaapi_0&#x27; and the filter &#x27;auto_scaler_0&#x27;<br />&#xA;Error reinitializing filters!&#xA;Failed to inject frame into filter network: Function not implemented&#xA;Error while processing the decoded data for stream #0:0&#xA;

    &#xA;

    When I removed the ",pad=640:480:0:14" part, the command continued. I pressed ctrl-c, and did not check the result.

    &#xA;

    "-q:v 18", "-level 3.1" and "-q:a" options seem not work.

    &#xA;

    Full message :

    &#xA;

    [h264 @ 0x55dfeb529340] Reinit context to 1280x720, pix_fmt: yuv420p&#xA;Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;my_video.mp4&#x27;:&#xA;  Metadata:&#xA;    major_brand     : isom&#xA;    minor_version   : 512&#xA;    compatible_brands: mp41&#xA;    creation_time   : 1970-01-01T00:00:00.000000Z&#xA;    encoder         : PotEncoder&#xA;  Duration: 00:22:45.01, start: 0.000000, bitrate: 3264 kb/s&#xA;  Stream #0:0(und): Video: h264 (Constrained Baseline), 1 reference frame (avc1 / 0x31637661), yuv420p(left), 1280x720 [SAR 1:1 DAR 16:9], 3068 kb/s, 23.98 fps, 23.98 tbr, 1199 tbn, 47.96 tbc (default)&#xA;    Metadata:&#xA;      creation_time   : 1970-01-01T00:00:00.000000Z&#xA;      handler_name    : VideoHandler&#xA;      vendor_id       : [0][0][0][0]&#xA;  Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 192 kb/s (default)&#xA;    Metadata:&#xA;      creation_time   : 1970-01-01T00:00:00.000000Z&#xA;      handler_name    : SoundHandler&#xA;      vendor_id       : [0][0][0][0]&#xA;File &#x27;out.mp4&#x27; already exists. Overwrite? [y/N] y&#xA;Stream mapping:&#xA;  Stream #0:0 -> #0:0 (h264 (native) -> h264 (h264_vaapi))&#xA;  Stream #0:1 -> #0:1 (mp3 (mp3float) -> aac (native))&#xA;Press [q] to stop, [?] for help&#xA;[h264 @ 0x55dfeb553e40] Reinit context to 1280x720, pix_fmt: vaapi_vld&#xA;[graph_1_in_0_1 @ 0x55dfeb6b7580] tb:1/44100 samplefmt:fltp samplerate:44100 chlayout:0x3&#xA;[graph 0 input from stream 0:0 @ 0x55dfebb4d000] w:1280 h:720 pixfmt:vaapi_vld tb:1/1199 fr:1199/50 sar:1/1&#xA;[auto_scaler_0 @ 0x55dfebb4f940] w:iw h:ih flags:&#x27;bicubic&#x27; interl:0&#xA;[Parsed_pad_1 @ 0x55dfebb4c700] auto-inserting filter &#x27;auto_scaler_0&#x27; between the filter &#x27;Parsed_scale_vaapi_0&#x27; and the filter &#x27;Parsed_pad_1&#x27;&#xA;Impossible to convert between the formats supported by the filter &#x27;Parsed_scale_vaapi_0&#x27; and the filter &#x27;auto_scaler_0&#x27;&#xA;Error reinitializing filters!&#xA;Failed to inject frame into filter network: Function not implemented&#xA;Error while processing the decoded data for stream #0:0&#xA;[AVIOContext @ 0x55dfeb5522c0] Statistics: 0 seeks, 0 writeouts&#xA;[aac @ 0x55dfeb551600] Qavg: 354.000&#xA;[aac @ 0x55dfeb551600] 2 frames left in the queue on closing&#xA;[AVIOContext @ 0x55dfeb530500] Statistics: 1144787 bytes read, 2 seeks&#xA;Conversion failed!&#xA;

    &#xA;