With ffmpeg print onto clipped video hh:mm:ss time *from original* and hh:mm:ss total duration from original

huangapple go评论60阅读模式
英文:

With ffmpeg print onto clipped video hh:mm:ss time *from original* and hh:mm:ss total duration from original

问题

I am using arch linux and bash and ffmpeg, all are up to date and the latest versions.

我使用Arch Linux、Bash和FFmpeg,它们都是最新版本。

I am clipping a video that is 30 seconds long and wish to clip from 5 secs to 10 seconds to a new file, from the original.

我正在剪辑一个时长30秒的视频,并希望从原始视频中剪辑出从第5秒到第10秒的部分,保存为新文件。

In the bottom right hand corner of the clip I wish to show timestamps from the original video as follows

我希望在剪辑的右下角显示原始视频的时间戳,如下所示:

  • in the 5th second "00:00:05/ 00:00:30"
  • 在第5秒:"00:00:05/ 00:00:30"
  • in the 6th second "00:00:06/ 00:00:30"
  • 在第6秒:"00:00:06/ 00:00:30"
    etc
    等等
  • in the 10th second "00:00:10/ 00:00:30"
  • 在第10秒:"00:00:10/ 00:00:30"

This is an apparentley simple question(?) but the syntax of the command is not at all obvious and I am hoping an expert may shed some light on this.

这似乎是一个简单的问题(?),但命令的语法并不明显,我希望有专家可以为我解释一下。

All I have so far for the drawtext part, which does not do what I want as it only counts the elapsed time from t=0 of the new clip, whereas I want it to show the timestamp and total duration of the original clip

到目前为止,我只有drawtext部分的内容,但它不符合我的要求,因为它只计算了新剪辑的从t=0开始的已经过去的时间,而我想要显示原始剪辑的时间戳和总时长

drawtext I started with

我从以下的drawtext开始:

"drawtext=text='%{pts\:gmtime\:0\:%M\\\\\:%S}':fontsize=24:fontcolor=black:x=(w-text_w-10):y=(h-text_h-10)"

ffmpeg line with drawtext I have started with

我已经开始使用以下的ffmpeg命令行和drawtext:

ffmpeg -ss 00:00:05 -i "$in_file" -filter_complex "drawtext=fontfile=font.ttf:text='sample text':x=10:y=10:fontsize=12:fontcolor=white:box=1:boxcolor=black@0.5:boxborderw=5,drawtext=text='%{duration\:hms}':fontsize=12:fontcolor=black:x=(w-text_w-10):y=(h-text_h-10)" -t 5 -c:a copy -c:v libx264 out_file.mp4

Note: The provided drawtext text value needs adjustment to display the original video's timestamp and duration correctly.

英文:

I am using arch linux and bash and ffmpeg, all are up to date and the latest versions.

I am clipping a video that is 30 seconds long and wish to clip from 5 secs to 10 seconds to a new file, from the original.

In the bottom right hand corner of the clip I wish to show timestamps from the original video as follows

  • in the 5th second "00:00:05/ 00:00:30"
  • in the 6th second "00:00:06/ 00:00:30"
    etc
  • in the 10th second "00:00:10/ 00:00:30"

This is an apparentley simple question(?) but the syntax of the command is not at all obvious and I am hoping an expert may shed some light on this.

All I have so far for the drawtext part, which does not do what I want as it only counts the elapsed time from t=0 of the new clip, whereas I want it to show the timestamp and total duration of the original clip

drawtext I started with

"drawtext=text='%{pts\:gmtime\:0\:%M\\\\\:%S}':fontsize=24:fontcolor=black:x=(w-text_w-10):y=(h-text_h-10)"

ffmpeg line with drawtext I have started with

ffmpeg -ss 00:00:05 -i  "$in_file" -filter_complex "drawtext=fontfile=font.ttf:text='sample text':x=10:y=10:fontsize=12:fontcolor=white:box=1:boxcolor=black@0.5:boxborderw=5,drawtext=text='%{duration\:hms}':fontsize=12:fontcolor=black:x=(w-text_w-10):y=(h-text_h-10)" -t 5 -c:a copy -c:v libx264 out_file.mp4

答案1

得分: 0

以下是翻译好的部分:

FFmpeg演示时间戳%{pts}只能通过使用C gmtime函数将其归零并偏移,据我所知,时间设置为UNIX时间秒中的2000年1月1日,即946684800秒,或者在午夜时分的其他任意时间来实现。

UNIX时间中的946684800秒对应于我们计算的起始点,即2000年01/01的午夜00:00:00,我们可以在此基础上添加自己的偏移量,例如这种情况下的5秒。

我们不显示日期的%d-%m-%Y部分,所以我们的方法是不可见的。尽管需要一些时间来弄清楚。

# 设置文件名
in_file="my_video_file.mp4"
out_file="my_video__clipped.mp4"

# 设置要剪辑的开始和结束时间
start_time_z="00:00:05"
end_time_z="00:00:10"

# 获取输入视频的持续时间,以hh:mm:ss格式,并将其转换为秒
total_duration=$(ffprobe -v error -show_entries format=duration \
  -of default=noprint_wrappers=1:nokey=1 "$in_file" \
  | awk '{printf("%02d:%02d:%02d\n", ($1/3600), ($1%3600)/60, $1%60)}')

# 转义时间戳中的冒号,以便ffmpeg可以打印它们
total_duration="$(echo $total_duration | sed 's@:@\\:@g')"

# 显示原始视频的持续时间。检查我们是否正确获取了它
#### yad --text="\n\n$total_duration\n\n" --button="Submit":0

# 将剪辑开始时间从hh:mm:ss转换为秒
start_secs=$(echo "$start_time_z" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')

# 将剪辑结束时间从hh:mm:ss转换为秒
end_secs=$(echo "$end_time_z" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')

# 剪辑的持续时间
duration_z=$((end_secs-start_secs))

# 将开始时间添加到946684800,这是2000年1月1日的00:00:00
total_z=$(echo "$start_secs + 946684800" | bc)
echo $total_z

# 运行命令以创建主要剪辑
# 以下字符串演示了UNIX时间的使用
# 通过删除%d-%m-%Y部分,我们删除了01-01-2000部分
# 这似乎是将pts移动并添加时间偏移的唯一方法

ffmpeg -ss "$start_time_z" \
    -i "$in_file_name" \
    -filter_complex \
"drawtext=fontfile=font.ttf:text='%{pts\:gmtime\:$total_z\:%T} / "$total_duration"':fontsize=$main_clip_font_size:fontcolor=white:x=(w-text_w-10):y=(h-text_h-10)" \
       -t  $duration_z \
       -c:a copy -c:v libx264 \
       "$out_file"
英文:

Here is the solution.

The ffmpeg presentation timestamp %{pts} can only be zeroed and offest, as far as I know, using the C gmtime function, with the time set to 1st Jan 2000 in unix time seconds, ie 946684800 seconds, or some other arbitrary time at midnight.

946684800 seconds in UNIX time gives midnight 00:00:00 on 01/01/2000 as a starting point for our calculations, to which we can add our own offset, in this case 5 seconds, as follows.

We don't display the %d-%m-%Y portion of the date, so our method is not seen. Works very well. Took a bit of figuring out though.

# set file names
in_file="my_video_file.mp4"
out_file="my_video__clipped.mp4"

# set start and end times of the clip to be made
start_time_z="00:00:05"
end_time_z="00:00:10"   

######################################################################
##########                                                  ########## 
##########       get the duration of the                    ##########
##########          input video in hh:mm:ss format          ##########
##########        and convert it to seconds                 ##########
##########                                                  ########## 
######################################################################


total_duration=$(ffprobe -v error -show_entries format=duration \
  -of default=noprint_wrappers=1:nokey=1 "$in_file" \
  | awk '{printf("%02d:%02d:%02d\n", ($1/3600), ($1%3600)/60, $1%60)}')

# escape the ":" in the time stamp so that they can be printed by ffmpeg
total_duration="$(echo $total_duration | sed 's@:@\\:@g')"

# display the duration of the original video. Check we have captured it correctly
####    yad --text="\n\n$total_duration\n\n" --button="Submit":0


######################################################################
##########                                                  ########## 
##########       convert the variable                       ##########
##########             $start_time_z   which                ##########
##########                    = 00:00:05                    ##########
##########                                                  ##########
##########    into seconds and add to it unix time for      ##########
##########           00:00:00 on 1st Jan 2000               ##########
##########                                                  ########## 
######################################################################


# convert the clip start time from hh:mm:ss to seconds
start_secs=$(echo "$start_time_z" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')

# convert the clip end time from hh:mm:ss to seconds
end_secs=$(echo "$end_time_z" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')

# duration of the clip
duration_z=$((end_secs-start_secs))


# Add start time to 946684800 which is 00:00:00 on 1st Jan 2000
total_z=$(echo "$start_secs + 946684800" | bc)
echo $total


######################################################################
##########                                                  ########## 
##########   run the command to create the MAIN  clip       ##########
##########                                                  ########## 
######################################################################


#   below string shows unix time in action
#     by removing the %d-%m-%Y portion we remove the 01-01-2000 part
#       this appears to be the only way to shift pts and add a time shift onto it

# drawtext=fontfile=font.ttf:text='%{pts\:gmtime\:$total_z\:%d-%m-%Y %T} / "$total_duration"':fontsize=$main_clip_font_size:fontcolor=white:x=(w-text_w-10):y=(h-text_h-10)" \

ffmpeg -ss "$start_time_z" \
    -i "$in_file_name" \
    -filter_complex \
"drawtext=fontfile=font.ttf:text='%{pts\:gmtime\:$total_z\:%T} / "$total_duration"':fontsize=$main_clip_font_size:fontcolor=white:x=(w-text_w-10):y=(h-text_h-10)" \
       -t  $duration_z \
       -c:a copy -c:v libx264 \
       "$out_file"

huangapple
  • 本文由 发表于 2023年5月24日 19:31:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定